Clone or copy a mysql database

Let’s go straight into the subject matter. Here are the steps to create a copy of a existing database.

1. Open mysql CLI
$ mysql -u root -p

2. List your databases 
$ show databases;

3. From the output list, copy the name of your database - The one we'll be cloning. 
exit the CLI (ctrl + C) or type $ exit

4. Create a backup dir 
$ mkdir -p ~/db_bak/

4. Export the database
mysqldump -u root -p YOUR_DATABASE_NAME > ~/db_bak/your_database_name.sql

5. Open mysql CLI again
mysql -u root -p

6. Create your new database
$ CREATE DATABASE YOUR_DESIRED_DB_NAME CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;


Note: For users mysql 8 users, notice that 'utf8mb4_0900_ai_ci' is now the new default.  


7. Import your data
mysqldump -u root -p YOUR_DESTINATION_DATABASE < ~/db_bak/your_database_name.sql


All set! 
Your database is ready! :-)

 

Leave a Reply

Close Menu