【问题】replace into 引发的从库auto_increment不自增问题

 mysql主从,主上的表有唯一索引,然后用replace into 对唯一索引列的值修改了,auto_increment是会增加的,但是从库并不会同步auto_increment值,有什么方法可以规避这个吗 。
 一旦涉及replace into,主从的表auto_increment就会不一样,这样后期如果主从切换就会有问题 
 

【分析】

 正常insert肯定是会变的,replace into并不是bug,如果值是不一样的那就和正常的insert是一样的,我说的场景是replace into的值是唯一索引列值一样的。
这种情况主库上是先delete后再insert,然后binlog中的表现形式是update的形式,所以主库的auto_increment会增加,但是从库只是update所以auto_increment不增加 
 

【对主从的影响】

主库的auto_increment会疯狂增长,而从库不动。
但主从切换之后,因为原主库现从库的auto_increment比较大,所以新主库对改变的插入在从库会疯狂报错主键重复....
  replace into 引发的从库auto_increment不自增问题
 

【解决办法】

建议:
if not exists (select 1 from t where id = 1)
  insert into t(id, update_time) values(1, getdate())
else
  update t set update_time = getdate() where id = 1;

 

 

 

 
摘录自群问题:mysql dba技术交流群

相关文章:

  • 2021-12-26
  • 2021-07-19
  • 2021-12-04
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 1970-01-01
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-01
  • 2021-09-02
  • 2021-11-13
  • 2021-10-30
相关资源
相似解决方案