Copying a MySQL database to a new database locally
Friday, August 31st, 2007Today I needed to rename a database in MySQL. I’m not sure if it’s possible but I decided to create a copy of the database instead with a new name and then drop the old one. This is really easily done using the following commands:
mysqladmin -h <hostname> create <new_dbname>
mysqldump --opt -h <hostname> <old_dbname> | mysql -h <hostname> <new_dbname>
The first command creates a new database on <host> called <new_dbname> and the second dumps <old_dbname> from <hostname> and sends the output directly into the third command, which simply executes the SQL statements into <new_dbname>.
Note that other parameters such as -u and -p are needed when security is enabled, and that the -h parameter can be excluded if both target and destination are the local machine.
The official documentation for mysqldump can be found here.