Whenever you dump a mysql table that has an AUTO_INCREMENT column, the next value is always attached to the definition of the table. You should see something like:
) ENGINE=InnoDB AUTO_INCREMENT=<some-number> ...
You may want to consider one of three(3) things
SUGGESTION #1 : mysqldump the database with table structure
imac2011:Desktop allendar$ /Applications/MAMP/Library/bin/mysqldump -u root -p my_database > my_database_data.sql
That way, the auto_increment is attached to the table definition
SUGGESTION #2 : mysqldump the data and table structure separately
imac2011:Desktop allendar$ /Applications/MAMP/Library/bin/mysqldump -u root -p my_database --no-create-info > my_database_data.sql
imac2011:Desktop allendar$ /Applications/MAMP/Library/bin/mysqldump -u root -p my_database --no-data > my_database_schema.sql
You would load my_database_schema.sql
first, then my_database_data.sql
SUGGESTION #3 : Read the next auto_increment of every table into a script
You can use a query like this to make the SQL script:
SELECT CONCAT('TRUNCATE TABLE ',db,'.',tb,'; ALTER TABLE ',db,'.',tb,' AUTO_INCREMENT=',autoinc,';')
FROM (SELECT table_schema db,table_name tb ,auto_increment autoinc
FROM information_schema.tables WHERE table_schema='my_database') A;
In the OS, you can do it like this:
SQLSTMT="SELECT CONCAT('TRUNCATE TABLE ',db,'.',tb,'; ALTER TABLE ',db,'.',tb,'AUTO_INCREMENT=',autoinc,';')"
SQLSTMT="${SQLSTMT} FROM (SELECT table_schema db,table_name tb ,auto_increment autoinc"
SQLSTMT="${SQLSTMT} FROM information_schema.tables WHERE table_schema='my_database') A;"
mysql -uroot -p -Ane"${SQLSTMT}" > autoinc.sql
Simply run autoinc.sql
before my_database_data.sql
Give it a Try !!!
I think that Percona's pt-online-schema-change might be a better solution for you as it can do an ALTER with very little downtime. It does, however, require adding a TRIGGER to the table.
Runnig DDL on your table will lead you into rebuilding your table, mysql is a row based engine so you will suffer the index rebuild this can be quite expensive depending on your engine, table complexity is factor as well.
Best Answer
You could probably use the online schema change feature of ALTER TABLE in MySQL 5.6
Please note the MySQL 5.6 Documentation for
ALTER TABLE
You would run the
ALTER TABLE
like this:What this will do is create a shadow copy of the table being changed. All reads and writes can go on with the original table. Additional changes made to the table is placed in the shadow copy. Everything is merged and presented at the end.
CAVEAT #1
The online schema change feature uses the folder mapped in tmpdir. Make sure the copy of the table can fit entirely in tmpdir. For example, if tmpdir is mapped to
/tmp
and the table you are changing is 7GB, there has to be more than 7GB free in/tmp
. If/tmp
is not large enough, please consider mounting/tmp
on a large volume.CAVEAT #2
Again, from the MySQL 5.6 Documentation for
ALTER TABLE
Changing Columns with older the DATETIME format will not work. You would have to use other means to make the new table.