• 【MySQL】GTID小结


    1.GTID的概念

    GTID(global transaction identifier)是全局事务标识符,在MySQL5.6版本中作为一个超级特性被推出。事务标识不仅对于Master(起源)的服务器来说是惟一的,而且在整个复制拓扑架构来说,也是全局唯一的。

    1)GTID的格式

    GTID = source_id:transaction_id 
    

    其中source_id :通过使用MySQL服务的server_uuid来表示 ,transaction_id :是在事务提交的时候系统顺序分配的一个序列号

    2)mysql.gtid_executed表

    GTIDs都存储在gtid_executed数据表中,在mysql系统数据库中。每一行的数据代表一个GTID或者一个GTID集合。包括source_uuid,集合开始的事务id和集合结束的事务id

    表结构如下:

    CREATE TABLE gtid_executed (
        source_uuid CHAR(36) NOT NULL,
        interval_start BIGINT(20) NOT NULL,
        interval_end BIGINT(20) NOT NULL, 
        PRIMARY KEY (source_uuid, interval_start)
        )
    

    备注:事务并不是立马写进gtid_executed表。当启用二进制日志的时候(log-bin = /data/mysqldata/3306/binlog/mysql-bin),只有日志被轮询或者数据库服务被关闭的时候,才会把所有的日志写入到gtid_executed数据表中。

    2.实战例子

    1)关闭数据库

    /usr/local/mysql/bin/mysqladmin -uroot -p'zsd@7101'  shutdown
    

    2)修改my.cnf文件

    gtid_mode=ON
    enforce-gtid-consistency=true
    log-slave-updates=1
    binlog_format= row
    skip-slave-start=1
    innodb_flush_log_at_trx_commit=2    
    sync_binlog=30  
    

    3)启动数据库

    /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysqldata/3306/my.cnf &
    

    4)执行一条数据

    insert into zstudent(stu_name,sex) values('hrd','M');
    commit;
    

    5)查看GTID的状态

    (root@localhost) [Ztest]> show master statusG;
        *************************** 1. row ***************************
        File: mysql-bin.000005
        Position: 1959
        Binlog_Do_DB: 
        Binlog_Ignore_DB: 
        Executed_Gtid_Set: 4160e9b3-58d9-11e8-b174-005056af6f24:1
        1 row in set (0.00 sec)
    

    备注:可以看到GTID集中分为了两段.
    其中4160e9b3-58d9-11e8-b174-005056af6f24就是server的uuid,1就是序列号。一直排序下去的。

    6)server的uuid的查询方式

        (root@localhost) [(none)]>  show GLOBAL VARIABLES like 'server_uuid';
        +---------------+--------------------------------------+
        | Variable_name | Value                                |
        +---------------+--------------------------------------+
        | server_uuid   | 4160e9b3-58d9-11e8-b174-005056af6f24 |
        +---------------+--------------------------------------+
        1 row in set (0.02 sec)
    

    7)开始继续插入数据

        insert into zstudent(stu_name,sex) values('hrd12','M');
        insert into zstudent(stu_name,sex) values('hrd13','M');
        insert into zstudent(stu_name,sex) values('hrd14','M');
        insert into zstudent(stu_name,sex) values('hrd15','M');
        insert into zstudent(stu_name,sex) values('hrd12','M');
        commmit;
    

    8)查看gtid_executed数据表

     (root@localhost) [(none)]> SELECT * FROM mysql.gtid_executed;
    +--------------------------------------+----------------+--------------+
    | source_uuid                          | interval_start | interval_end |
    +--------------------------------------+----------------+--------------+
    | 4160e9b3-58d9-11e8-b174-005056af6f24 |              1 |           11 |
    | 4160e9b3-58d9-11e8-b174-005056af6f24 |             12 |           12 |
    +--------------------------------------+----------------+--------------+
    2 rows in set (0.00 sec) 
    

    如上记录,可以看出它们并不是,马上回写入至gtid_executed数据表中。

    9)flush log之后,再次查看gtid_executed数据表

     (root@localhost) [(none)]> flush logs;
    Query OK, 0 rows affected (0.01 sec)
    
    (root@localhost) [(none)]> SELECT * FROM mysql.gtid_executed;
    +--------------------------------------+----------------+--------------+
    | source_uuid                          | interval_start | interval_end |
    +--------------------------------------+----------------+--------------+
    | 4160e9b3-58d9-11e8-b174-005056af6f24 |              1 |           19 |
    +--------------------------------------+----------------+--------------+
    1 row in set (0.00 sec) 
    

    备注:看到日志轮询之后,事务就会被写入至gtid_executed数据表中,而且会用到数据表的压缩技术,控制压缩的变量参数为:gtid_executed_compression_period ,默认值为1000。

    知识点小总结:由于是否开启了GTID,关键是上面提到的两个参数

    gtid_mode=ON
    enforce-gtid-consistency=true
    

    验证上述参数,在MYSQL服务中是否生效,用如下命令:

        (root@localhost) [(none)]> show variables like '%gtid%';
        +----------------------------------+-------------------------------------------+
        | Variable_name                    | Value                                     |
        +----------------------------------+-------------------------------------------+
        | binlog_gtid_simple_recovery      | ON                                        |
        | enforce_gtid_consistency         | ON                                        |
        | gtid_executed                    | 4160e9b3-58d9-11e8-b174-005056af6f24:1-19 |
        | gtid_executed_compression_period | 1000                                      |
        | gtid_mode                        | ON                                        |
        | gtid_next                        | AUTOMATIC                                 |
        | gtid_owned                       |                                           |
        | gtid_purged                      |                                           |
        | session_track_gtids              | OFF                                       |
        +----------------------------------+-------------------------------------------+
        9 rows in set (0.01 sec)
    

    至此,对于GTID一个简单的介绍和运用,

  • 相关阅读:
    apue-ubuntu环境搭建
    visualgdb 调试arm
    CMake速记
    umask
    转换函数conversion function
    c++ hex string array 转换 串口常用
    tcp与串口透传(select)
    sqlite3数据库修复SQLite-database disk image is malformed
    container_of宏
    shell 入门学习
  • 原文地址:https://www.cnblogs.com/zhangshengdong/p/11732376.html
Copyright © 2020-2023  润新知