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

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

Copy files to, from, or between different hosts using Secure Copy (scp)

Examples:

Copy the file “foobar.txt” from a remote host to the local host:

Copy the file “foobar.txt” from the local host to a remote host:

Copy the directory “foo” from the local host to a remote host’s directory “bar”:

Copy the file “foobar.txt” from remote host “rh1.edu” to remote host “rh2.edu”:

Copying the files “foo.txt” & “bar.txt” from the local host to your home directory on the remote host:

See more scp examples: