MySQL :: MySQL 8.0 Reference Manual :: 19.5.1.1 Replication and AUTO_INCREMENT (original) (raw)

19.5.1.1 Replication and AUTO_INCREMENT

Statement-based replication ofAUTO_INCREMENT,LAST_INSERT_ID(), andTIMESTAMP values is carried out subject to the following exceptions:

CREATE TABLE t2 LIKE t1;  
ALTER TABLE t2 ADD id INT AUTO_INCREMENT PRIMARY KEY;  
INSERT INTO t2 SELECT * FROM t1 ORDER BY col1, col2;  

Important
To guarantee the same ordering on both source and replica, the ORDER BY clause must name_all_ columns of t1.
The instructions just given are subject to the limitations of CREATE TABLE ... LIKE: Foreign key definitions are ignored, as are the DATA DIRECTORY andINDEX DIRECTORY table options. If a table definition includes any of those characteristics, createt2 using a CREATE TABLE statement that is identical to the one used to create t1, but with the addition of the AUTO_INCREMENT column.
Regardless of the method used to create and populate the copy having the AUTO_INCREMENT column, the final step is to drop the original table and then rename the copy:

DROP t1;  
ALTER TABLE t2 RENAME t1;  

See also Section B.3.6.1, “Problems with ALTER TABLE”.