There are many use cases when you must migrate MySQL database between 2 servers, like cloning a database for testing, a separate database for running reports, or completely migrating a database system to a new server. Broadly, you will take a data backup on the first server, transfer it remotely to the destination server, and finally restore the backup on the new MySQL instance.

This article will walk you through the steps to migrate MySQL Database between 2 Servers using 3 simple steps. Additionally, we will explore the process of performing a MySQL migration, using copy MySQL database from one server to another operation. This process is crucial when you want to move your MySQL database to another server without losing any data or functionality.

We will cover the necessary steps and considerations involved in successfully completing a MySQL migration. So, whether you are looking to clone a database, create a separate database for reporting purposes, or completely migrate your database to a new server, this guide will provide you with the information you need.

Steps to Migrate MySQL Database Between 2 Servers

Migrate MySQL Database to Another Database: Migrate MySQL Database
Image Source

Let’s understand the steps to migrate the MySQL database between 2 servers. Understanding the process of transferring MySQL databases from one server to another is crucial for maintaining data integrity and continuity of services. To migrate MySQL database seamlessly, ensure both source and target servers are compatible

Below are the steps you can follow to understand how to migrate MySQL database between 2 servers:

Migrate MySQL Database with Hevo’s no-code Data Pipeline seamlessly

Hevo is the only real-time ELT No-code Data Pipeline platform that cost-effectively automates data pipelines that are flexible to your needs. With integration with 150+ Data Sources (40+ free sources), we help you not only export data from sources & load data to the destinations but also transform & enrich your data, & make it analysis-ready.

Start for free now!

Get Started with Hevo for Free

1) Backup the Data

The first step to migrate MySQL database is to take a dump of the data that you want to transfer. This operation will help you move mysql database to another server. To do that, you will have to use mysqldump command. The basic syntax of the command is:

mysqldump -u [username] -p [database] > dump.sql

If the database is on a remote server, either log in to that system using ssh or use -h and -P options to provide host and port respectively.

mysqldump -P [port] -h [host] -u [username] -p [database] > dump.sql

There are various options available for this command, let’s go through the major ones as per the use case.

A) Backing Up Specific Databases

mysqldump -u [username] -p [database] > dump.sql

This command dumps specified databases to the file.

You can specify multiple databases for the dump using the following command:

mysqldump -u [username] -p --databases [database1] [database2] > dump.sql

You can use the –all-databases option to backup all databases on the MySQL instance.

mysqldump -u [username] -p --all-databases > dump.sql

B) Backing Up Specific Tables

The above commands dump all the tables in the specified database, if you need to take backup of some specific tables, you can use the following command:

mysqldump -u [username] -p [database] [table1] [table2] > dump.sql

C) Custom Query

If you want to backup data using some custom query, you will need to use the where option provided by mysqldump.

mysqldump -u [username] -p [database] [table1] --where="WHERE CLAUSE" > dump.sql

Example: mysqldump -u root -p testdb table1 --where="mycolumn = myvalue" > dump.sql

Note:

  • By default, mysqldump command includes DROP TABLE and CREATE TABLE statements in the created dump. Hence, if you are using incremental backups or you specifically want to restore data without deleting previous data, make sure you use the –no-create-info option while creating a dump.
mysqldump -u [username] -p [database] --no-create-info > dump.sql
  • If you need just to copy the schema but not the data, you can use –no-data option while creating the dump.
mysqldump -u [username] -p [database] --no-data > dump.sql

Other use cases 

Here’s a list of uses for the mysqldump command based on use cases:

  • To backup a single database:
mysqldump -u [username] -p [database] > dump.sql
  • To backup multiple databases:
mysqldump -u [username] -p --databases [database1] [database2] > dump.sql
  • To backup all databases on the instance:
mysqldump -u [username] -p --all-databases > dump.sql
  • To backup specific tables:
