MySQL GTID複製

like052629發表於2015-07-20

什麼是GTID

什麼是GTID呢, 簡而言之,就是全域性事務ID(global transaction identifier ),最初由google實現,官方MySQL在5.6才加入該功能。
GTID是事務提交時建立分配的唯一識別符號,所有事務均與GTID一一對映。

GTID的格式類似於:
5882bfb0-c936-11e4-a843-000c292dc103:1
這個字串,用“:”分開,前面表示這個伺服器的server_uuid,這是一個128位的隨機字串,在第一次啟動時生成(函式generate_server_uuid),對應的variables是隻讀變數server_uuid。 它能以極高的機率保證全域性唯一性,並存到檔案
DATADIR/auto.cnf中。因此要注意保護這個檔案不要被刪除或修改。

第二部分是一個自增的事務ID號,事務id號+server_uuid來唯一標示一個事務。

mysql> show global variables like '%gtid%';
+--------------------------+------------------------------------------+
| Variable_name            | Value                                    |
+--------------------------+------------------------------------------+
| enforce_gtid_consistency | ON                                       |
| gtid_executed            | 5882bfb0-c936-11e4-a843-000c292dc103:1-6 |
| gtid_mode                | ON                                       |
| gtid_owned               |                                          |
| gtid_purged              |                                          |
+--------------------------+------------------------------------------+
5 rows in set (0.00 sec)

mysql> show global variables like '%uuid%';
+---------------+--------------------------------------+
| Variable_name | Value                                |
+---------------+--------------------------------------+
| server_uuid   | 5882bfb0-c936-11e4-a843-000c292dc103 |
+---------------+--------------------------------------+
1 row in set (0.00 sec)

shell> cat auto.cnf 
[auto]
server-uuid=5882bfb0-c936-11e4-a843-000c292dc103

設定GTID複製

  1. 同步主從資料
    mysql> SET @@global.read_only = ON;
    Query OK, 0 rows affected (0.01 sec)
  2. 停止所有資料庫
    shell> mysqladmin -u root -p shutdown
  3. 設定開發GTID模式並啟動所有資料庫

    shell> vi my.cnf  新增如下內容
    ================================================================
    [mysqld]
    gtid_mode=ON
    log-slave-updates=ON
    enforce-gtid-consistency=ON          #強制GTID的一致性
    ================================================================
  4. 從庫指定主庫

    mysql> CHANGE MASTER TO  
         -> MASTER_HOST = host, 
         -> MASTER_PORT = port, 
         -> MASTER_USER = user, 
         -> MASTER_PASSWORD = password, 
         -> MASTER_AUTO_POSITION = 1;
    
    mysql> START SLAVE;
    Query OK, 0 rows affected (0.04 sec)
  5. 禁止read-only模式

    mysql> SET @@global.read_only = OFF;
    Query OK, 0 rows affected (0.00 sec)

GTID複製的限制

GTID 模式例項和非GTID模式例項是不能進行復制的,要求非常嚴格,要麼都是GTID,要麼都不是
gtid_mode 是隻讀的,要改變狀態必須1)關閉例項、2)修改配置檔案、3) 重啟例項

  • 更新非事務引擎表
    在同一事務中更新事務表與非事務表將導致多個GTIDs分配給同一事務
mysql> cretea table tt (id int) engine=myisam;
mysql> insert into tt values(1),(2);
mysql> cretea table t (id int) engine=innodb;
mysql> insert into t values(1),(2);
mysql> set autocommit = 0;
mysql> begin;

mysql> update t set id = 3 where id =2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> update tt set id = 3 where id =2;
ERROR 1785 (HY000): When @@GLOBAL.ENFORCE_GTID_CONSISTENCY = 1, updates to non-transactional tables can 
only be done in either autocommitted statements or single-statement transactions, and never in the same 
statement as updates to transactional tables.
  • CREATE TABLE … SELECT statements
    不安全的基於語句複製,實際是兩個獨立的事件,一個用於建表,一個用於向新表插入源表資料。
