How to duplicate a MySQL database on the same server
Michaels answer abowe works well but does not copy events, stored procedures or triggers.
To copy those a few more switches is needed for mysqldump:
--events --triggers --routines
To complement an already made copy:
mysqldump --user=username --password=passwd --no-data --no-create-info --no-create-db --events --triggers --routines live_db | mysql --user=username --password=passwd test_db
The mysql
command line client will accept a stream of SQL statements from standard input. You can therefore pipe the output of mysqldump
directly into mysql
on the command line. Doing this as a cron job will regularly overwrite your test data with updated live data:
mysql --user=username --password=passwd -e 'DROP DATABASE test_db;'
mysql --user=username --password=passwd -e 'CREATE DATABASE test_db;'
mysqldump --user=username --password=passwd live_db | mysql --user=username --password=passwd test_db
Note that since your data is large, it will take a long time.