mysqldump -u [username] -p [database] [table1] [table2] > dump.sql
  • To backup data using some custom query:
mysqldump -u [username] -p [database] [table1] --where="WHERE CLAUSE" > dump.sql

Example: 

mysqldump -u root -p testdb table1 --where="mycolumn = myvalue" > dump.sql
  • To copy only the schema but not the data:
mysqldump -u [username] -p [database] --no-data > dump.sq
  • To restore data without deleting previous data (incremental backups):
mysqldump -u [username] -p [database] --no-create-info > dump.sql

2) Copy the Database Dump on the Destination Server

Once you have created the dump as per your specification, the next step to migrate MySQL database is to use the data dump file to move the MySQL database to another server (destination). You will have to use the “scp” command for that.

Scp -P [port] [dump_file].sql [username]@[servername]:[path on destination]

Examples:

scp dump.sql root@130.243.18.62:/var/data/mysql
scp -P 3306 dump.sql root@130.243.18.62:/var/data/mysql
  • To copy to a single database, use this syntax: 
scp all_databases.sql user@example.com:~/
  • For a single database:
scp database_name.sql user@example.com:~/

Here’s an example:

scp dump.sql root@130.243.18.62:/var/data/mysql scp -P 3306 dump.sql root@130.243.18

3) Restore the Dump

The last step in MySQL migration is restoring the data on the destination server. MySQL command directly provides a way to restore to dump data to MySQL.

mysql -u [username] -p [database] < [dump_file].sql

Example:

mysql -u root -p testdb < dump.sql

Don’t specify the database in the above command if your dump includes multiple databases.

mysql -u root -p < dump.sql
  • For all databases:
mysql -u [user] -p --all-databases < all_databases.sql
  • For a single database:
mysql -u [user] -p newdatabase < database_name.sql
  • For multiple databases:
mysql -u root -p < dump.sql

Limitations with Dumping and Importing MySQL Data

Dumping and importing MySQL data can present several challenges:

  1. Time Consumption: The process can be time-consuming, particularly for large databases, due to creating, transferring, and importing dump files, which may slow down with network speed and database size.
  2. Potential for Errors: Human error is a significant risk, including overlooking steps, misconfiguring settings, or using incorrect parameters with the mysqldump command.
  3. Data Integrity Issues: Activities on the source database during the dump process can lead to data inconsistencies in the exported SQL dump. Measures like putting the database in read-only mode or locking tables can mitigate this but may impact application availability.
  4. Memory Limitations: Importing massive SQL dump files may encounter memory constraints, necessitating adjustments to MySQL server configurations on the destination machine.

Conclusion

Following the above-mentioned steps, you can migrate MySQL database between two servers easily, but to migrate MySQL database to another server can be quite cumbersome activity especially if it’s repetitive. An all-in-one solution like Hevo takes care of this effortlessly and helps manage all your data pipelines in an elegant and fault-tolerant manner.

Hevo will automatically catalog all your table schemas and do all the necessary transformations to copy MySQL database from one server to another. Hevo will fetch the data from your source MySQL server incrementally and restore that seamlessly onto the destination MySQL instance. Hevo will also alert you through email and Slack if there are schema changes or network failures. All of this can be achieved from the Hevo UI, with no need to manage servers or cron jobs.

VISIT OUR WEBSITE TO EXPLORE HEVO

Want to take Hevo for a spin?

Sign Up for a 14-day free trial and experience the feature-rich Hevo suite firsthand. You can also have a look at the unbeatable Hevo pricing that will help you choose the right plan for your business needs.

Share your experience of learning about the steps to migrate MySQL database between 2 servers in the comments section below.

Rachit
Freelance Technical Content Writer, Hevo Data

Rachit enjoys writing aobut data science, software architecture, and all technical topics related to this. He passionately creates content aimed at aiding data teams in solving complex business problems.

No-code Data Pipeline for your Data Warehouse

Get Started with Hevo