防止 MYSQL 在服务器重启时重置自动增量值

问题描述:

我们遇到了情况.我们使用自动增量存储 id 并删除行以保持表清洁.我们不希望 MYSQL 在意外重启时重置自动增量.虽然在删除 MYSQL 时不会重置 auto inc 计数器,我们对此感到满意,但偶尔 MYSQL 会重新启动,并且该计数器会重置为 max(id) +1.我们想避免这种情况,有没有办法告诉 MYSQL 将自动递增计数器保持在重启前的位置.

We have a situation. We store id with auto increment and delete the rows to keep table clean. We do not want MYSQL to reset the auto increment when it gets rebooted accidentally. Though upon deleting MYSQL do not reset the auto inc counter and we are happy with that but occasionally MYSQL restart and that counter is reset to max(id) +1. We want to avoid that, is there a way we can tell MYSQL to keep the auto increment counter to where it was before the restart.

It's know bug: https://bugs.mysql.com/bug.php?id=199.该错误尚未解决.

It's know bug: https://bugs.mysql.com/bug.php?id=199. The bug is not solved yet.

最好的解决方法可能是使用序列:https://openquery.com.au/blog/implementing-sequences-using-a-stored-function-and-triggers

The best workaround probably is to use sequences: https://openquery.com.au/blog/implementing-sequences-using-a-stored-function-and-triggers