Last Updated: February 25, 2016
·
5.154K
· mchail

Copy mysql schema to new database

I recently wanted to test changing database character-encoding settings on a production RDS database, so I created a new local database mimicking the schema of my production database. The two-step process made it easy to tear down and recreate as I tuned my migration script.

All commands are executed on your local machine.

mysqldump -u my_username -pmy_password -h my_host -P my_port -d my_database > dbschema.sql
mysql -u root -e "create database encoding_test"
mysql -u root encoding_test < dbschema.sql

At this point, run any schema-modifying scripts you wish against your local database (encoding_test, in my case).

To start over:

mysql -u root -e "drop database encoding_test"
mysql -u root -e "create database encoding_test"
mysql -u root encoding_test < dbschema.sql

The -d in the mysqldump command is critical - it tells the process to exclude data and only dump the schema.