Knowledge Base Hub

Browse through our helpful how-to guides to get the fastest solutions to your technical issues.

Home  >  Database  >  How to Import and Export MySQL Database via Command Line (SSH)?
Top Scroll

How to Import and Export MySQL Database via Command Line (SSH)?

 3 min

If you are an advanced user, you may wish to use SSH to access and make changes to your account, as it offer a more robust set of options with less limitations. For this, you need to have a basic idea about SSH.

Steps to Import Your Database from the Command Line:

Note: Make sure your database file is in .sql format, it cannot be compressed in a .zip or .tar.gz.

  1. At first, login to SSH upload your database to your home directory (You can do this using cPanel File Manager, an FTP client, or SSH).
  2. In case the database isn’t available, please create a database, a user, and give the user complete privileges to the database. Take a note of the username and password.
  3. Then log into your server using SSH.
  4. With the ls -l command, verify that the file is present in your current directory.
  5. In case the file isn’t available, upload/move the file to the correct directory, or go to the directory where the file is located.
  6. Enter the below command and replace ‘user_name’, ‘database_name, and ‘file.sql’ with your username, database name and file:
    mysql -p -u user_name database_name < file.sql
  7. You will be asked for your database user password, and then your database will be imported.

Steps to Export Database from the Command Line

  1. Open the command line on the computer where the database is stored. For example, if your database is on another web hosting account or with another web hosting provider, use SSH and log in to the account. In case you have physical access to the computer, open a DOS or terminal window to access the command line.
  2. Type the below command, and then press Enter. Replace username with your username, and dbname with the database name that you want to export:
    mysqldump -u username -p dbname > dbexport.sql

    Note: In this example, the dbexport.sql filename is used for the exported database, but you can name the file with the one you want.

  3. Enter your password at the Enter password prompt.
  4. The dbexport.sql file now comprise of all the data for the dbname database. If the dbexport.sql file is located on a remote computer, download the file to your local computer.

In this way, you can import and export your database via command line.

Also Read:

How to Import and Export the MySQL Database with phpMyAdmin?

MySQL Database Import Is Failing, Reason: The .sql File Is Too Big

For our Knowledge Base visitors only
Get 10% OFF on Hosting
Special Offer!
30
MINS
59
SECS
Claim the discount before it’s too late. Use the coupon code:
STORYSAVER
Note: Copy the coupon code and apply it on checkout.