暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

CREATE TABLE table0_copy AS SELECT * FROM table0报错原因分析

新架构思考 2021-01-14
1102

1、数据库执行语句CREATE TABLE table0_copy as SELECT * FROM table0报错,错误日志:

    16:54:17 CREATE TABLE table0_copy as SELECT * FROM table0 Error Code: 1786. CREATE TABLE ... SELECT is forbidden when @@GLOBAL.ENFORCE_GTID_CONSISTENCY = 1




    2、错误原因

    查看数据库版本:

      select version();  

      结果为:'5.6.36-log'


      查看gtid是否开启:

        show global variables like '%gtid%'

        结果为:'enforce_gtid_consistency', 'ON'


        原因:

        在5.6及以上的版本内,开启了 enforce_gtid_consistency=true 功能导致的,MySQL官方解释说当启用 enforce_gtid_consistency 功能的时候,

        MySQL只允许能够保障事务安全,并且能够被日志记录的SQL语句才被执行,像create table … select 和 DROP TEMPORARY TABLE语句,

        以及同时更新事务表和非事务表的SQL语句或事务都不允许执行。


        3、解决方法

        方法一:

        修改 :

          SET @@GLOBAL.ENFORCE_GTID_CONSISTENCY = off;

          方法二:

          CREATE TABLE table0_copy as SELECT * FROM table0的方式会拆分成两部分:
            create table table0_copy like table0;
            insert into table0_copy select *from table0;



            4、mysql官方文档说明:

            --enforce-gtid-consistency only takes effect if binary logging takes place for a statement. If binary logging is disabled on the server, or if statements are not written to the binary log because they are removed by a filter, GTID consistency is not checked or enforced for the statements that are not logged.


            Only statements that can be logged using GTID safe statements can be logged when enforce_gtid_consistency is set to ON, so the operations listed here cannot be used with this option:


            CREATE TEMPORARY TABLE or DROP TEMPORARY TABLE statements inside transactions.


            Transactions or statements that update both transactional and nontransactional tables. There is an exception that nontransactional DML is allowed in the same transaction or in the same statement as transactional DML, if all nontransactional tables are temporary.


            CREATE TABLE ... SELECT statements, prior to MySQL 8.0.21. From MySQL 8.0.21, CREATE TABLE ... SELECT statements are allowed for storage engines that support atomic DDL.



            5、原文链接

            https://dev.mysql.com/doc/refman/8.0/en/replication-options-gtids.html

            文章转载自新架构思考,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

            评论