參考mysql
http://imysql.com/tag/gtidsql
http://mysqllover.com/?p=594數據庫
Mysql 基於GTID的主從複製及切換ide
1、主從複製配置測試
兩個mysql服務的my.cnf 中相關內容配置ui
[mysqld]spa
#從複製數據庫表設置日誌
replicate-wild-ignore-table = mysql.%,information_schema.%,innodb.%,innodb_log.%,performance_schema.%,test.%,tmp.% #複製時忽略數據庫及表orm
#my.cnf 開啓gtid server
log_slave_updates # 表示便可以當從也能夠當主
gtid_mode = on
enforce-gtid-consistency = true
10.123.2.128 my.cnf
[mysqld]
server_id=10128 # 全部 mysql服務保持各不相同
10.123.2.165 my.cnf
[mysqld]
server_id=10165 # 全部 mysql服務保持各不相同
# 驗證gtid已開啓
mysql> SHOW VARIABLES like '%gtid%';
+--------------------------+-----------+
| Variable_name | Value |
+--------------------------+-----------+
| enforce_gtid_consistency | ON |
| gtid_executed | |
| gtid_mode | ON |
| gtid_next | AUTOMATIC |
| gtid_owned | |
| gtid_purged | |
+--------------------------+-----------+
# 查看當前mysql服務的uuid,gtid的主從同步,絕大多數功能基於server_uuid
mysql> SHOW VARIABLES like '%server_uuid%';
ip: 10.235.2.128
server_uuid: aad2226a-6ed3-11e4-9ae0-080027cdcda2
ip: 10.235.2.165
server_uuid: 68a061ee-b013-11e4-845a-080027a36fd3
分別在10.235.2.12八、10.235.2.165兩個 mysql服務中添加 zabbix_repl用戶同步權限
mysql> grant Replication client,replication slave on *.* to 'zabbix_repl'@'%' identified by '123456';flush privileges;
10.235.2.165 的mysql服務中設置主mysql信息,其中的master_auto_position=1 參數會自動判斷從主的什麼位置同步數據
mysql> stop slave;
mysql> change master to MASTER_HOST='10.235.2.128',master_user='zabbix_repl',master_password='123456',MASTER_PORT = 3306,MASTER_CONNECT_RETRY = 60,master_auto_position=1;
mysql> start slave;
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.235.2.128
Master_User: zabbix_repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 551
Relay_Log_File: relay-log.000002
Relay_Log_Pos: 761
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
...
...
...
Master_Server_Id: 10128
Master_UUID: aad2226a-6ed3-11e4-9ae0-080027cdcda2 # 主數據庫的server_uuid
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: aad2226a-6ed3-11e4-9ae0-080027cdcda2:1-2 # 記錄了relay日誌從Master獲取了binlog日誌的位置的gtid,由master_uuid:事務id組成
Executed_Gtid_Set: 68a061ee-b013-11e4-845a-080027a36fd3:1-3, # 本機binlog執行位置的gtid,由 server_uuid:事務id 組成
aad2226a-6ed3-11e4-9ae0-080027cdcda2:1-2 # 主服務上binlog執行位置的gtid,由 master_uuid:事務id 組成
Auto_Position: 1
在10.235.2.128的主mysql中添加數據測試
mysql> create database test2;
mysql> show master status \G
*************************** 1. row ***************************
File: mysql-bin.000001
Position: 1014
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set: aad2226a-6ed3-11e4-9ae0-080027cdcda2:1-4
再次查看10.235.2.165從mysql同步狀態,同步正常
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| innodb |
| innodb_log |
| mysql |
| performance_schema |
| test |
| test2 | # 新添加的測試庫
| testdb1 |
| tmp |
+--------------------+
mysql> show slave status \G
*************************** 1. row ***************************
...
...
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
...
...
Master_UUID: aad2226a-6ed3-11e4-9ae0-080027cdcda2
...
...
Retrieved_Gtid_Set: aad2226a-6ed3-11e4-9ae0-080027cdcda2:1-4
Executed_Gtid_Set: 68a061ee-b013-11e4-845a-080027a36fd3:1-3,
aad2226a-6ed3-11e4-9ae0-080027cdcda2:1-4
Auto_Position: 1
2、主從切換
在10.235.2.165主機
mysql> stop slave;
在10.235.2.128主機
mysql> stop slave;
mysql> change master to MASTER_HOST='10.235.2.165',master_user='zabbix_repl',master_password='123456',MASTER_PORT = 3306,MASTER_CONNECT_RETRY = 60,master_auto_position=1;
mysql> start slave;
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.235.2.165
Master_User: zabbix_repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 1529
Relay_Log_File: relay-log.000002
Relay_Log_Pos: 888
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
...
...
Master_UUID: 68a061ee-b013-11e4-845a-080027a36fd3
...
...
Retrieved_Gtid_Set: 68a061ee-b013-11e4-845a-080027a36fd3:1-3
Executed_Gtid_Set: 68a061ee-b013-11e4-845a-080027a36fd3:1-3 # 通常與master mysql 10.235.2.165mysql的gtid相同
Auto_Position: 1
mysql>
在10.235.2.165主機
mysql> show master status \G
*************************** 1. row ***************************
File: mysql-bin.000001
Position: 1529
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set: 68a061ee-b013-11e4-845a-080027a36fd3:1-3, # 10.235.2.165 mysql的gtid
aad2226a-6ed3-11e4-9ae0-080027cdcda2:1-4
在10.235.2.165測試刪除數據
mysql> drop database test2;
Query OK, 0 rows affected (0.12 sec)
在10.235.2.128查看數據已經同步(已經沒有數據庫test2)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| innodb |
| innodb_log |
| mysql |
| performance_schema |
| test |
| testdb1 |
| tmp |
+--------------------+
到這裏主機切換已經完成
3、主主複製
原理:
一、雙方節點都得有建立具備複製權限的用戶 # 參考1、主從複製 添加權限用戶
二、雙方都得啓用中繼日誌和二進制日誌
三、爲保證具備自動增加功能的字段能正確生成ID,須要配置兩個節點分別使用偶數或奇數ID號
四、都要把對方配置爲本身的主節點 # 參考1、主從複製 change master to ...
在my.cnf添加下面相關配置
10.235.2.165 my.cnf配置
[mysqld]
auto-increment-offset = 1 #自增id爲奇數
auto-increment-increment = 2 #自增id的增量
log_slave_updates # 表示便可以當從也能夠當主
10.235.2.128 my.cnf配置
[mysqld]
auto-increment-offset = 2 #自增id爲偶數
auto-increment-increment = 2 #自增id的增量
log_slave_updates # 表示便可以當從也能夠當主
4、GTID主從複製遇到的異常狀況
錯誤1:
當備庫複製出錯時,傳統的跳過錯誤的方法是設置sql_slave_skip_counter,而後再START SLAVE。
但若是打開了GTID,就會設置失敗:
mysql> set global sql_slave_skip_counter = 1;
ERROR 1858 (HY000): sql_slave_skip_counter can not be set when the server is running with @@GLOBAL.GTID_MODE = ON. Instead, for each transaction that you want to skip, generate an empty transaction with the same GTID as the transaction
提示的錯誤信息告訴咱們,能夠經過生成一個空事務來跳過錯誤的事務。
咱們手動產生一個備庫複製錯誤:
Last_SQL_Error: Error ‘Unknown table ‘test.t1」 on query. Default database: ‘test’. Query: ‘DROP TABLE `t1` /* generated by server */’
查看binlog中,該DDL對應的GTID爲7a07cd08-ac1b-11e2-9fcf-0010184e9e08:1131
在備庫上執行:
mysql> STOP SLAVE;
Query OK, 0 rows affected (0.00 sec)
mysql> SET SESSION GTID_NEXT = '7a07cd08-ac1b-11e2-9fcf-0010184e9e08:1131';
Query OK, 0 rows affected (0.00 sec)
mysql> BEGIN; COMMIT;
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
mysql> SET SESSION GTID_NEXT = AUTOMATIC;
Query OK, 0 rows affected (0.00 sec)
mysql> START SLAVE;
再查看show slave status,就會發現錯誤事務已經被跳過了。這種方法的原理很簡單,空事務產生的GTID加入到GTID_EXECUTED中,這至關於告訴備庫,這個GTID對應的事務已經執行了。