標籤:mysql 主主複製
一、主主優點
雙機熱備的概念簡單說一下,就是要保持兩個資料庫的狀態自動同步。
對任何一個資料庫的操作都自動應用到另外一個資料庫,始終保持兩個資料庫資料一致。這樣做的好處:
(1)可以做災備,其中一個壞了可以切換到另一個。
(2)可以做負載平衡,可以將請求分攤到其中任何一台上,提高網站輸送量。
二、環境
系統內容:linux
Mysql版本:5.6.24
Master1IP:192.168.0.1
Master2IP:192.168.0.2
三、操作步驟
(1)在master1上建立專門備份的使用者
【master1】
mysql –uroot -pgrant replication slave on *.* to ‘repl_user‘@‘192.168.0.2‘identified by ‘repl_user‘;flush privileges;
(2)開啟主伺服器的binarylog
【master1】
vi /etc/my.cnfserver_id = 1log-bin=master-binbinlog_format=mixed read-only=0 binlog-ignore-db=mysql binlog-ignore-db=information_schema binlog-ignore-db=performance_schemaauto-increment-increment=5 auto-increment-offset=1
(3)擷取主伺服器的狀態和同步狀態
先鎖定資料庫:flush tables with read lock;匯出資料:mysqldump -root -p test>/tmp/test_0511.sql查看master1的binary日誌位置:show master status\G;*************************** 1. row*************************** File: master-bin.000009 Position: 6001 Binlog_Do_DB: Binlog_Ignore_DB:mysql,information_schema,performance_schemaExecuted_Gtid_Set:1 row in set (0.00 sec) ERROR:No query specified 解除鎖定:unlock tables;
(4)設定master2需要複製的資料庫
【master2】
vi /etc/my.cnfserver_id = 2log-bin = master-binbinlog_format = mixed replicate-ignore-db = mysqlreplicate-ignore-db = information_schemareplicate-ignore-db = performance_schemarelay_log=mysqld-relay-binlog-slave-updates = ON /etc/init.d/mysqld restart
(5)匯入初態, 開始同步。
create database test default charset utf8;use testsource /tmp/test_0511.sql CHANGE MASTER TO MASTER_HOST=‘192.168.0.1‘, MASTER_USER=‘repl_user‘, MASTER_PASSWORD=‘repl_user‘, MASTER_LOG_FILE=‘master-bin.000009‘, MASTER_LOG_POS=6001; start slave;show slave status\G;
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/6C/E7/wKioL1VVwgnRgdriAAAw6FWTk0I006.jpg" title="QQ20150515174615.png" alt="wKioL1VVwgnRgdriAAAw6FWTk0I006.jpg" />
注意圖中的紅框,兩個都是Yes,說明開啟成功。
(6)在master2上建立專門備份的使用者
【master2】
mysql –uroot -pgrant replication slave on *.* to ‘repl_user‘@‘192.168.0.1‘identified by ‘repl_user‘;flush privileges;
(7)開啟master2主伺服器的binarylog
vi /etc/my.cnfread-only=0binlog-ignore-db=mysqlbinlog-ignore-db=information_schemabinlog-ignore-db=performance_schemaauto-increment-increment=10auto-increment-offset=2 /etc/init.d/mysqld restart
(8) 開啟主伺服器的binarylog
Master2日誌狀態:
show master status\G;*************************** 1. row*************************** File: master-bin.000002 Position: 120 Binlog_Do_DB: Binlog_Ignore_DB:mysql,information_schema,performance_schemaExecuted_Gtid_Set:1 row in set (0.00 sec)
(9)修改master1設定檔
【maste1】開啟中繼
vi /etc/my.cnfreplicate-ignore-db = mysqlreplicate-ignore-db = information_schemareplicate-ignore-db = performance_schemarelay_log=mysqld-relay-binlog-slave-updates = ON /etc/init.d/mysqld restart
(10)啟動同步
CHANGE MASTER TO MASTER_HOST=‘192.168.0.2‘, MASTER_USER=‘repl_user‘, MASTER_PASSWORD=‘repl_user‘, MASTER_LOG_FILE=‘master-bin.000002‘, MASTER_LOG_POS=120;start slave;show slave status\G;
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/6C/E7/wKioL1VVwp3zfHgfAAAw6FWTk0I540.jpg" title="QQ20150515174615.png" alt="wKioL1VVwp3zfHgfAAAw6FWTk0I540.jpg" />
出現錯誤:[ERROR] Slave SQL: Slave failed toinitialize relay log info structure from the repository, Error_code: 1872解決方案:reset slave;
本文出自 “筆記” 部落格,請務必保留此出處http://sunflower2.blog.51cto.com/8837503/1651691
Mysql---主主互備