How do I import a MySQL database with SSH?

This article will show you how to import a MySQL database into your Virtual Private Server using SSH.

If you’d rather use an SSH connection to import an SQL backup file into your database (rather than phpMyAdmin), this can be done by following the steps outlined below:

Please note: the following article was originally created for our current generation of Virtual Private Servers. As such, these instructions may not be relevant to our previous generation of Virtual Private Servers.

Important: before you get started, you will need the following:

Step 1 of 9

Start by uploading your SQL backup file to your server via FTP or File Manager.

Step 2 of 9

Once you’ve done this, connect to your server via SSH.

Step 3 of 9

Enter the command ls, which will list all your server’s files and folders.

Step 4 of 9

Next, enter the command cd, followed by ../ to find the directory you uploaded the backup file in.

Step 5 of 9

Once you’ve found this directory, connect to MySQL by entering this command but DON’T press Enter on your keyboard:

mysql -h hostname -P port -u username -p database < backup.sql

Please note: if your host uses the value localhost, you can exclude -h hostname. Similarly, if you have a standard port value of 3306, you can exclude -P port.

Step 6 of 9

Next, you’ll need to change the following values:

  1. hostname: your hostname.
  2. port: your MySQL port number (3306 by default).
  3. username: the username for your database.
  4. database: the name of the database you wish to export.
  5. backup.sql: the name of your backup file.
Step 7 of 9

Once you’ve done this, press Enter on your keyboard.

Step 8 of 9

Next, enter the password for your database user into the provided field and then press enter on your keyboard.

Please note: this field may not always appear so please enter your password even if you don’t receive anything.

Step 9 of 9

After a few moments, your SQL backup file will be imported into your database.