Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

How to import and export a MySQL database with SSH

This guide shows you how to save a database to a file, and load a file back into a database, using commands.

Databases2 min read8 steps

What is SSH?

SSH is a safe way to type commands on a remote server. You open it with a program such as Terminal on Mac and Linux, or PowerShell or PuTTY on Windows. Ask Hostvento support whether SSH is on for your plan.

A database holds your site's data. Exporting means saving it into a file. Importing means loading a file back in. The file usually ends in .sql.

Export a database

  1. Log in to your server with SSH.
  2. Run this command. Replace the names with yours:
    mysqldump -u username -p dbname > backup.sql
    This copies the database named dbname into a file called backup.sql in the current folder.
  3. Type your database password when asked. Nothing shows as you type. That is normal.
  4. Check the file exists with ls -lh backup.sql. It shows the file and its size.

To make a smaller file, compress it while exporting:

mysqldump -u username -p dbname | gzip > backup.sql.gz

Import a database

First upload your .sql file to the server. You can use File Manager in cPanel or an FTP program. FTP is a way of sending files to a server.

The target database must already exist. If not, create it first in cPanel under MySQL Databases.

Warning: An import can overwrite tables with the same names. Take a backup of the target database first.

  1. Log in with SSH and go to the folder holding the file. Use cd foldername.
  2. Run:
    mysql -u username -p dbname < backup.sql
    This reads the file and loads it into dbname.
  3. Type the password when asked.
  4. Wait until the prompt returns. No message usually means success.

For a compressed file:

gunzip < backup.sql.gz | mysql -u username -p dbname
Tip: Mind the arrows. The arrow > sends data into a file (export). The arrow < reads data from a file (import).

Hit a problem? Open a support ticket.

Quick recap

  • Export with mysqldump, and import with mysql.
  • The database must exist before you import.
  • Remember which way the arrow points.
  • Back up before overwriting.