Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1951670
  • 博文数量: 176
  • 博客积分: 1857
  • 博客等级: 上尉
  • 技术积分: 2729
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-14 22:55
个人简介

吾生有涯,而知无涯,适当止学.循序渐进,步步提升 Talk is cheap, show me the code.

文章分类

全部博文(176)

文章存档

2019年(1)

2018年(14)

2017年(20)

2016年(31)

2015年(15)

2014年(5)

2013年(10)

2012年(80)

分类: Mysql/postgreSQL

2018-05-31 16:24:27

1.GTID的概念

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

1)GTID的格式为:
  1. 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
  1. CREATE TABLE gtid_executed (
  2.     source_uuid CHAR(36) NOT NULL,
  3.     interval_start BIGINT(20) NOT NULL,
  4.     interval_end BIGINT(20) NOT NULL,                                  
  5.     PRIMARY KEY (source_uuid, interval_start)
  6. )
复制代码
备注:事务并不是立马写进gtid_executed表。当启用二进制日志的时候(log-bin = /data/mysqldata/3306/binlog/mysql-bin),只有日志被轮询或者数据库服务被关闭的时候,才会把所有的日志写入到
gtid_executed数据表中。

3.实战例子:

1)关闭数据库:
  1. usr/local/mysql/bin/mysqladmin -uroot -p'zsd@7101'  shutdown
复制代码
2)修改my.cnf文件
  1. gtid_mode=ON
  2. enforce-gtid-consistency=true
  3. log-slave-updates=1
  4. binlog_format= row
  5. skip-slave-start=1
  6. innodb_flush_log_at_trx_commit=2    //这些参数的意思,如需知道,听下回分解。
  7. sync_binlog=30                                 
复制代码


3)启动数据库
  1. /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysqldata/3306/my.cnf &
复制代码


4)执行一条数据
  1. insert into zstudent(stu_name,sex) values('hrd','M');
  2. commit;
复制代码
5)查看GTID的状态
  1. (root@localhost) [Ztest]> show master status\G;
  2. *************************** 1. row ***************************
  3.              File: mysql-bin.000005
  4.          Position: 1959
  5.      Binlog_Do_DB: 
  6. Binlog_Ignore_DB: 
  7. Executed_Gtid_Set: 4160e9b3-58d9-11e8-b174-005056af6f24:1
  8. 1 row in set (0.00 sec)
复制代码
备注:可以看到GTID集中分为了两段一个是4160e9b3-58d9-11e8-b174-005056af6f24 ,另外一个是1
        其中4160e9b3-58d9-11e8-b174-005056af6f24就是server的uuid
               1就是序列号。一直排序下去的。
*server的uuid的查询方式
  1. (root@localhost) [(none)]>  show GLOBAL VARIABLES like 'server_uuid';
  2. +---------------+--------------------------------------+
  3. | Variable_name | Value                                |
  4. +---------------+--------------------------------------+
  5. | server_uuid   | 4160e9b3-58d9-11e8-b174-005056af6f24 |
  6. +---------------+--------------------------------------+
  7. 1 row in set (0.02 sec)
复制代码


6)开始继续插入数据
  1. insert into zstudent(stu_name,sex) values('hrd12','M');
  2. insert into zstudent(stu_name,sex) values('hrd13','M');
  3. insert into zstudent(stu_name,sex) values('hrd14','M');
  4. insert into zstudent(stu_name,sex) values('hrd15','M');
  5. insert into zstudent(stu_name,sex) values('hrd12','M');
  6. commmit;
复制代码


7)查看gtid_executed数据表

  1. (root@localhost) [(none)]> SELECT * FROM mysql.gtid_executed;
  2. +--------------------------------------+----------------+--------------+
  3. | source_uuid                          | interval_start | interval_end |
  4. +--------------------------------------+----------------+--------------+
  5. | 4160e9b3-58d9-11e8-b174-005056af6f24 |              1 |           11 |
  6. | 4160e9b3-58d9-11e8-b174-005056af6f24 |             12 |           12 |
  7. +--------------------------------------+----------------+--------------+
  8. 2 rows in set (0.00 sec)
复制代码
备注:这里可以看到他们并不是,马上回写入至gtid_executed数据表中。

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

  1. (root@localhost) [(none)]> flush logs;
  2. Query OK, 0 rows affected (0.01 sec)

  3. (root@localhost) [(none)]> SELECT * FROM mysql.gtid_executed;
  4. +--------------------------------------+----------------+--------------+
  5. | source_uuid                          | interval_start | interval_end |
  6. +--------------------------------------+----------------+--------------+
  7. | 4160e9b3-58d9-11e8-b174-005056af6f24 |              1 |           19 |
  8. +--------------------------------------+----------------+--------------+
  9. 1 row in set (0.00 sec)
复制代码
备注:看到只要日志轮询之后,事务就会被写入至gtid_executed数据表中,而且会数据表的压缩技术,控制压缩的变量参数为:[size=14.256px]gtid_executed_compression_period[size=14.256px] ,默认值为1000。
[size=14.256px]

知识点小总结:由于是否开启了GTID,关键是上面提到的两个参数
gtid_mode=ON
enforce-gtid-consistency=true
验证上述参数,在MYSQL服务中是否生效,用如下命令:
  1. (root@localhost) [(none)]> show variables like '%gtid%';
  2. +----------------------------------+-------------------------------------------+
  3. | Variable_name                    | Value                                     |
  4. +----------------------------------+-------------------------------------------+
  5. | binlog_gtid_simple_recovery      | ON                                        |
  6. | enforce_gtid_consistency         | ON                                        |
  7. | gtid_executed                    | 4160e9b3-58d9-11e8-b174-005056af6f24:1-19 |
  8. | gtid_executed_compression_period | 1000                                      |
  9. | gtid_mode                        | ON                                        |
  10. | gtid_next                        | AUTOMATIC                                 |
  11. | gtid_owned                       |                                           |
  12. | gtid_purged                      |                                           |
  13. | session_track_gtids              | OFF                                       |
  14. +----------------------------------+-------------------------------------------+
  15. 9 rows in set (0.01 sec)
复制代码


这里就算对于GTID一个简单的介绍和运用,下一篇帖子会讲一个基于GTID的replication。
                                                                              和innodb引擎关于innodb_flush_log_at_trx_commit和sync_binlog两个参数的内部机理的总结。be continue!!!
阅读(2409) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~