How to import/export a MySQL database via SSH?

When dealing with large databases say over 128MB it will take more time doing the importation or exporting via the Cpanel or any other panel. It is therefore recommended to do this via SSH.

In this article, we will look at how to import and export a database via SSH. Be sure to replace the following variables in the commands below:

P.S: We assume that you have already created the database and the database user.

DBUSER – Refers to the MySQL user assigned to your database.

DBNAME – Refers to the name of your MySQL database.

Importing a MySQL database

  1. Log in to the server through SSH
  2. Navigate to the directory where your .sql file is.

To import a MySQL database, use the mysql command. Below is the full command to use:

mysql -u DBUSER -p DBNAME < database.sql

You will be prompted for the password of your MySQL user.

The database.sql is the name of the file that you are importing to your database. This must be done in your current working directory.

Exporting a MySQL database

To export a MySQL database, use the mysqldump command. Below is the full command for exporting your database:

mysqldump -u DBUSE -p DBNAME > database.sql

You will be prompted for a password – this is your MySQL user’s password.

The MySQL database will be exported to a file named “database.sql” in your current directory.

That is all you need to know about how to import or export the database via SSH.

Was this article helpful?

Related Articles

Leave A Comment?