How to migrate a MySQL database from one Linux server to another using the command the line

How to move/migrate a MySQL database from one Linux server to another via the command line using “mysqldump” to export the database , “scp” (secure copy) to transfer it, and “mysql” to import it.

In this example, we’ll migrate/move a theoretical SQL database named twatdb from it’s current location on example.com over to the twat.ca server.

1. Export the database from the old server using “mysqldump”

For our example, let’s say our database is named “junglist”. Before transferring the database file to the new VPS, we first need to create a database dump on the original server using mysqldump:

mysqldump -u root -p –opt [database name] > [database file].sql

2. Transfer the .sql database file to the new server

While there’s many different ways we could move our .sql file over to the new server (SFTP, rsync, etc.) we’re going to use “scp” (secure copy) in this example.

Syntax: scp [database name].sql [username]@[servername]:path/to/database/

Using scp, we’ll put the file in our home folder on the new server:

3. Import the database on the new server

Log in to your new server via SSH and import using mysql:

mysql -u root -p [database name] < [database file].sql