Open In App

How To Export and Import a .SQL File From Command Line With Options?

Last Updated : 02 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Structured Query Language is a computer language that we use to interact with a relational database.SQL is a tool for organizing, managing, and retrieving archived data from a computer database. In this article , we will learn to export and import .SQL files with command line options.

Export:

You can export the database along with its structure into .SQL file using a command line tool called mysqldump as follows , 

Syntax :

mysqldump -h localhost -u username -p database > filename.sql

where,

  • -h / –host : host address to connect
  • u / –user : username of the account in the server
  • -p / –password : to get the password prompt
  • database : name of the database to import
  • filename.sql : the output file

For example, the complete command looks something like the below and creates a file by the name filename.sql.

Query:

mysqldump --column-statistics=0 -h 
localhost -u root -p --events 
--triggers --routines company  
> filename.sql

Output :

 

 

This has created the sql file with the given name.

Import :

You can import the database along with its structure into .SQL file using a command line tool called  mysql as follows , 

Syntax :

mysql -h localhost -u username -p database < input.sql

where,

  • -h / –host : host address to connect
  • -u / –user : username of the account in the server
  • -p / –password : to get the password prompt
  • database : name of the database to import
  • input.sql : the input .SQL file

For example, here we will import the filename.sql that was exported above:

Query:

mysql -h localhost -u root 
-p company < filename.sql

Output :

 

Now you can find the database company on the server.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads