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,上面操作会导致主从复制异常。

posted @ 2019-03-26 11:04  TeyGao  阅读(1078)  评论(0编辑  收藏  举报