确保在主服务器上 skip_networking 选项处于 OFF 关闭状态, 这是默认值。如果是启用的,则从站无法与主站通信,并且复制失败。
sudodockerexecmysql_master/bin/sh-c"mysql -uroot -p123456 -e \"show variables like '%skip_networking%';\""
Mysql 双机热备
A: 172.17.0.2
B: 172.17.0.3
1. 首先创建测试数据库, <A、B 都需要创建>
create database test default charset utf8;
2. 创建同步用户,<A、B 都需要创建>
# A
grant replication slave on *.* to 'replicate'@'172.17.0.3' identified by '123456';
flush privileges;
# B
grant replication slave on *.* to 'replicate'@'172.17.0.2' identified by '123456';
flush privileges;
# 在 A,B 上测试连通性
mysql -ureplicate -p123456 -h172.17.0.2 -e 'show databases'
mysql --p123456 -h172.17.0.3 -e 'show databases'
3. 修改数据库配置文件,然后重启
# A
server-id = 1
log-bin = mysql-bin
binlog-do-db = test
binlog-ignore-db = mysql
log-slave-updates
sync_binlog = 1
auto_increment_offset = 1
auto_increment_increment = 2
replicate-do-db = test
replicate-ignore-db = mysql,information_schema
# B 配置同上,只修改 `server-id`
server-id = 2
3. 修改mysql配置文件,重启 MySQL 服务
# A, B 机器查看状态
show master status\G;
# A
change master to MASTER_HOST='172.17.0.3', MASTER_PORT=3306, MASTER_USER='replicate',MASTER_PASSWORD='123456',master_log_file='mysql-bin.000001',master_log_pos=0;
# B
change master to MASTER_HOST='172.17.0.2', MASTER_PORT=3306,MASTER_USER='replicate',MASTER_PASSWORD='123456',master_log_file='mysql-bin.000001',master_log_pos=0;