docker启动mysql-master
- 启动mysql master节点
docker run -p 3307:3306 --name mysql-master \
-v /mydata/mysql/master/log:/var/log/mysql \
-v /mydata/mysql/master/data:/var/lib/mysql \
-v /mydata/mysql/master/conf:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=root \
-d mysql:5.7
- 在目录 /mydata/mysql/master/conf下,编辑my.cnf配置文件,加入如下代码
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
init_connect='SET collation_connection=utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
skip-name-resolve
server_id=1
log-bin=mysql-bin
read-only=0
# 需要同步的数据库,多个库,可以写多行
# binlog-do-db=db1
# binlog-do-db=db2
binlog-do-db=需要同步的数据库
# 忽略同步的库
replicate-ignore-db=mysql
replicate-ignore-db=sys
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
- 修改完成my.cnf配置文件后,重启mysql-master
docker restart mysql-master
- 用数据库连接工具,连接到master,执行如下SQL:
grant replication slave on *.* to 'backup'@'%' identified by '123456';
show master status;
看到如图示,说明master配置成功:
docker启动mysql-slave
- 启动mysql slave节点
docker run -p 3317:3306 --name mysql-slave-01 \
-v /mydata/mysql/slaver/log:/var/log/mysql \
-v /mydata/mysql/slaver/data:/var/lib/mysql \
-v /mydata/mysql/slaver/conf:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=root \
-d mysql:5.7
- 在目录 /mydata/mysql/slaver/conf下,编辑my.cnf配置文件,加入如下代码
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
init_connect='SET collation_connection=utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
skip-name-resolve
server_id=2
log-bin=mysql-bin
read-only=1
binlog-do-db=需要同步的库
replicate-ignore-db=mysql
replicate-ignore-db=sys
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
- 修改完成my.cnf配置文件,重启slave
docker restart mysql-slave-01
- 用数据库连接工具,连接到slave,执行如下SQL:
# 值 master_log_file 参考配置master时,第4步输出结果中file字段
# master_port 字段的值为master节点启动时,堆外暴露的端口
# master_host 为主节点的ip
# master_user, master_password 值为配置master时,第4步创建的用户,密码
change master to master_host='192.168.94.133', master_user='backup', master_password='123456',
master_log_file='mysql-bin.000003', master_log_pos=0,master_port=3307;
start slave;
show slave status;
看到如图结果,则表示配置成功