• ADADADADAD

    再来理解一下杀手级新特性:gtid[ mysql数据库 ]

    mysql数据库 时间:2024-12-03 12:12:12

    作者:文/会员上传

    简介:

    1.一个事务,就会给一个gtid编号。来看看例子:mysql>showmasterstatus;+---------------+----------+--------------+------------------+-----------------------------------

    以下为本文的正文内容,内容仅供参考!本站为公益性网站,复制本文以及下载DOC文档全部免费。

    1.一个事务,就会给一个gtid编号。来看看例子:

    mysql>showmasterstatus;+---------------+----------+--------------+------------------+------------------------------------------------+|File|Position|Binlog_Do_DB|Binlog_Ignore_DB|Executed_Gtid_Set|+---------------+----------+--------------+------------------+------------------------------------------------+|binlog.000017|58105|||b526042e-89a7-11e8-bddb-000c29d7d637:1-1304086|+---------------+----------+--------------+------------------+------------------------------------------------+1rowinset(0.00sec)mysql>insertintostudent(id)values(5);QueryOK,1rowaffected(0.01sec)mysql>showmasterstatus;+---------------+----------+--------------+------------------+------------------------------------------------+|File|Position|Binlog_Do_DB|Binlog_Ignore_DB|Executed_Gtid_Set|+---------------+----------+--------------+------------------+------------------------------------------------+|binlog.000017|58432|||b526042e-89a7-11e8-bddb-000c29d7d637:1-1304087|+---------------+----------+--------------+------------------+------------------------------------------------+1rowinset(0.00sec)mysql>begin->;QueryOK,0rowsaffected(0.00sec)mysql>insertintostudent(id)values(6);QueryOK,1rowaffected(0.00sec)mysql>insertintostudent(id)values(7);QueryOK,1rowaffected(0.00sec)mysql>commit;QueryOK,0rowsaffected(0.01sec)mysql>showmasterstatus;+---------------+----------+--------------+------------------+------------------------------------------------+|File|Position|Binlog_Do_DB|Binlog_Ignore_DB|Executed_Gtid_Set|+---------------+----------+--------------+------------------+------------------------------------------------+|binlog.000017|58918|||b526042e-89a7-11e8-bddb-000c29d7d637:1-1304088|+---------------+----------+--------------+------------------+------------------------------------------------+1rowinset(0.00sec)mysql>begin;QueryOK,0rowsaffected(0.00sec)mysql>insertintostudent(id)values(8);QueryOK,1rowaffected(0.00sec)mysql>rollback;QueryOK,0rowsaffected(0.00sec)mysql>showmasterstatus;+---------------+----------+--------------+------------------+------------------------------------------------+|File|Position|Binlog_Do_DB|Binlog_Ignore_DB|Executed_Gtid_Set|+---------------+----------+--------------+------------------+------------------------------------------------+|binlog.000017|58918|||b526042e-89a7-11e8-bddb-000c29d7d637:1-1304088|+---------------+----------+--------------+------------------+------------------------------------------------+1rowinset(0.00sec)mysql>quitBye[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'/*!*/;QueryOK,0rowsaffected(0.00sec)mysql>select*fromstudentwhereid=4;+----+------+------+---------+---------+---------------------+---------------------+|id|name|age|textcol|blobcol|birday|birday2|+----+------+------+---------+---------+---------------------+---------------------+|4|3|3|3|NULL|2018-07-2510:42:29|2018-07-2510:42:33|+----+------+------+---------+---------+---------------------+---------------------+1rowinset(0.00sec)mysql>CREATETABLE`student`(->`id`intNOTNULLAUTO_INCREMENT,->`name`varchar(255)NULL,->`age`intNULL,->`textcol`textNULL,->`blobcol`blobNULL,->`birday`timestampNULLONUPDATECURRENT_TIMESTAMP,->`birday2`datetimeNULLONUPDATECURRENT_TIMESTAMP,->PRIMARYKEY(`id`)->)->;QueryOK,0rowsaffected(0.00sec)mysql>showmasterstatus;+---------------+----------+--------------+------------------+------------------------------------------------+|File|Position|Binlog_Do_DB|Binlog_Ignore_DB|Executed_Gtid_Set|+---------------+----------+--------------+------------------+------------------------------------------------+|binlog.000017|58105|||b526042e-89a7-11e8-bddb-000c29d7d637:1-1304086|+---------------+----------+--------------+------------------+------------------------------------------------+1rowinset(0.00sec)mysql>insertintostudnet(id)values(5);ERROR1837(HY000):When@@SESSION.GTID_NEXTissettoaGTID,youmustexplicitlysetittoadifferentvalueafteraCOMMITorROLLBACK.PleasecheckGTID_NEXTvariablemanualpagefordetailedexplanation.Current@@SESSION.GTID_NEXTis'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。

    为什么会这样呢?

    思考一下。。。。。。

    再来理解一下杀手级新特性:gtid.docx

    将本文的Word文档下载到电脑

    推荐度:

    下载
    热门标签: gtid一下理解