Mysql-5.6基於GTID主從複製

wKioL1OB8EeDF_SBAAEYFXPU8AE375.jpg

一、術語解析

1.TID:Transaction ID,事務的ID號:也就是說在mysql複製中每一個事務都有自己的ID號(隨機數)

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不同就保證全局不一樣了。

4.全局事務ID有何用處?簡單來講GTID能夠保證讓一個從服務器到其他的從服務器那裏實現數據複製而且能夠實現數據整合的。GTID在分佈式架構中可以保證數據的一致性。從而也實現了mysql的高可用性。

5.GTID相關操作:默認情況下將一個事務記錄進二進制文件時將首先記錄它的GTID而且GTID和事務相關信息一併要發送給從服務器由從服務器在在本地應用認證但是絕對不會改變原來的事務ID號。

6.因此在GTID的架構上就算有了N層架構,複製是N級架構、事務ID依然不會改變;有效的保證了數據的完整和安全性。

二、新增選項 

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號必須惟一;


三、操作步驟

主服: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




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章