zoukankan      html  css  js  c++  java
  • MySQL Replication--修改主键为NULL导致的异常

    测试环境:
    MySQL 5.5.14/MySQL 5.6.36

    测试脚本:

    create table tb001(id int primary key,c1 int);
    alter table tb001 modify id int null;
    show create table tb001;

    建表语句为:

    CREATE TABLE `tb001` (
      `id` int(11) NOT NULL DEFAULT '0',
      `c1` int(11) DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8

    上面ALTER命令想要将表主键ID列修改为null,执行完成未报错,但未修改成功。

    查看BINLOG日志发现:

    # at 696251545
    #190326 10:51:55 server id 2817457  end_log_pos 696251653 	Query	thread_id=9585927	exec_time=0	error_code=0
    SET TIMESTAMP=1553568715/*!*/;
    create table tb001(id int primary key,c1 int)
    /*!*/;
    # at 696251653
    #190326 10:52:00 server id 2817457  end_log_pos 696251752 	Query	thread_id=9585927	exec_time=0	error_code=0
    SET TIMESTAMP=1553568720/*!*/;
    alter table tb001 modify id int null
    /*!*/;

    发现该命令被记录到BINLOG日志中。

    上面代码在MySQL 5.7版本中执行,会报错:

    错误代码: 1171
    All parts of a PRIMARY KEY must be NOT NULL; if you need NULL in a key, use UNIQUE instead

    如果主库为MySQL 5.5或MySQL 5.6,而从库为MySQL 5.7,上面操作会导致主从复制异常。

  • 相关阅读:
    05 . Python入门值循环语句
    04 . kubernetes资源清单YAML入门
    04 . Python入门之条件语句
    03 . Python入门之运算符
    05 . k8s实战之部署PHP/JAVA网站
    02 . Python之数据类型
    01 . Python简介
    04 . Mysql主从复制和Mycat读写分离
    03 . MysSQL权限和备份
    02 . Mysql基础操作及增删改查
  • 原文地址:https://www.cnblogs.com/gaogao67/p/10598855.html
Copyright © 2011-2022 走看看