MySQL 5.0 Reference Manual  /  ...  /  Replication and AUTO_INCREMENT Replication and AUTO_INCREMENT

Replication of AUTO_INCREMENT, LAST_INSERT_ID(), and TIMESTAMP values is done correctly, subject to the following exceptions.

  • AUTO_INCREMENT columns in tables on the slave must match the same columns on the master; that is, AUTO_INCREMENT columns must be replicated to AUTO_INCREMENT columns.

    This is a known issue which is fixed in MySQL 5.5. (Bug #12669186)

  • INSERT DELAYED ... VALUES(LAST_INSERT_ID()) inserts a different value on the master and the slave. (Bug #20819) This is fixed in MySQL 5.1 when using row-based or mixed-format binary logging. For more information, see Replication Formats.

  • Before MySQL 5.0.26, a stored procedure that uses LAST_INSERT_ID() does not replicate properly.

  • When a statement uses a stored function that inserts into an AUTO_INCREMENT column, the generated AUTO_INCREMENT value is not written into the binary log, so a different value can in some cases be inserted on the slave. This is also true of a trigger that causes an INSERT into an AUTO_INCREMENT column.

  • An insert into an AUTO_INCREMENT column caused by a stored routine or trigger running on a master that uses MySQL 5.0.60 or earlier does not replicate correctly to a slave running MySQL 5.1.12 through 5.1.23 (inclusive). (Bug #33029)

  • An INSERT into a table that has a composite primary key that includes an AUTO_INCREMENT column that is not the first column of this composite key is not logged or replicated correctly.

    This issue does not affect tables using the InnoDB storage engine, since InnoDB does not allow the creation of a composite key that includes an AUTO_INCREMENT column that is not the first column in the key.

  • Adding an AUTO_INCREMENT column to a table with ALTER TABLE might not produce the same ordering of the rows on the slave and the master. This occurs because the order in which the rows are numbered depends on the specific storage engine used for the table and the order in which the rows were inserted. If it is important to have the same order on the master and slave, the rows must be ordered before assigning an AUTO_INCREMENT number. Assuming that you want to add an AUTO_INCREMENT column to a table t1 that has columns col1 and col2, the following statements produce a new table t2 identical to t1 but with an AUTO_INCREMENT column:

    INSERT INTO t2 SELECT * FROM t1 ORDER BY col1, col2;

    To guarantee the same ordering on both master and slave, 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 and INDEX DIRECTORY table options. If a table definition includes any of those characteristics, create t2 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;

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

Download this Manual
User Comments
Sign Up Login You must be logged in to post a comment.