mysql> create table t engine=innodb as select * from tt;
ERROR 1786 (HY000): CREATE TABLE ... SELECT is forbidden when @@GLOBAL.ENFORCE_GTID_CONSISTENCY = 1.
  • 臨時表
    事務內部不能執行建立刪除臨時表語句,但可以在事務外執行,但必須設定set autocommit = 1
mysql> create temporary table tttt(id int);
ERROR 1787 (HY000): When @@GLOBAL.ENFORCE_GTID_CONSISTENCY = 1, the statements CREATE TEMPORARY TABLE and 
DROP TEMPORARY TABLE can be executed in a non-transactional context only, and require that AUTOCOMMIT = 1.
mysql> set autocommit = 1;
Query OK, 0 rows affected (0.00 sec)

mysql> create temporary table tttt(id int);
Query OK, 0 rows affected (0.04 sec)
  • 不執行不支援的語句
    啟用--enforce-gtid-consistency選項啟動GTID模式,上述不支援的語句將會返回錯誤。

運維操作

a. 忽略複製錯誤
當備庫複製出錯時,傳統的跳過錯誤的方法是設定sql_slave_skip_counter,然後再START SLAVE。
但如果開啟了GTID,就會設定失敗:

mysql> stop slave;
Query OK, 0 rows affected (0.03 sec)

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

提示的錯誤資訊告訴我們,可以透過生成一個空事務來跳過錯誤的事務。
我們手動產生一個備庫複製錯誤:

[slave]
mysql> alter table t add primary key pk_id(id);
Query OK, 2 rows affected (0.12 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> insert into t values(1);
mysql> insert into t values(4);
mysql> insert into t values(5);

mysql> show master status ;
+-------------------+----------+--------------+------------------+-------------------------------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                         |
+-------------------+----------+--------------+------------------+-------------------------------------------+
| mysql-info.000004 |      914 |              |                  | 5882bfb0-c936-11e4-a843-000c292dc103:1-17 |
+-------------------+----------+--------------+------------------+-------------------------------------------+
1 row in set (0.00 sec)

mysql> show slave status \G
*************************** 1. row ***************************
     ...
       Slave_IO_Running: Yes
       lave_SQL_Running: No
             Last_Errno: 1062
             Last_Error: Could not execute Write_rows event on table db_test.t; Duplicate entry '1' for key 'PRIMARY', 
       Error_code: 1062; handler error HA_ERR_FOUND_DUPP_KEY; the event's master log mysql-info.000004, end_log_pos 401
     Retrieved_Gtid_Set: 5882bfb0-c936-11e4-a843-000c292dc103:1-15
      Executed_Gtid_Set: 5882bfb0-c936-11e4-a843-000c292dc103:1-14,
                         f1e6584a-c935-11e4-a840-000c29348dbe:1
          Auto_Position: 1
1 row in set (0.00 sec)


mysql> SET @@SESSION.GTID_NEXT= '5882bfb0-c936-11e4-a843-000c292dc103:15';
Query OK, 0 rows affected (0.00 sec)

mysql> begin;
Query OK, 0 rows affected (0.00 sec)

mysql> commit;
Query OK, 0 rows affected (0.00 sec)

mysql> SET SESSION GTID_NEXT = AUTOMATIC;

mysql> start slave;

mysql> show slave status\G
*************************** 1. row ***************************
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
                   Last_Errno: 0
           Retrieved_Gtid_Set: 5882bfb0-c936-11e4-a843-000c292dc103:1-17
            Executed_Gtid_Set: 5882bfb0-c936-11e4-a843-000c292dc103:1-17,
                               f1e6584a-c935-11e4-a840-000c29348dbe:1
                Auto_Position: 1
1 row in set (0.00 sec)

再檢視show slave status,就會發現錯誤事務已經被跳過了。這種方法的原理很簡單,空事務產生的GTID加入到GTID_EXECUTED中,
這相當於告訴備庫,這個GTID對應的事務已經執行了,此時主從資料不一致。

整理自網路

Svoid
2015-03-17

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28258625/viewspace-1742583/,如需轉載,請註明出處,否則將追究法律責任。

相關文章