首先配置主從的my.cnf文件
主
[mysqld]
log-bin=mysql-bin
server-id=1
從1和從2
[mysqld]
server-id=2
主配置從復(fù)制時(shí)使用的賬號(hào)密碼,%是全部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ù)庫(kù)配置
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; -- 位置
這個(gè)位置君编,就是你的binlog的行號(hào),從頭開始復(fù)制就是0川慌,從現(xiàn)在為止開始復(fù)制就是上面查詢出來的行號(hào)
啟動(dòng)復(fù)制:
mysql> start slave;
查看狀態(tài)
mysql> show slave status\G;
我遇到的問題是吃嘿,IO不通
Slave_IO_Running: Connecting
Slave_SQL_Running: Yes
因?yàn)槲业哪J(rèn)端口不是3306祠乃,所以需要重新配置一下
--更新一下端口:
mysql>CHANGE MASTER TO MASTER_PORT = 3300;
--啟動(dòng)服務(wù)
mysql>START SLAVE;
mysql> show slave status\G
第二個(gè)問題
ERROR:
No query specified
原因是show slave status\G加了分號(hào),show slave status\G兑燥; 去掉就好了
第三個(gè)問題:server ids相同亮瓷。大概是因?yàn)槲矣玫耐豢頳ocker鏡像導(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).
第四個(gè)問題:
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:
因?yàn)橛绣e(cuò)誤導(dǎo)致的降瞳。這種要么選擇排查錯(cuò)誤并修改log文件嘱支,要么干脆跳過錯(cuò)誤
set global sql_replica_skip_counter=100; 數(shù)量可以設(shè)置大一點(diǎn)
雙方數(shù)據(jù)庫(kù)都重啟,ok挣饥。