首先配置主從的my.cnf文件
主
[mysqld]
log-bin=mysql-bin
server-id=1
從1和從2
[mysqld]
server-id=2
主配置從復(fù)制時使用的賬號密碼,%是全部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;
查看一下狀態(tài)
mysql>show master status;

狀態(tài)
進(jìn)入從數(shù)據(jù)庫配置
mysql> CHANGE MASTER TO MASTER_HOST='192.168.1.101', -- 主服務(wù)器IP
MASTER_USER='master', -- 主服務(wù)器用戶
MASTER_PASSWORD='123456', -- 主服務(wù)器用戶密碼
MASTER_LOG_FILE='binlog.000001', -- 主服務(wù)器
MASTER_LOG_POS=0; -- 位置
這個位置,就是你的binlog的行號,從頭開始復(fù)制就是0,從現(xiàn)在為止開始復(fù)制就是上面查詢出來的行號
啟動復(fù)制:
mysql> start slave;
查看狀態(tài)
mysql> show slave status\G;
我遇到的問題是,IO不通
Slave_IO_Running: Connecting
Slave_SQL_Running: Yes
因為我的默認(rèn)端口不是3306,所以需要重新配置一下
--更新一下端口:
mysql>CHANGE MASTER TO MASTER_PORT = 3300;
--啟動服務(wù)
mysql>START SLAVE;
mysql> show slave status\G
第二個問題
ERROR:
No query specified
原因是show slave status\G加了分號,show slave status\G; 去掉就好了
第三個問題:server ids相同。大概是因為我用的同一款docker鏡像導(dǎo)致的。
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:
因為有錯誤導(dǎo)致的。這種要么選擇排查錯誤并修改log文件,要么干脆跳過錯誤
set global sql_replica_skip_counter=100; 數(shù)量可以設(shè)置大一點
雙方數(shù)據(jù)庫都重啟,ok。