Import and Export MySQL Database Using Command Line

Posted by

We will learn import and export MySql databases through the command-line interface in this article. It is a simple and basic procedure.

To export or import data using MySQL, first, log in to your server with login details.

Export MySQL Database Using the Command Line

We use mysqldump to export data from the database. To use the command, the user should have login details and the privilege to export data.

We use the below command with options to export the data:

-u: It will be the user name of the database.

-p: It will be the password of the database. Also, you can write a password like this( p'YourPaswordHere' ) with p in a single quote

DatabaseName: It will be the database in which you must export the data.

TableName: It is optional. If you want to export particular tables, you can write the table names after the database name.

Suppose your database is on another server and you want to export from that server so you can pass the host in the command.

-h: It will be the hostname of the database that may be IP(Internet Protocol).

Import MySQL Database Using the Command Line

Follow the below steps to import MySQL tables:

  1. Log in to the MySQL server.
  2. Create a database using the command CREATE DATABASE YourDatabaseName; .
  3. Use or select the created database using the command USE YourDatabaseName; .
  4. Run the below command

Also, you can import the tables using the below command

Change the UserName, Password, and DatabaseName with your Username, Password, and Databasename.

YourSqlFile: Change it to the full path to the SQL dump file that needs to be imported.

Example: /home/downloads/products.sql

If the query will run successfully then it will show something like that:

Log in to the MySQL shell and check the data to see whether the import was successful by running USE YourDatabase and SHOW tables command.

If there are any issues with the import, MySQL will show them in the CLI(Command Line Interface).

Leave a Reply

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