詳解MySQL主從復(fù)制實(shí)戰(zhàn) - 基于日志點(diǎn)的復(fù)制
基于日志點(diǎn)的復(fù)制
1、在主庫(kù)與從庫(kù)上建立專用的復(fù)制賬號(hào)
MariaDB [employees]> create user 'repl'@'172.%' identified by '123456';
注意在生產(chǎn)上的密碼必須依照相關(guān)規(guī)范以達(dá)到一定的密碼強(qiáng)度, 并且規(guī)定在從庫(kù)上的特定網(wǎng)段上才能訪問(wèn)主庫(kù)
2、在主庫(kù)與從庫(kù)上授予復(fù)制權(quán)限
MariaDB [employees]> grant replication slave on *.* to 'repl'@'172.%';
3、配置主庫(kù)
注意啟用二進(jìn)制日志需要重啟服務(wù), 而server_id是一個(gè)動(dòng)態(tài)參數(shù), 可以結(jié)合命令行與配置文件以達(dá)到免重啟的持久化配置. 注意server_id在集群中是唯一的.
[mysqld] log_bin = /var/log/mysql/mariadb-bin log_bin_index = /var/log/mysql/mariadb-bin.index binlog_format = row server_id = 101
NOTE: 把日志與數(shù)據(jù)分開(kāi)是個(gè)好習(xí)慣, 最好能放到不同的數(shù)據(jù)分區(qū)
4、配置從庫(kù)
選項(xiàng)log_slave_update決定是否把中繼日志relay_log存放到本機(jī)的binlog中, 如果是配置鏈路復(fù)制, 那么該選項(xiàng)必填. 注意server_id在集群中是唯一的.
[mysqld] # replication log_bin = /var/log/mysql/mariadb-bin log_bin_index = /var/log/mysql/mariadb-bin.index server_id = 102 # slaves relay_log = /var/log/mysql/relay-bin relay_log_index = /var/log/mysql/relay-bin.index relay_log_info_file = /var/log/mysql/relay-bin.info log_slave_updates = ON read_only
5、初始化從庫(kù)的數(shù)據(jù)
此處使用mysqldump在主庫(kù)上進(jìn)行備份, 在生產(chǎn)上建議大家用xtrabackup進(jìn)行無(wú)鎖的熱備(基于innodb引擎).
備份主庫(kù)上的employees數(shù)據(jù)庫(kù)的數(shù)據(jù)
mysqldump --single-transaction --master-data=1 --triggers --routines --databases employees -u root -p >> backup.sql
將備份文件backup.sql通過(guò)scp或者docker volume卷掛載到從服務(wù)器上, 并且導(dǎo)入至從庫(kù)中
mysql -u root -p < backup.sql
6、啟動(dòng)復(fù)制鏈路
現(xiàn)有master@172.20.0.2和slave@172.20.0.3, 并且已經(jīng)通過(guò)mysqldump將數(shù)據(jù)同步至從庫(kù)slave中. 現(xiàn)在在從服務(wù)器slave上配置復(fù)制鏈路
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='master', MASTER_USER='repl', MASTER_PASSWORD='123456', MASTER_LOG_FILE='mariadb-bin.000029', MASTER_LOG_POS=516; Query OK, 0 rows affected (0.02 sec)
在從庫(kù)上啟動(dòng)復(fù)制鏈路
MariaDB [(none)]> start slave; Query OK, 0 rows affected (0.01 sec)
7、在從庫(kù)上檢查slave狀態(tài)
Slave_IO_Running與Slave_SQL_Running必須為YES, 如果出現(xiàn)錯(cuò)誤須詳細(xì)閱讀Last_IO_Error或Last_SQL_Error的提示信息
MariaDB [(none)]> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: master Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mariadb-bin.000029 Read_Master_Log_Pos: 516 Relay_Log_File: relay-bin.000002 Relay_Log_Pos: 539 Relay_Master_Log_File: mariadb-bin.000029 Slave_IO_Running: Yes Slave_SQL_Running: Yes 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: 516 Relay_Log_Space: 831 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 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: 101 Master_SSL_Crl: Master_SSL_Crlpath: Using_Gtid: No Gtid_IO_Pos: Replicate_Do_Domain_Ids: Replicate_Ignore_Domain_Ids: Parallel_Mode: conservative 1 row in set (0.00 sec)
8、在主庫(kù)檢查dump線程
檢測(cè)是否已經(jīng)正確啟動(dòng)binlog dump線程
MariaDB [(none)]> show processlist \G *************************** 1. row *************************** Id: 7 User: root Host: 172.20.0.1:41868 db: employees Command: Sleep Time: 56 State: Info: NULL Progress: 0.000 *************************** 2. row *************************** Id: 10 User: repl Host: 172.20.0.3:45974 db: NULL Command: Binlog Dump Time: 246 State: Master has sent all binlog to slave; waiting for binlog to be updated Info: NULL Progress: 0.000
可以看到row 2上有Command為Binlog Dump的命令被啟動(dòng), 證明復(fù)制線程已經(jīng)被成功啟動(dòng)
9、總結(jié)
優(yōu)點(diǎn)
- 技術(shù)成熟, BUG相對(duì)較少
- 對(duì)SQL查詢沒(méi)有任何限制, 如基于GTID復(fù)制時(shí)不是所有SQL都可以使用
缺點(diǎn)
- 故障轉(zhuǎn)移時(shí)重新獲取新主的日志偏移量較為困難
在一主多從環(huán)境下, 若舊master宕機(jī)后在集群中選舉出新master, 其他的從庫(kù)要對(duì)這個(gè)新的master進(jìn)行重新同步, 由于每個(gè)DB的binlog都是獨(dú)立存在, 所以很難找出開(kāi)始同步的日志點(diǎn)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持本站。
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。