基於 Gtid 的 MySQL 主從同步實踐

前幾天,有讀者在後臺留言問我可有基於Gtid的Mysql主從同步的文章,我記得歷史文章應該有說起過,也有多是隻是說起,可能沒有詳細的過程介紹,因此,今天,民工哥就給你們安排一波。mysql

什麼是GTID?

  • 一、全局惟一,一個事務對應一個GTID
  • 二、替代傳統的binlog+pos複製;使用master_auto_position=1自動匹配GTID斷點進行復制
  • 三、MySQL5.6開始支持
  • 四、在傳統的主從複製中,slave端不用開啓binlog;可是在GTID主從複製中,必須開啓binlog
  • 五、slave端在接受master的binlog時,會校驗GTID值
  • 六、爲了保證主從數據的一致性,多線程同時執行一個GTID

組成

Master_UUID:序列號舉例:ceb0ca3d-8366-11e8-ad2b-000c298b7c9a:1-5ceb0ca3d-8366-11e8-ad2b-000c298b7c9a其實就是master的uuid值;1-5是序列號,每次一個事務完成都會自增1,也就是說下一次爲1-6。

工做原理

  • 一、master更新數據時,會在事務前產生GTID,一同記錄到binlog日誌中。
  • 二、slave端的i/o 線程將變動的binlog,寫入到本地的relay log中。
  • 三、sql線程從relay log中獲取GTID,而後對比slave端的binlog是否有記錄。
  • 四、若是有記錄,說明該GTID的事務已經執行,slave會忽略。
  • 五、若是沒有記錄,slave就會從relay log中執行該GTID的事務,並記錄到binlog。
  • 六、在解析過程當中會判斷是否有主鍵,若是沒有就用二級索引,若是沒有就用所有掃描

GTID主從配置

版本:MySQL5.7linux

配置master

vim /etc/my.cnf
 [client]
 socket=/usr/local/mysql/mysql.sock
 [mysqld]
 basedir=/usr/local/mysql
 datadir=/usr/local/mysql/data
 user=mysql
 pid-file=/usr/local/mysql/data/mysqld.pid
 log-error=/usr/local/mysql/data/mysql.err
 socket=/usr/local/mysql/mysql.sock
 port=3306
 server-id=1
 gtid-mode=ON
 enforce-gtid-consistency=ON
 server-id=1
 binlog_format=row
 log-bin=/usr/local/mysql/data/mysql-bin
systemctl restart mysqld
firewall-cmd --add-port=3306/tcp --permanent
firewall-cmd --reload

配置slave

vim /etc/my.cnf
 [client]
 socket=/usr/local/mysql/mysql.sock
 [mysqld]
 basedir=/usr/local/mysql
 datadir=/usr/local/mysql/data
 user=mysql
 pid-file=/usr/local/mysql/data/mysqld.pid
 log-error=/usr/local/mysql/data/mysql.err
 socket=/usr/local/mysql/mysql.sock
 port=3306
 server-id=2
 gtid-mode=ON
 enforce-gtid-consistency=ON
 server-id=2
 binlog_format=ROW
 log-bin=/usr/local/mysql/data/mysql-bin
 log_slave_updates=ON
 skip-slave-start=1
systemctl restart mysqld
firewall-cmd --add-port=3306/tcp --permanent
firewall-cmd --reload

master受權配置

mysql -uroot -p
mysql> grant replication slave on *.* to 'rep'@'10.0.0.%' identified by '123';
mysql> flush privileges;

slave配置同步

mysql -uroot -p
mysql> change master to master_host='10.0.0.132', master_user='rep',master_password='123',master_port=3306,master_auto_position=1;
mysql> start slave;

查看slave的狀態

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.0.0.132
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 635
               Relay_Log_File: slave-relay-bin.000005
                Relay_Log_Pos: 848
        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: 635
              Relay_Log_Space: 1308
              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: ceb0ca3d-8366-11e8-ad2b-000c298b7c9a
             Master_Info_File: /usr/local/mysql/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: ceb0ca3d-8366-11e8-ad2b-000c298b7c9a:1-4
            Executed_Gtid_Set: ceb0ca3d-8366-11e8-ad2b-000c298b7c9a:1-4
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

圖片

出現這兩個yes表示同步成功sql

圖片

經過slave的狀態信息,能夠看到GTID的值、Matser\_UUID等信息vim

查看master狀態

mysql> show master status;
+------------------+----------+--------------+------------------+------------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                        |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000003 |      635 |              |                  | ceb0ca3d-8366-11e8-ad2b-000c298b7c9a:1-4 |
+------------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)

注意對比slave端,Executed\_Gtid\_Set的值應該是同樣的。服務器

驗證主從

master上多線程

mysql> create database test01;
Query OK, 1 row affected (0.00 sec)

mysql> show master status;
+------------------+----------+--------------+------------------+------------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                        |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000003 |      800 |              |               | ceb0ca3d-8366-11e8-ad2b-000c298b7c9a:1-5 |
+------------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)

slave上socket

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test01             |
+--------------------+
5 rows in set (0.07 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.0.0.132
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 800
               Relay_Log_File: slave-relay-bin.000005
                Relay_Log_Pos: 1013
        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: 800
              Relay_Log_Space: 1473
              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: ceb0ca3d-8366-11e8-ad2b-000c298b7c9a
             Master_Info_File: /usr/local/mysql/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: ceb0ca3d-8366-11e8-ad2b-000c298b7c9a:1-5
            Executed_Gtid_Set: ceb0ca3d-8366-11e8-ad2b-000c298b7c9a:1-5
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

須要注意的是,GTID的值在完成一次事務後,變成了ceb0ca3d-8366-11e8-ad2b-000c298b7c9a:1-5(自增1)tcp

排障

思路
  • a、確保master開放3306端口
  • b、最好關閉selinux
  • c、master上受權同步,slave上change master命令指定master的信息不要寫錯
  • d、UUID問題

圖片

若是你出現了上圖所示的問題,表示你的master和slave的UUID是同樣的,通常這種狀況多出現於克隆虛擬機ide

解決辦法:

找到slave上的MySQL數據目錄下的auto.cnf文件(這個文件實際上是自動生成的mysql服務器的UUID值),將它刪除,而後重啓MySQL,而後MySQL會從新生成一個UUID。而後停掉slave,從新開啓就能夠了(個人mysql的數據目錄是在/usr/local/mysql/data下,詳情查看my.cnf配置文件)ui

cd /usr/local/mysql/data
rm -f auto.cnf
systemctl restart mysql
[root@slave data]# cat auto.cnf
[auto]
server-uuid=020c7f26-be57-11e8-8e2d-000c29b63bad

經過cat命令查看該文件,發現UUID已經改變

mysql -uroot -p
mysql> stop slave;
mysql> start slave;

總結

排障過程當中,注意須要停掉slave,作完修改以後在開啓,不然你的修改多是不會生效的。

來源: https://blog.51cto.com/u_1343...

相關文章
相關標籤/搜索