Duplicate a MySQL database on a single line

On this article, it is shown a very convenient way to duplicate a complete MySQL database using a single line of code:

$ mysqladmin create {dest_db_name} -u {username} --password={password} && \
mysqldump -u {username} --password={password} {source_db_name} | \
mysql -u {username} --password={password} {dest_db_name}

Where:

  • {dest_db_name}: Name of the destination database.
  • {source_db_name}: Name of the database to be duplicated.
  • {username}: MySQL username.
  • {password}: MySQL password.

If we need to duplicate it to another server, we would do something like this:

$ mysqladmin create {dest_db_name} -u {dest_username} --password={dest_password} -h {dest_server} && \
mysqldump -u {source_username} --password={source_password} -h {source_server} {source_db_name} | \
mysql -u {dest_username} --password={dest_password} -h {dest_server} {dest_db_name}

Where:

  • {source_db_name}: Name of the source database.
  • {source_server}: Source MySQL server.
  • {source_username}: Source MySQL username.
  • {source_password}: Source MySQL password.
  • {dest_db_name}: Name of the destination database.
  • {dest_server}: Destination MySQL server.
  • {dest_username}: Destination MySQL username.
  • {dest_password}: Destination MySQL password.

2 thoughts on “Duplicate a MySQL database on a single line”

  1. @Andres:

    You could run it from the command prompt on the DB server itself. On linux it will work just that way, but on windows you would need to make a small change (remove “&& \” and make a one line command).

    Another option is to run it from another machine where you have MySQL installed (ie, your development PC). Note that the DB server will need to allow remote access.

Leave a Reply

Your email address will not be published. Required fields are marked *

Are you human? *

This site uses Akismet to reduce spam. Learn how your comment data is processed.