How do I import an SQL file using the command line in MySQL?

How do I import an SQL file using the command line in MySQL?

Published at - Aug 13, 2021


mysql -u username -p database_name < file.sql

Check MySQL Options.

Note-1: It is better to use the full path of the SQL file file.sql.

Note-2: Use -R and --triggers to keep the routines and triggers of the original database. They are not copied by default.

Note-3 You may have to create the (empty) database from MySQL if it doesn’t exist already and the exported SQL doesn’t contain CREATE DATABASE (exported with --no-create-db or -n option) before you can import it.


A common use of mysqldump is for making a backup of an entire database:

shell> mysqldump db_name > backup-file.sql

You can load the dump file back into the server like this:


shell> mysql db_name < backup-file.sql

The same in Windows command prompt:

mysql -p -u [user] [database] < backup-file.sql


C:\> cmd.exe /c "mysql -u root -p db_name < backup-file.sql"

MySQL command line

mysql> use db_name;
mysql> source backup-file.sql;

About author

Harendra Kanojiya

Hello, I am Harendra Kumar Kanojiya - Owner of this website and a Fullstack web developer. I have expertise in full-stack web development using Angular, PHP, Node JS, Python, Laravel, Codeigniter and, Other web technologies. I also love to write blogs on the latest web technology to keep me and others updated. Thank you for reading the articles.

Related Posts -

How do I quickly rename a MySQL database (change schema name)?
How do I quickly rename a M ...

For&nbsp;InnoDB, the following seems to work: create the new empty databas...

How to get the size of the MySQL database?
How to get the size of the ...

Run this query and you&#8217;ll probably get what you&#8217;re looking for...

Follow Us

Follow us on facebook Click Here

Facebook QR
Scan from mobile
Join our telegram channel Click Here
Telegram QR
Scan from mobile