溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

MySQL 5.7如何使用GTID方式搭建復(fù)制環(huán)境

發(fā)布時間:2021-11-03 09:45:32 來源:億速云 閱讀:147 作者:小新 欄目:MySQL數(shù)據(jù)庫

這篇文章主要為大家展示了“MySQL 5.7如何使用GTID方式搭建復(fù)制環(huán)境”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“MySQL 5.7如何使用GTID方式搭建復(fù)制環(huán)境”這篇文章吧。

當(dāng)使用GTIDs(global transaction identifiers),每個事務(wù)在提交時,都會被標(biāo)記一個獨特的事務(wù)號,被用于在備庫上應(yīng)用,這樣在搭建復(fù)制環(huán)境時,不用使用日志文件和日志位置的傳統(tǒng)方式搭建,大大簡化了復(fù)制環(huán)境的搭建流程。可以使用語句級別和行級別的復(fù)制格式,建議使用行級別的復(fù)制格式。

GTID的格式如下
GTID = source_id:transaction_id
source_id代表源服務(wù)端,transaction_id代表事務(wù)的順序號。

限制:
因為以GTID為基礎(chǔ)的復(fù)制時基于事務(wù)的,一些特性在復(fù)制中會有限制。
不支持非事務(wù)性的表,如MyISAM表等。
不支持CREATE TABLE ... SELECT語句。CREATE TABLE ... SELECT對于語句級別的復(fù)制格式不安全。當(dāng)使用行級別的復(fù)制格式時,這個語句在日志中被記錄成兩個單獨的事件,一個是表的創(chuàng)建,另一個是表的插入操作。當(dāng)這個語句在一個事務(wù)中被執(zhí)行時,在某些情況下這兩個事件會被分配相同的事務(wù)號,這樣第2個執(zhí)行插入操作的事務(wù)可能會被從庫跳過。
臨時表。在事務(wù)內(nèi)部,GTID復(fù)制不支持CREATE TEMPORARY TABLE、DROP TEMPORARY TABLE語句。
GTID復(fù)制不支持sql_slave_skip_counter參數(shù)。如果需要跳過事務(wù),使用主庫上的gtid_executed參數(shù)。
主庫gtid_purged參數(shù)包含了所有在主庫二進(jìn)制日志中清除的所有事務(wù)。

搭建流程:

編輯主庫的配置文件,并重啟主庫
# Log
server-id = 27100
log-bin = production-bin
#log-bin-index = /log/production-bin.index
binlog_format = row
log_slave_updates
gtid-mode = ON
enforce-gtid-consistency = ON

編輯從庫的配置文件,并重啟從庫
# Log
server-id = 35100
log-bin = production-bin
#log-bin-index = /log/production-bin.index
binlog_format = row
log_slave_updates
gtid-mode = ON
enforce-gtid-consistency = ON

在主庫上導(dǎo)出備份,并傳輸?shù)綇膸?br/> [root@localhost 20160609]# mysqldump -uroot -p'System#2013' -S /var/lib/mysql/mysql.sock -A -R --single-transaction --default-character-set=utf8 > 20160609.sql

在從庫上應(yīng)用備份
[root@localhost 20160609]# mysql -uroot -p'System#2013' < 20160609.sql

在Master數(shù)據(jù)庫上面創(chuàng)建復(fù)制專用賬戶
mysql> grant replication slave on *.* to 'repl'@'192.168.78.%' identified by 'Mysql#2015';
Query OK, 0 rows affected, 1 warning (0.17 sec)

在從庫上執(zhí)行CHANGE MASTER命令
mysql> change master to
    -> master_host='192.168.78.141',
    -> master_port=3306,
    -> master_user='repl',
    -> master_password='Mysql#2015',
    -> master_auto_position = 1;
Query OK, 0 rows affected, 2 warnings (0.31 sec)

--啟動IO和SQL線程
mysql> start slave;
Query OK, 0 rows affected (0.04 sec)

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.78.141
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: production-bin.000002
          Read_Master_Log_Pos: 448
               Relay_Log_File: localhost-relay-bin.000002
                Relay_Log_Pos: 671
        Relay_Master_Log_File: production-bin.000002
             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: 448
              Relay_Log_Space: 882
              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: 27100
                  Master_UUID: cf291e84-2c89-11e6-b6f0-000c29631605
             Master_Info_File: /var/lib/mysql/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: cf291e84-2c89-11e6-b6f0-000c29631605:1
            Executed_Gtid_Set: cf291e84-2c89-11e6-b6f0-000c29631605:1
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

mysql> show processlist;
+----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
| Id | User        | Host      | db   | Command | Time | State                                                  | Info             |
+----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
|  4 | root        | localhost | fire | Query   |    0 | starting                                               | show processlist |
|  6 | system user |           | NULL | Connect |  480 | Waiting for master to send event                       | NULL             |
|  7 | system user |           | NULL | Connect |  153 | Slave has read all relay log; waiting for more updates | NULL             |
+----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
3 rows in set (0.02 sec)

以上是“MySQL 5.7如何使用GTID方式搭建復(fù)制環(huán)境”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI