How to Import / Export MySQL Database via SSH

In this quick article, I am going to show you how to import and export MySQL database using CLI command (Command Line Interface). If the database is small (i.e, Less than 50 MB), we can use phpMyAdmin. But if the server doesn’t have the phpMyAdmin access or If the database is too big, then it’s very difficult to import/export the database on the server. To make this process simple, this blog will help you.

Importing MySQL Database via SSH

Here is the SSH command to import a MySQL database on the server,

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.

Example:

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

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 -p mydb < /home/blogtreat/public_html/data.sql

Exporting MySQL Database via SSH

Here is the SSH command to export a MySQL database from the server,

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 to.

Example:

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

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 -p mydb > /home/blogtreat/public_html/data.sql

Hope this helps.

Leave a Reply

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