auto_increment_increment 、auto_increment_offset

auto_increment_increment 、auto_increment_offset

看到这2个参数,就会想到双主,其实这2个参数会对表的主键自增值有影响。

auto_increment_increment 自增值 。

auto_increment_offset 初始值,决定了自增id的奇偶。要比上一个参数小,如果大,此值就被忽略,不起作用。

When the value of auto_increment_offset is greater than that of auto_increment_increment, the value ofauto_increment_offset is ignored.

 

线上一定不要修改这2个值,业务是长连接,session使用的老的参数值,新session都是用的修改后的参数值,这样业务会出现主键冲突。

故障没有模拟出来,基本上确定是bug,https://bugs.mysql.com/bug.php?id=67526 

 

innodb机制是将自增值放到内存的,没有持久化的。数据库重启,有可能导致自增值变小,导致主键冲突。

初始化自增值是用这个命令:SELECT MAX(ai_colFROM table_name FOR UPDATE;  数据库重启可能会导致自增值回溯。

 

innodb_autoinc_lock_mode=1,表示主键是连续的。

 

不要修改自增值,否则会导致主键冲突:

mysql> CREATE TABLE t1 -> c1 INT NOT NULL AUTO_INCREMENT-> PRIMARY KEY (c1-> ENGINE = InnoDB;

mysql> INSERT INTO t1 VALUES(0)(0)(3);

mysql> SELECT c1 FROM t1;

+----| c1 +----| 1 | 2 | 3 +----+

mysql> UPDATE t1 SET c1 WHERE c1 1;

mysql> SELECT c1 FROM t1;

+----| c1 +----| 2 | 3 | 4 +----+

session2:

mysql> INSERT INTO t1 VALUES(0);

ERROR 1062 (23000): Duplicate entry '4' for key 'PRIMARY'

 

last_insert_id() 会比 max(c1) 小1,插入肯定会报主键冲突。

 

mysql> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
| 7 |
+------------------+
1 row in set (0.00 sec)

mysql> select max(c1) from t1;
+---------+
| max(c1) |
+---------+
| 8 |
+---------+
1 row in set (0.00 sec)

posted on 2017-07-18 10:30  星期六男爵  阅读(311)  评论(0)    收藏  举报

导航