您好,登錄后才能下訂單哦!
1.一個事務,就會給一個gtid編號。來看看例子:
mysql> show master status; +---------------+----------+--------------+------------------+------------------------------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +---------------+----------+--------------+------------------+------------------------------------------------+ | binlog.000017 | 58105 | | | b526042e-89a7-11e8-bddb-000c29d7d637:1-1304086 | +---------------+----------+--------------+------------------+------------------------------------------------+ 1 row in set (0.00 sec) mysql> insert into student(id) values(5); Query OK, 1 row affected (0.01 sec) mysql> show master status; +---------------+----------+--------------+------------------+------------------------------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +---------------+----------+--------------+------------------+------------------------------------------------+ | binlog.000017 | 58432 | | | b526042e-89a7-11e8-bddb-000c29d7d637:1-1304087 | +---------------+----------+--------------+------------------+------------------------------------------------+ 1 row in set (0.00 sec) mysql> begin -> ; Query OK, 0 rows affected (0.00 sec) mysql> insert into student(id) values(6); Query OK, 1 row affected (0.00 sec) mysql> insert into student(id) values(7); Query OK, 1 row affected (0.00 sec) mysql> commit; Query OK, 0 rows affected (0.01 sec) mysql> show master status; +---------------+----------+--------------+------------------+------------------------------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +---------------+----------+--------------+------------------+------------------------------------------------+ | binlog.000017 | 58918 | | | b526042e-89a7-11e8-bddb-000c29d7d637:1-1304088 | +---------------+----------+--------------+------------------+------------------------------------------------+ 1 row in set (0.00 sec) mysql> begin ; Query OK, 0 rows affected (0.00 sec) mysql> insert into student(id) values(8); Query OK, 1 row affected (0.00 sec) mysql> rollback; Query OK, 0 rows affected (0.00 sec) mysql> show master status; +---------------+----------+--------------+------------------+------------------------------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +---------------+----------+--------------+------------------+------------------------------------------------+ | binlog.000017 | 58918 | | | b526042e-89a7-11e8-bddb-000c29d7d637:1-1304088 | +---------------+----------+--------------+------------------+------------------------------------------------+ 1 row in set (0.00 sec) mysql> quit Bye [root@yw-gz-hd-test-211 ~]# cat /data/mysql3308/auto.cnf [auto] server-uuid=b526042e-89a7-11e8-bddb-000c29d7d637
仔細看看上面的例子,當前gtid:b526042e-89a7-11e8-bddb-000c29d7d637:1304086,然后我插入一條數據,執行一個事務,gtid加1,變成b526042e-89a7-11e8-bddb-000c29d7d637:1304087,然后我用begin commit,標志另外一個事務,插入2條數據,gtid加1,變成b526042e-89a7-11e8-bddb-000c29d7d637:1304088,然后我rollback,gtid不加1.
到這里,我們停下來,思考一下。。。。。。。。。。。
gtid是什么東東?明白了吧,有所領悟了吧。
2.為什么說gtid能避免重復執行,避免丟數據呢?
來看下下面這個例子:
mysql> SET @@SESSION.GTID_NEXT= 'b526042e-89a7-11e8-bddb-000c29d7d637:1304076'/*!*/; Query OK, 0 rows affected (0.00 sec) mysql> select * from student where id=4; +----+------+------+---------+---------+---------------------+---------------------+ | id | name | age | textcol | blobcol | birday | birday2 | +----+------+------+---------+---------+---------------------+---------------------+ | 4 | 3 | 3 | 3 | NULL | 2018-07-25 10:42:29 | 2018-07-25 10:42:33 | +----+------+------+---------+---------+---------------------+---------------------+ 1 row in set (0.00 sec) mysql> CREATE TABLE `student` ( -> `id` int NOT NULL AUTO_INCREMENT , -> `name` varchar(255) NULL , -> `age` int NULL , -> `textcol` text NULL , -> `blobcol` blob NULL , -> `birday` timestamp NULL ON UPDATE CURRENT_TIMESTAMP , -> `birday2` datetime NULL ON UPDATE CURRENT_TIMESTAMP , -> PRIMARY KEY (`id`) -> ) -> ; Query OK, 0 rows affected (0.00 sec) mysql> show master status; +---------------+----------+--------------+------------------+------------------------------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +---------------+----------+--------------+------------------+------------------------------------------------+ | binlog.000017 | 58105 | | | b526042e-89a7-11e8-bddb-000c29d7d637:1-1304086 | +---------------+----------+--------------+------------------+------------------------------------------------+ 1 row in set (0.00 sec) mysql> insert into studnet(id) values(5); ERROR 1837 (HY000): When @@SESSION.GTID_NEXT is set to a GTID, you must explicitly set it to a different value after a COMMIT or ROLLBACK. Please check GTID_NEXT variable manual page for detailed explanation. Current @@SESSION.GTID_NEXT is 'b526042e-89a7-11e8-bddb-000c29d7d637:1304076'.
這個信息量有點大。一點點理解,首先我
SET @@SESSION.GTID_NEXT= 'b526042e-89a7-11e8-bddb-000c29d7d637:1304076'
但是當前gtid是b526042e-89a7-11e8-bddb-000c29d7d637:1-1304086 。什么意思,就是說1304076 事務包含在1-1304086。也就是說,這個事務已經執行了,不需要再執行了。理解了嗎?
思考一下。。。。。。。。。
這個理解通了,你就會發現,為什么我發布了一個create table 命令,查詢返回Query OK, 0 rows affected (0.00 sec)
就是什么作用也沒有。意思就是說,這個命令create table 我已經執行過了,所以不需要執行。
再來看看錯誤:ERROR 1837 (HY000): When @@SESSION.GTID_NEXT is set to a GTID, you must explicitly set it to a different value after a COMMIT or ROLLBACK. Please check GTID_NEXT variable manual page for detailed explanation. Current @@SESSION.GTID_NEXT is 'b526042e-89a7-11e8-bddb-000c29d7d637:1304076'.
意思就是說,你每執行一個命令,都必須先執行 set @@SESSION.GTID_NEXT = xxxxx。
為什么會這樣呢?
思考一下。。。。。。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。