實(shí)現(xiàn)mysql級(jí)聯(lián)復(fù)制的方法示例
所謂級(jí)聯(lián)復(fù)制就是master服務(wù)器,只給一臺(tái)slave服務(wù)器同步數(shù)據(jù),然后slave服務(wù)器在向后端的所有slave服務(wù)器同步數(shù)據(jù),降低master服務(wù)器的寫壓力,和復(fù)制數(shù)據(jù)的網(wǎng)絡(luò)IO。
一,配置master服務(wù)器
1,修改主配置文件
vim /etc/my.cnf
在[mysql]配置塊下添加如下兩行配置
[mysql] log_bin #開啟二進(jìn)制日志功能 server_id=1 #為當(dāng)前節(jié)點(diǎn)設(shè)置一個(gè)全局惟一的ID號(hào)
2,重啟mysql服務(wù),使配置生效
systemctl restart mairadb
3,創(chuàng)建有復(fù)制權(quán)限的用戶賬號(hào)
GRANT REPLICATION SLAVE ON *.* TO 'repluser'@'HOST' IDENTIFIED BY 'replpass';
命令解析:
- 'repluser'@'HOST' :設(shè)置用戶名即主機(jī)ip或網(wǎng)段,網(wǎng)段用%表示 例如10.0.0.%
- IDENTIFIED BY:設(shè)置密碼
- *.* :表示所有數(shù)據(jù)庫(kù),所有表
- GRANT REPLCATION SLAVE:就是允許該用戶復(fù)制數(shù)據(jù)
該命令作用就是授權(quán)repluser能拷貝數(shù)據(jù)庫(kù)的所有內(nèi)容
二,中繼slave服務(wù)器配置
1,修改主配置文件
vim /etc/my.cnf
在[mysql]配置塊中添加如下兩行配置
[mysqld] log_bin server_id=2 #為當(dāng)前節(jié)點(diǎn)設(shè)置一個(gè)全局惟一的ID號(hào) read_only=ON #限制從服務(wù)器為只讀."注意:此限制對(duì)擁有SUPER權(quán)限的用戶均無效" log_slave_updates #該項(xiàng)的作用是把master服務(wù)器的二進(jìn)制日志計(jì)入到本機(jī),然后再把二進(jìn)制日志復(fù)制給后端的其他slave服務(wù)器
2,重啟mysql服務(wù),使配置生效
systemctl restart mariadb
3,使用有復(fù)制權(quán)限的用戶賬號(hào)連接至主服務(wù)器,并啟動(dòng)復(fù)制線程
CHANGE MASTER TO MASTER_HOST='host', #指定master主機(jī)IP MASTER_USER='repluser', #指定master被授權(quán)的用戶名 MASTER_PASSWORD='replpass',#指定被授權(quán)的用戶密碼 MASTER_LOG_FILE='mysql-bin.xxxxx', #指定從master服務(wù)器的那個(gè)二進(jìn)制日志開始復(fù)制 MASTER_LOG_POS=#; #二進(jìn)制日志位置,可以在master服務(wù)器上執(zhí)行該命令查看,show master logs; 啟動(dòng)復(fù)制線程IO_THREAD和SQL_THREAD START SLAVE;
4,查看中繼slave服務(wù)器狀態(tài)
MariaDB [(none)]> start slave; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.68.7 Master_User: repluser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mariadb-bin.000001 Read_Master_Log_Pos: 557 Relay_Log_File: mariadb-relay-bin.000002 Relay_Log_Pos: 843 Relay_Master_Log_File: mariadb-bin.000001 Slave_IO_Running: Yes "重點(diǎn)關(guān)注如果是NO表示線程沒起來" Slave_SQL_Running: Yes "重點(diǎn)關(guān)注 如果是NO表示該線程沒起來" Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 557 Relay_Log_Space: 1139 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 "該項(xiàng)表示同步時(shí)間 0表示即使同步" Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1
三,后端slave配置
1,修改配置文件
vim /etc/my.cnf
在[mysql]配置塊中添加如下兩行配置
[mysqld] server_id=3 #為當(dāng)前節(jié)點(diǎn)設(shè)置一個(gè)全局惟一的ID號(hào) read_only=ON #限制從服務(wù)器為只讀."注意:此限制對(duì)擁有SUPER權(quán)限的用戶均無效"
2,重啟mysql服務(wù),使配置生效
systemctl restart mariadb
3,使用有復(fù)制權(quán)限的用戶賬號(hào)連接至主服務(wù)器,并啟動(dòng)復(fù)制線程
CHANGE MASTER TO MASTER_HOST='中繼host', #指定中繼slave主機(jī)IP MASTER_USER='repluser', #指定master被授權(quán)的用戶名 MASTER_PASSWORD='replpass',#指定被授權(quán)的用戶密碼 MASTER_LOG_FILE='mysql-bin.xxxxx', #指定從中繼slave服務(wù)器的那個(gè)二進(jìn)制日志開始復(fù)制 MASTER_LOG_POS=#; #二進(jìn)制日志位置,可以在slave服務(wù)器上執(zhí)行該命令查看,show master logs; 啟動(dòng)復(fù)制線程IO_THREAD和SQL_THREAD START SLAVE;
4,查看slave服務(wù)器狀態(tài)
MariaDB [(none)]> start slave; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.68.17 Master_User: repluser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mariadb-bin.000001 Read_Master_Log_Pos: 557 Relay_Log_File: mariadb-relay-bin.000002 Relay_Log_Pos: 843 Relay_Master_Log_File: mariadb-bin.000001 Slave_IO_Running: Yes "重點(diǎn)關(guān)注如果是NO表示線程沒起來" Slave_SQL_Running: Yes "重點(diǎn)關(guān)注 如果是NO表示該線程沒起來" Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 557 Relay_Log_Space: 1139 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 "該項(xiàng)表示同步時(shí)間 0表示即使同步" Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1
5,最后在master服務(wù)器上創(chuàng)建數(shù)據(jù)庫(kù)測(cè)試即可查看是否同步
級(jí)聯(lián)復(fù)制特點(diǎn)
- 降低master服務(wù)器的壓力,網(wǎng)絡(luò)io壓力
- 但是會(huì)產(chǎn)生數(shù)據(jù)不一致的問題
總結(jié)
- 中繼slave需要打開二進(jìn)制日志,必須加上log_slave_updates配置項(xiàng)
- 注意read_only=ON作用,限制從服務(wù)器為只讀."注意:此限制對(duì)擁有SUPER權(quán)限的用戶均無效"
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持本站。
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。