docker mysql 主从

下载mysql

docker pull mysql:5.7

启动 mysql-master 和 mysql-slave

// mysql-master
docker run \
--name mysql-master \
--privileged=true \
-v /home/dev-soft/mysql/master/:/etc/mysql/conf.d \
-v /home/dev-soft/mysql/master/master-data:/var/lib/mysql \
-p 23306:3306 \
-e MYSQL_ROOT_PASSWORD=root \
-d docker.io/mysql:5.7

// mysql-slave
docker run \
--name mysql-slave \
--privileged=true \
-v /home/dev-soft/mysql/slave:/etc/mysql/conf.d \
-v /home/dev-soft/mysql/slave/slave-data:/var/lib/mysql \
-p 23307:3306 --link mysql-master:master \
-e MYSQL_ROOT_PASSWORD=root \
-d docker.io/mysql:5.7

在 mysql-master 上 创建用户并授权

docker exec -it mysql-master /bin/bash

mysql -uroot -proot

grant replication slave on *.* to 'test'@'%' identified by '123456';
flush privileges;

// 查看主的状态
show master status;

在 mysql-slave 上并配置 slave

docker exec -it mysql-slave /bin/bash

mysql -uroot -proot

change master to master_host='master', master_user='root', master_password='root',master_port=3306, master_log_file='mysql-bin.000003', master_log_pos=1024, master_connect_retry=30;

// 停止从库
stop slave;

// 启用从库
start slave;

// 查看从的状态
show slave status\G

//结果如下:
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: master
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 30
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 1024
               Relay_Log_File: 2639c6f2da5b-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

mysql 相关配置如下

//创建 master-data 目录
/home/dev-soft/mysql/master/master-data


//master: /home/dev-soft/mysql/master/master.cnf
[root@www master]# cat master.cnf
[mysqld]
##################基础设置##################
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /var/lib/mysql
#log-error      = /var/log/mysql/error.log

# By default we only accept connections from localhost
#bind-address   = 127.0.0.1
skip-host-cache
skip-name-resolve
#Mysql服务的唯一编号 每个mysql服务Id需唯一
server-id=1

#数据库默认字符集,主流字符集支持一些特殊表情符号(特殊表情符占用4个字节)
character-set-server = utf8mb4

#数据库字符集对应一些排序等规则,注意要和character-set-server对应
collation-server = utf8mb4_general_ci

#设置client连接mysql时的字符集,防止乱码
init_connect=‘SET NAMES utf8mb4‘

#是否对sql语句大小写敏感,1表示不敏感
lower_case_table_names = 1

##################主从复制设置##################
#开启mysql binlog功能
log-bin=/var/log/mysql/mysql-bin.index

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0


//创建 slave-data 目录
/home/dev-soft/mysql/slave/slave-data

// slave 配置:/home/dev-soft/mysql/slave/slave.cnf
[root@www slave]# cat slave.cnf
[mysqld]
##################基础设置##################
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /var/lib/mysql
#log-error      = /var/log/mysql/error.log

# By default we only accept connections from localhost
#bind-address   = 127.0.0.1
skip-host-cache
skip-name-resolve
#Mysql服务的唯一编号 每个mysql服务Id需唯一
server-id=2

#数据库默认字符集,主流字符集支持一些特殊表情符号(特殊表情符占用4个字节)
character-set-server = utf8mb4

#数据库字符集对应一些排序等规则,注意要和character-set-server对应
collation-server = utf8mb4_general_ci

#设置client连接mysql时的字符集,防止乱码
init_connect=‘SET NAMES utf8mb4‘

#是否对sql语句大小写敏感,1表示不敏感
lower_case_table_names = 1

##################主从复制设置##################
#开启mysql binlog功能
log-bin=/var/log/mysql/mysql-bin.index

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0


posted @ 2018-12-08 13:59  张建斌  阅读(99)  评论(0编辑  收藏