How to Import / Export MySQL Database via SSH

In this quick article, I will show you how to import and export MySQL database using SSH command. We can import and export small size of the database (i.e, Less than 50 MB) using phpMyAdmin very easily. But if the database is big, it’s too difficult via phpMyAdmin. So we can use SSH command to import and export large size of the database.

Importing MySQL Database via SSH

Here is the SSH command to import a MySQL database,

mysql -u dbusername -pdbpassword dbname < /path/to/file/backup.sql

In the above command,

  • dbusername: name of a database user assigned to this database.
  • dbpassword: the database user password.
  • dbname: the name of your database you are importing to.
  • /path/to/file/backup.sql: the path to the MySQL dump that you are importing to. Here you can use both relative and absolute path formats. If you are in the folder with the file, you can just type the file name with the extension.

Note: In the above command, don’t give the space between -p key and the password if you enter it in the command.

You can also use the following command to import the MySQL database,

mysql -u dbusername -p dbname < /path/to/file/backup.sql

In this way, you need to enter the password in the next line.

Example:

mysql -u root -pwelcome mydb < /home/blogtreat/public_html/data.sql

Exporting MySQL Database via SSH

Here is the SSH command to export a MySQL database,

mysqldump -u dbusername -pdbpassword dbname > /path/to/file/backup.sql

In the above command,

  • dbusername: name of a database user assigned to this database.
  • dbpassword: the database user password.
  • dbname: the name of your database you are exporting from.
  • /path/to/file/backup.sql: the path to the MySQL dump that you are exporting from.

You can also use the following command to export the MySQL database,

mysqldump -u dbusername -p dbname > /path/to/file/backup.sql

In this way, you need to enter the password in the next line.

Example:

mysqldump -u root -pwelcome mydb > /home/blogtreat/public_html/data.sql

Hope this helps.

Leave a Reply

Your email address will not be published. Required fields are marked *