1. 若 gtid_next=automatic, 代表使用默认值、Mysql会把server_uuid:gno 分配给它 a. 记录binlog时、先记录一行set @@session.gtid_mext = 'server_uuid.gno' b. 把这个gtid加入到本实例的gtid集合 2. 若gtid是固定的值、eg. 通过setgtid_next='current_gtid'指定、会有两种可能性: a. 若current_gtid已存在实例的gtid集合、接下来的事务会被系统忽略 b. 若不再、就将该值分配给要执行的事务、系统不需要生成、gno也不用+1
这样、MySQL维护了一个GTID集合、用来对应实例执行过的所有事务
当出现主键冲突时、可以通过GTID加入的方式、跳过事务
1 2 3 4 5
set gtid_next='conflict gtid'; begin; commit; // 通过提交一个空事务、将gtid加入到实例X的GTID集合 set gtid_next=automatic; // 恢复GTID自动分配模式 startslave; // 启动从库
2019-05-2121:46:447f9ac2fff700 *** (1) TRANSACTION: TRANSACTION 14234356386, ACTIVE 0 sec fetching rows mysql tables in use 1, locked 1 LOCK WAIT 4 lock struct(s), heap size 1184, 2 row lock(s) MySQL thread id 101287388, OS thread handle 0x7f9ac472c700, query id 1591383055210.20.00.217 zop Searching rows for update update driver_statistics set statistics_status=0 , statistics_time=NOW() where statistics_status=1and statistics_time <= DATE_SUB(NOW(),INTERVA L 120 SECOND) *** (1) WAITING FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 65 page no 203 n bits 208 index `PRIMARY` of table `platform`.`driver_statistics` trx id 14234356386 lock_mode X loc ks rec but not gap waiting *** (2) TRANSACTION: TRANSACTION 14234356375, ACTIVE 0 sec updating or deleting, thread declared inside InnoDB 0 mysql tables in use 1, locked 1 3 lock struct(s), heap size 1184, 2 row lock(s), undo log entries 1 MySQL thread id 101570708, OS thread handle 0x7f9ac2fff700, query id 1591383053910.20.00.217 zop updating update driver_statistics SET status = 2,time = '2019-05-21 21:46:44.96',d_id = 44201, d_phone = '15601237231', d_name = 'k' where id = 54145 *** (2) HOLDS THE LOCK(S): RECORD LOCKS space id 65 page no 203 n bits 208 index `PRIMARY` of table `platform`.`driver_statistics` trx id 14234356375 lock_mode X loc ks rec but not gap *** (2) WAITING FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 65 page no 7749 n bits 1616 index `idx_statistics_status` of table `platform`.`driver_statistics` trx id 14234356375 lock_mode X locks rec but not gap waiting *** WE ROLL BACK TRANSACTION (2) ------------
先看事务一的信息
***(1) TRANSACTION: TRANSACTION 14234356386, ACTIVE 0 sec fetching rows
active 0 sec 表示事务活跃时间 fetching rows 表示事务正在做的事儿
1
可能的事务有 inserting、updating、deleting、fetching rows等
mysql tables in use 1, locked 1 LOCK WAIT 4 lock struct(s), heap size 1184, 2 row lock(s)
mysql tables in use 1 表示mysql有一个表被使用 locked 1 表示有一个表锁 LOCK WAIT 表示事务正在等待锁 4 lock struct(s) 表示该事务的锁链表长度为 4, 表示该事务的锁链表的长度为 11,每个链表节点代表该事务持有的一个锁结构,包括表锁,记录锁以及 autoinc 锁等。 heap size 1184 为事务分配的锁堆内存大小 2 row lock(s) 表示当前事务持有的行锁个数, 通过遍历上边的4个锁结构、找到其中类型为LOCK_REC的记录数、
MySQL thread id 101287388, OS thread handle 0x7f9ac472c700, query id 15913830552 10.20.00.217 zop Searching rows for update
MySQL thread id .. OS... 当前事务线程信息 10.20.00.217 zop 数据库的ip和dbname Searching rows for update 当前事务的状态
update d_statistics set statistics_status=0, statistics_time=now() where statistics_status=1 and statistics_time <=date_sub(now(),interval 120 seconds)
当前正在执行的sql语句
*** (1) WAITING FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 65 page no 203 n bits 208 index PRIMARY of table platform.driver_statistics trx id 14234356386 lock_mode X loc ks rec but not gap waiting
lock_mode X 表示改记录锁为排他锁 RECORD LOCKS 表示正在等待的是记录锁 index PRIMARY 表示要加锁的索引为主键索引 n bits 208 表示这个记录锁结构上保留有208个bit位(改page上的记录数+64)、 lock_mode X表示该记录锁为排他锁、 locks rec but not gap waiting表示要加的锁为记录锁、并且处于锁等待状态
(2) HOLDS THE LOCK(S): RECORD LOCKS space id 65 page no 203 n bits 208 index `PRIMARY` of table `platform`.`driver_statistics` trx id 14234356375 lock_mode X loc ks rec but not gap
表示事务2 持有主键索引的X记录锁、
1 2 3
(2) WAITING FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 65 page no 7749 n bits 1616 index `idx_statistics_status` of table `platform`.`driver_statistics` trx id 14234356375 lock_mode X locks rec but not gap waiting
假如磁盘空间是1.2T、现有一个1T的表、可否完成一个inpalce的DDL? 不能、inplace也是需要临时空间的、altertable t engine=innodb,ALGORITHM=inplace; copy表其实是: altertable t engine=innodb,ALGORITHM=copy; 对应图三