Transferring a database between MySQL servers can be done using SCP (Secure Copy), a method of copying files derived from the Bash Linux Shell. Keep in mind, you will need to know the passwords for both servers.

In order to migrate the MySQL Database, there are two steps you need to perform:

Step 1 — Perform a MySQL Dump

Before transferring the database file to the new Server, we need to back it up on the original server by using the mysqldump command. It will ask for your MySQL Root Password. You could also do this as a user of a specific database, if you have permissions… just change the -u root to -u YOURUSERNAME

cd ~
mysqldump -u root -p [database name] > [database name].sql

After the dump is performed, you are ready to copy the database backup

Step Two—Copy the Database

SCP helps you copy the database. If you used the previous command, you exported your database to your home folder.

The SCP command has the following syntax:

scp -P 22 [database name].sql [username]@[servername]:~/

For example:

scp -P 22 mydatabase.sql user@example.com:~/

After you connect, the database will be copied to the new server.

Step Three—Import the Database

Login to the other server where you copied the database to. Then import it using the following command. If you don’t have root access, use the user assigned to the new database. You might need to create the database first.

mysql -u root -p newdatabase < ~/mydatabase.sql

With that, you have successfully transferred the database.