1、術語解析mysql
1.TID:Transaction ID,事務的ID號:也就是說在mysql複製中每個事務都有本身的ID號(隨機數)sql
2.GTID:Global Transaction ID,全局事務ID,在整個事務架構中每個事務ID號是全局惟一的,不止是在一個節點上而是整個主從複製架構中每任何兩個事務的ID號都不會相同。數據庫
3.全局事務ID是怎麼生成的?簡單來說是由mysql服務器自動管理的,在mysql5.6之後每個mysql服務器都有一個全局惟一的ID號叫作uuid,通用惟一識別碼 (Universally Unique Identifier),而GTID就是由當前節點的UUID(一個128位的隨機數)和爲當前節點生成的隨機數(TID)組成的,所以只要UUID不一樣再在此基礎上保證事務ID不一樣就保證全局不同了。vim
4.全局事務ID有何用處?簡單來說GTID可以保證讓一個從服務器到其餘的從服務器那裏實現數據複製並且可以實現數據整合的。GTID在分佈式架構中能夠保證數據的一致性。從而也實現了mysql的高可用性。安全
5.GTID相關操做:默認狀況下將一個事務記錄進二進制文件時將首先記錄它的GTID並且GTID和事務相關信息一併要發送給從服務器由從服務器在在本地應用認證可是絕對不會改變原來的事務ID號。bash
6.所以在GTID的架構上就算有了N層架構,複製是N級架構、事務ID依然不會改變;有效的保證了數據的完整和安全性。服務器
2、新增選項 多線程
1.MySQL5.6引入的GTID(Global Transaction IDs)使得其複製功能的配置、監控及管理變得更加易於實現,且更加健壯。架構
2.要在MySQL 5.6中使用複製功能,其服務配置段[mysqld]中於少應該定義以下選項:分佈式
2.1. binlog-format:二進制日誌的格式,有row、statement和mixed幾種類型;
須要注意的是:當設置隔離級別爲讀提交READ-COMMITED必須設置二進制日誌格式爲ROW,如今MySQL官方認爲STATEMENT這個已經再也不適合繼續使用;但mixed類型在默認的事務隔離級別下,可能會致使主從數據不一致;log-slave-updates(slave更新是否記入日誌)、gtid-mode(gtid類型)、enforce-gtid-consistency(強制gtid一致性)、report-port和report-host:用於啓動GTID及知足附屬的其它需求;
2.2.master-info-repository(資源庫)和relay-log-info-repository:啓用此兩項,可用於實如今崩潰時保證二進制及從服務器安全的功能;
2.3.sync-master-info:確保服務器崩潰時無信息丟失;
2.4.slave-paralles-workers:設定從服務器的SQL線程數;0表示關閉多線程複製功能;值與你要複製的數據庫數目相同便可;
2.5.binlog-checksum校驗碼、master-verify-checksum和slave-sql-verify-checksum:啓用複製有關的全部校驗功能;
2.6.binlog-rows-query-log-events:用於在二進制日誌詳細記錄事件相關的信息,可下降故障排除的複雜度;
2.7.log-bin:啓用二進制日誌,這是保證複製功能的基本前提;
2.8.server-id:同一個複製拓撲中的全部服務器的id號必須唯一;
3、操做步驟
主服:station20:192.168.1.20
從服:station21:192.168.1.21
support-files/my-default.cnf 幾乎爲空文件,mysql-5.6不提供my.cnf
修改主節點配置文件
[root@station20~]# vim /etc/my.cnf server-id = 1 log-bin=mysql-bin binlog_format=row 二進制格式改成行row模式,三種模式statement語句模式,row行模式,mixed混合模式 當設置隔離級別爲READ-COMMITED必須設置二進制日誌格式爲ROW,如今MySQL官方認爲STATEMENT這個已經再也不適合繼續使用;但mixed類型在默認的事務隔離級別下,可能會致使主從數據不一致; #添加如下這些選項 log-slave-updates=true slave更新是否記入日誌 gtid-mode=on 啓用gtid類型,不然就是普通的複製架構 enforce-gtid-consistency=true 強制GTID的一致性 master-info-repository=TABLE 主服信息記錄庫=表/文件 relay-log-info-repository=TABLE 中繼日誌信息記錄庫 sync-master-info=1 同步主庫信息 slave-parallel-workers=4 從服務器的SQL線程數,要複製庫數目相同 binlog-checksum=CRC32 校驗碼 master-verify-checksum=1 主服校驗 slave-sql-verify-checksum=1 從服校驗 binlog-rows-query-log_events=1 二進制日誌詳細記錄事件 report-port=3306 提供複製報告端口 report-host=station20.example.com 提供複製報告主機 [root@station20~]# service mysqld restart [root@station20~]# mysql -e "show master status;" 已執行過的GTID集 Executed_Gtid_Set +------------------+----------+--------------+------------------+-------------------+ |File | Position | Binlog_Do_DB |Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ |mysql-bin.000002 | 120 | | | | +------------------+----------+--------------+------------------+-------------------+ mysql5.6之後每臺mysql服務器都有一個全局惟一的ID號叫作uuid,GTID就是由當前節點的UUID(一個128位的隨機數)和爲當前節點生成的隨機數(TID)組成的,所以只要UUID不一樣再在此基礎上保證事務ID不一樣就保證全局不同。 [root@station20~]# mysql -e "show global variables like '%uuid%';" +---------------+--------------------------------------+ |Variable_name | Value | +---------------+--------------------------------------+ |server_uuid |3eda76df-e355-11e3-8d42-000c294698bf | +---------------+--------------------------------------+ 建立複製用戶 mysql>grant replication slave,replication client on *.* to repluser@'192.168.1.%'identified by 'replpass'; mysql>flush privileges;
修改從節點配置文件
[root@station20~]# scp /etc/my.cnf 192.168.1.21:/etc/my.cnf [root@station21~]# vim /etc/my.cnf 從master複製my.cnf,修改關鍵部分 server-id = 2 log-bin=mysql-bin binlog_format=row log-slave-updates=true gtid-mode=on enforce-gtid-consistency=true master-info-repository=TABLE relay-log-info-repository=TABLE sync-master-info=1 slave-parallel-workers=4 binlog-checksum=CRC32 master-verify-checksum=1 slave-sql-verify-checksum=1 binlog-rows-query-log_events=1 report-port=3306 report-host=station21.example.com [root@station21~]# service mysqld restart [root@station21~]# mysql -e "show global variables like '%uuid%';" +---------------+--------------------------------------+ |Variable_name | Value | +---------------+--------------------------------------+ |server_uuid |08c840ad-e35c-11e3-8d6f-000c29ed6c68 | +---------------+--------------------------------------+ 從庫鏈接主庫 mysql>change master to master_host='192.168.1.20', master_user='repluser',master_password='replpass', master_auto_position=1; mysql>start slave; mysql>show slave status\G; ***************************1. row *************************** Slave_IO_State: Waiting formaster to send event Master_Host: 192.168.1.20 Master_User: repluser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 151 Relay_Log_File:station21-relay-bin.000002 不用配置,自動生成中繼日誌 Relay_Log_Pos: 361 Relay_Master_Log_File: mysql-bin.000003 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: 151 Relay_Log_Space: 569 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: 1 Master_UUID:3eda76df-e355-11e3-8d42-000c294698bf Master_Info_File:mysql.slave_master_info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has readall 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: Executed_Gtid_Set: Auto_Position: 1 mysql>show processlist; | Id | User | Host | db | Command | Time | State | Info | | 1 | systemuser | | NULL | Connect | 164 | Slave has read all relay log; waitingfor the slave I/O thread to update it | NULL | | 2 | systemuser | | NULL | Connect | 164 | Waiting for master to send event | NULL | | 3 | systemuser | | NULL | Connect | 164 | Waiting for an event fromCoordinator |NULL | | 4 | systemuser | | NULL | Connect | 164 | Waiting for an event fromCoordinator |NULL | | 5 | systemuser | | NULL | Connect | 164 | Waiting for an event fromCoordinator |NULL | | 6 | systemuser | | NULL | Connect | 164 | Waiting for an event fromCoordinator |NULL | | 7 | root | localhost | NULL | Query | 0| init | show processlist | 4個複製線程,主庫上同時建立4個數據庫,每一個數據庫事務啓動一個複製線程,這些複製線程可同時啓動; 測試主從測試 主庫 mysql>create database hellodb; mysql>show databases; +--------------------+ |Database | +--------------------+ |information_schema | |hellodb | |mysql | | performance_schema| |test | +--------------------+ mysql>show processlist; | Id | User |Host | db | Command | Time | State |Info | | 1 |repluser | slave:60241 | NULL | Binlog Dump GTID | 1178 | Master has sent allbinlog to slave; waiting for binlog to be updated | NULL | | 2 | root | localhost | NULL | Query | 0 | init | show processlist | 從庫 mysql>show databases; +--------------------+ |Database | +--------------------+ |information_schema | |hellodb | |mysql | |performance_schema | |test | +--------------------+ mysql>show processlist; | Id | User | Host | db |Command | Time | State | Info | 1 | systemuser | | NULL | Connect | 126 | Slave has read all relay log; waitingfor the slave I/O thread to update it | NULL | 2 | systemuser | | NULL | Connect | 920 | Waiting for master to send event | NULL | 3 | systemuser | | NULL | Connect | 920 | Waiting for an event fromCoordinator | NULL | 4 | systemuser | | NULL | Connect | 920 | Waiting for an event fromCoordinator | NULL | 5 | systemuser | | NULL | Connect | 920 | Waiting for an event fromCoordinator | NULL | 6 | systemuser | | NULL | Connect | 126 | Waiting for an event fromCoordinator | NULL | 7 | root |localhost | NULL | Query | 0| init | show processlist