標籤:
項目中客戶需要高可用,資料庫是高可用的關鍵一部分。研究了很多mysql的高可用,用的比較多的就是mysql cluster 和mysql 主從主主複製。
mysql cluster過於複雜,有5個節點,當然最少要部署在兩台機器上,所有重點研究了一下主主複製。
1、設定檔 my.cnf
A節點
log-bin=mysql-bin
server-id = 1
binlog-do-db=test 主庫 需要複製的資料庫名,如果複製多個資料庫,重複設定這個選項即可
binlog-ignore-db=mysql 主庫不需要複製的資料庫苦命,如果複製多個資料庫,重複設定這個選項即可
replicate-do-db=test 從庫需要複製的資料庫名,如果複製多個資料庫,重複設定這個選項即可
replicate-ignore-db=mysql 從庫不需要複製的資料庫苦命,如果複製多個資料庫,重複設定這個選項即可
log-slave-updates
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=1
B節點:
log-bin=mysql-bin
server-id = 2
binlog-do-db=test
binlog-ignore-db=mysql
replicate-do-db=test
replicate-ignore-db=mysql
log-slave-updates
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=2
配置改完要要從起mysql才能進行下一步的操作。
AB節點各建一個對方訪問的帳號:
GRANT REPLICATION SLAVE ON *.* to ‘repl‘@‘192.168.10.131‘ identified by ‘password’;
操作之前最好沒有業務發生,如果有的話可以用主要資料庫鎖表的方式操作
flush tables with read lock;
AB節點查看show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000005 | 261 | | |
記錄下 FILE 及 Position 的值,在後面進行從伺服器操作的時候需要用到。
AB節點執行最後兩個參數都是對方的。
change master to master_host=‘192.168.21.10‘,master_user=‘repl‘,master_password=‘111111‘,master_log_file=‘mysqlmaster-bin.000001‘,master_log_pos=594;
ab節點啟動 start slave;
AB節點檢查:
show slave status\G
Slave_IO_State: Waiting for master to send event
Master_Host: 1xx.3.4x.2xx
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000120 //主庫BinaryLog名
Read_Master_Log_Pos: 974015581 //主庫BinaryLog位移量,只要有資料更新就會增長。
Relay_Log_File: mysqld-relay-bin.000020 //從庫中繼日誌名
Relay_Log_Pos: 320839203 //從庫中繼日誌位移量
Relay_Master_Log_File: mysql-bin.000120 //對當前主庫BinaryLog做中繼
Slave_IO_Running: Yes //接收更新狀態
Slave_SQL_Running: Yes //執行狀態
Replicate_Do_DB: nq //複製的庫
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 //可以跳過Last_Errno
Exec_Master_Log_Pos: 974015581 //執行到的位移量
Relay_Log_Space: 320839203 //中繼位移量
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
1 row in set (0.00 sec)
其中Slave_IO_Running 與 Slave_SQL_Running 的值都必須為YES,才表明狀態正常。如果有錯誤,優先查看錯誤記錄檔進行解決。
取消主要資料庫鎖定
mysql> UNLOCK TABLES;
剩下的就是驗證了。
我做過測試當一台機器關掉mysql服務後,在另一台機器操作,後面重啟後依然可以同步過去,只是不知道緩衝會保留多長時間。
mysql 主主複製