首先配置主从的my.cnf文件
主
[mysqld]
log-bin=mysql-bin
server-id=1
从1和从2
[mysqld]
server-id=2
主配置从复制时使用的账号密码,%是全部ip,不建议
CREATE USER 'copylocal'@'%' IDENTIFIED WITH mysql_native_password BY 'copyAll';
CREATE USER 'copynew'@'%' IDENTIFIED WITH mysql_native_password BY 'copyAll';
GRANT REPLICATION SLAVE ON *.* TO 'copylocal'@'%';
GRANT REPLICATION SLAVE ON *.* TO 'copynew'@'%';
flush privileges;
查看一下状态
mysql>show master status;
状态
进入从数据库配置
mysql> CHANGE MASTER TO MASTER_HOST='192.168.1.101', -- 主服务器IP
MASTER_USER='master', -- 主服务器用户
MASTER_PASSWORD='123456', -- 主服务器用户密码
MASTER_LOG_FILE='binlog.000001', -- 主服务器
MASTER_LOG_POS=0; -- 位置
这个位置,就是你的binlog的行号,从头开始复制就是0,从现在为止开始复制就是上面查询出来的行号
启动复制:
mysql> start slave;
查看状态
mysql> show slave status\G;
我遇到的问题是,IO不通
Slave_IO_Running: Connecting
Slave_SQL_Running: Yes
因为我的默认端口不是3306,所以需要重新配置一下
--更新一下端口:
mysql>CHANGE MASTER TO MASTER_PORT = 3300;
--启动服务
mysql>START SLAVE;
mysql> show slave status\G
第二个问题
ERROR:
No query specified
原因是show slave status\G加了分号,show slave status\G; 去掉就好了
第三个问题:server ids相同。大概是因为我用的同一款docker镜像导致的。
Last_IO_Errno: 13117
Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server ids; these ids must be different for replication to work (or the --replicate-same-server-id option must be used on slave but this does not always make sense; please check the manual before using it).
第四个问题:
Last_SQL_Error: Coordinator stopped because there were error(s) in the worker(s). The most recent failure being: Worker 1 failed executing transaction 'ANONYMOUS' at master log binlog.000005, end_log_pos 3827262. See error log and/or performance_schema.replication_applier_status_by_worker table for more details about this failure or others, if any.
Replicate_Ignore_Server_Ids:
因为有错误导致的。这种要么选择排查错误并修改log文件,要么干脆跳过错误
set global sql_replica_skip_counter=100; 数量可以设置大一点
双方数据库都重启,ok。
网友评论