How to Clone a MySQL Database with a Different Name

Introduction

Cloning a MySQL database with a different name can be a crucial task, especially when you need to replicate an existing database’s structure and content for testing or development purposes. In this comprehensive guide (How to clone a MySQL Database with a Different Name), we will walk you through each step of creating a duplicate of your MySQL database, complete with tables, stored procedures, triggers, and more.

Cloning MySQL Database with a Different Name Step by Step guide:

Step 1: Prepare Your Source Database

Before you can embark on cloning your database, it’s crucial to ensure that your source database (Database1) exists and contains all the necessary data and structure you intend to replicate.

Step 2: Create the Target Database (Creating a New Database – Database2)

Within your MySQL server, manually create a new database (Database2). This new database will serve as the destination for the cloned data and structure.

Step 3: Copy Table Creation Statements (Replicating Table Structures)

For a seamless cloning process, right-click on each table in Database1 and copy the SQL “CREATE TABLE” statements. You can easily copy all the statements at once.

Copy statements of all table creation query together.

Step 4: Prepare SQL Query for Execution (Configuring SQL Queries)

To successfully execute the SQL queries and create the tables in Database2, it’s essential to prepare your script. Begin by inserting the following statements at the start of your script:

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!50503 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
SET @MYSQLDUMP_TEMP_LOG_BIN = @@SESSION.SQL_LOG_BIN;
SET @@SESSION.SQL_LOG_BIN= 0;

At the end of your SQL script, add the following statements to restore the previous settings and finalize the cloning process:

/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
SET @@SESSION.SQL_LOG_BIN = @MYSQLDUMP_TEMP_LOG_BIN;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

Step 5: Execute the SQL Queries (Executing SQL Queries)

Execute all the SQL queries together to create the tables in Database2. This step ensures that your tables are ready for data replication.

Step 6: Handle Stored Procedures, Triggers, and Functions (Managing Routines)

By default, MySQL does not clone stored procedures, triggers, and functions. To replicate these elements, execute the SQL script from your source database (Database1) that contains these routines. You can save this script as “database1_routines.sql” and execute all the statements together.

Step 7: Clone Data from Database1 to Database2 (Replicating Data)

To copy the data from Database1 to Database2, export Database1 with the “DumpDataOnly” option selected. Additionally, choose the “Create Dump in Single Transaction (self-contained file only)” checkbox for consistency.

Selection options of exporting data during cloning a MySQL Database with a Different Name

Step 8: Execute Data Import

Open the exported SQL file in MySQL Workbench or copy its contents into a new SQL query page. Execute all the “INSERT” statements together to populate Database2 with the data from Database1.

Conclusion

Following these steps will enable you to successfully clone a MySQL database with a different Name. This process encompasses the replication of structure, data, stored procedures, triggers, and functions. Whether you’re testing, developing, or creating backups, this method proves invaluable for managing your databases effectively.

By implementing these steps, you’ll have a cloned database (Database2) that mirrors the original (Database1), allowing you to work efficiently while preserving the integrity of your data. Happy cloning!

For more advance topic follow link: Advance Topics