-
配置Master數(shù)據(jù)庫
- 添加同步用的用戶
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.0.%' IDENTIFIED BY 'mysql';
- repl用戶必須具有REPLICATION SLAVE權(quán)限祸憋,除此之外沒有必要添加不必要的權(quán)限会宪,密碼為mysql。說明一下192.168.0.%蚯窥,這個配置是指明repl用戶所在服務(wù)器掸鹅,這里%是通配符,表示192.168.0.0-192.168.0.255的Server都可以以repl用戶登陸主服務(wù)器拦赠。當(dāng)然你也可以指定固定Ip巍沙。
- 在[mysqld]下面增加下面幾行代碼:
server-id=1 //主數(shù)據(jù)庫服務(wù)的唯一標(biāo)識號
log-bin=mysql-bin
2.1 想要配置哪些同步哪些不同步則添加如下信息:
binlog-do-db = test3 #要同步的數(shù)據(jù)庫
binlog-do-db = test4
binlog-ignore-db = mysql #不需要同步的數(shù)據(jù)庫
binlog_ignore_db = information_schema
binlog_ignore_db = performation_schema
binlog_ignore_db = sys
- 查看日志
mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000014 | 412 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
- 重啟MySQL服務(wù)
-
配置Slave從服務(wù)器
- 在[mysqld]下面增加下面幾行代碼:
server-id=100 //從數(shù)據(jù)庫服務(wù)的唯一標(biāo)識號
log-bin=mysql-bin
- 重啟mysql使配置生效
- 然后連接主庫
mysql> CHANGE MASTER TO
MASTER_HOST='192.168.10.212',
MASTER_PORT=3306,
MASTER_USER='repl',
MASTER_PASSWORD='mysql',
MASTER_LOG_FILE='mysql-bin.000014',
MASTER_LOG_POS=412
for channel '500';
Query OK, 0 rows affected
# 開始同步
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
stop slave; //停止同步
start slave; //開始同步
//必須和【主庫】的信息匹配。
CHANGE MASTER TO
MASTER_HOST='192.168.10.212', //主庫IP
MASTER_PORT=3306, //主庫端口
MASTER_USER='repl', //訪問主庫且有同步復(fù)制權(quán)限的用戶
MASTER_PASSWORD='mysql', //登錄密碼
【關(guān)鍵處】從主庫的該log_bin文件開始讀取同步信息荷鼠,主庫show master status返回結(jié)果
MASTER_LOG_FILE='mysql-bin.000014',
【關(guān)鍵處】從文件中指定位置開始讀取句携,主庫show master status返回結(jié)果
MASTER_LOG_POS=412 //主數(shù)據(jù)庫文件開始位置,查詢?nèi)罩緯r獲得
for channel '300'; //定義通道名稱
- 查看同步狀態(tài)
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.0.0.11
Master_User: tongbu
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000014
Read_Master_Log_Pos: 20014049
Relay_Log_File: centos02-relay-bin.000002
Relay_Log_Pos: 412
Relay_Master_Log_File: mysql-bin.000014
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
…………
- 配置完成颊咬,到主庫創(chuàng)建數(shù)據(jù)庫進(jìn)行一些數(shù)據(jù)操作务甥,驗證配置
-
遇到的一些錯誤
Slave_IO_Running: No //未運(yùn)行
查看從數(shù)據(jù)庫日志文件
Last_IO_Error: Got fatal error 1236 from master when reading data from binary log:
'Client requested master to start replication from impossible position'
查詢網(wǎng)上文檔解決 MySQL主從失敗 錯誤Got fatal error 1236解決方法
我自己直接改了MASTER_LOG_POS=412 解決,沒有從主數(shù)據(jù)庫找POS
其它一個用的解決文檔mysql 數(shù)據(jù)同步 出現(xiàn)Slave_IO_Running:No問題的解決方法小結(jié)
- 另外一個有用的文檔
MySQL5.7多主一從(多源復(fù)制)同步配置