mysqldump - Export structure only without autoincrement

MysqlExportMysqldumpDatabase SchemaAuto Increment

Mysql Problem Overview


I have a MySQL database and I am trying to find a way to export its structure only, without the auto increment values. mysqldump --no-data would almost do the job, but it keeps the auto_increment values. Is there any way to do it without using PHPMyAdmin (that I know it can do it)?

Mysql Solutions


Solution 1 - Mysql

You can do this :

mysqldump -u root -p -h <db-host> --opt <db-name> -d --single-transaction | sed 's/ AUTO_INCREMENT=[0-9]*\b//' > <filename>.sql

As mentioned by others, If you want sed to works properly, add the g (for global replacement) parameter like this :

mysqldump -u root -p -h <db-host> --opt <db-name> -d --single-transaction | sed 's/ AUTO_INCREMENT=[0-9]*\b//g' > <filename>.sql

(this only works if you have GUI Tools installed: mysqldump --skip-auto-increment)

New UPDATE thanks to comments.

The \b is useless and sometimes will break the command. See this SO topic for explanations. So the optimized answer would be :

mysqldump -u root -p -h <db-host> --opt <db-name> -d --single-transaction | sed 's/ AUTO_INCREMENT=[0-9]*//g' > <filename>.sql

Solution 2 - Mysql

JoDev's answer worked perfectly for me with a small adjustment to the sed regular expression:

mysqldump -d -h localhost -u<user> -p<password> <databaseName> | sed 's/ AUTO_INCREMENT=[0-9]*//g' > databaseStructure.sql

Solution 3 - Mysql

It is --create-options, which is included with --opt, by default, which generates the AUTO_INCREMENT table definitions.

If you only want the base tables,

mysql -hlocalhost -uuser -ppass --skip-column-names --batch \
	-e "select table_name from tables where table_type = 'BASE TABLE' and table_schema = 'schemaname'" INFORMATION_SCHEMA \
| xargs mysqldump -hlocalhost -uuser -ppass \
	--no-data --skip-triggers --skip-opt --no-create-db \
	schemaname

If you want views, triggers and routines too,

mysqldump -hlocalhost -uuser -ppass \
    --skip-opt --events --routines --no-data \
    schemaname

Solution 4 - Mysql

Thanks to this post, I was able to answer my question:

> How can I do version control on my db?

Then I just created this script: db_bkp.sh

#!/bin/sh
filename="db_structure.sql"
backupfolder="/var/www/"
fpath="$backupfolder/$filename"
usr="DBUSER"
pass="DBPASS"
db="DBNAME"
mysqldump --user=$usr --password=$pass --no-data $db | sed 's/ AUTO_INCREMENT=[0-9]*//g' > "$fpath"

Then I added this to crontab:

30 5 * * * sh /home/scripts/db_bkp.sh

Then in my repo I added the result, db_structure.sql to git and before pushing changes to prod I always check if there's any structural changes I forgot to do on all dbs.

Solution 5 - Mysql

mysqldump -u [USER] -p [PASSWORD] -d --skip-opt --single-transaction [DB_SCHEMA] > [FILE.ESTENSIONE]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionParisView Question on Stackoverflow
Solution 1 - MysqlJoDevView Answer on Stackoverflow
Solution 2 - MysqlJohnWView Answer on Stackoverflow
Solution 3 - MysqlErin SchoonoverView Answer on Stackoverflow
Solution 4 - MysqlGerardo RoscianoView Answer on Stackoverflow
Solution 5 - MysqlZio PanzuView Answer on Stackoverflow