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

Created November 10, 2021


In some cases if you have a really large .sql file, it is best to import it via the command line.

You might need to do this in case that you are using a tool like PHPmyAdmin, which more often than not, has a certain limit that the file that you want to import could be.

In this tutorial, you will learn how to import an .sql file


Before you get started you need to have MySQL installed and an .sql backup.

In case that you don't have an .sql backup of your database, you can follow the steps here on how to do that:

How to export/backup a MySQL/MariaDB database with mysqldump

Creating a blank database

What I usually prefer doing is to create a completely empty database, so that way I know that there will be no existing data or table structure which could cause issues during the import.

To create a new blank database, first access MySQL:

mysql -u your_user -p 

And then you can use the following statement:


Finally exit the MySQL prompt:


Note: make sure to change the demo_db with the name of the database that you want to create.

Importing an .sql file to MySQL

Once you have your plain database in place, you can import your .sql file using the following command:

mysql -h localhost -u username -p demo_db < your_exported_file.sql

Rundown of the command:

Once you run the command it might take a while for the operation to complete depending on the size of the file. You would not see any output while the operation is on going.


This is pretty much it! Now you know how to import a .sql file to a MySQL database!

To learn more about SQL, make sure to check out this free eBook here:

💡 Introduction to SQL eBook

In case that you are already an SQL expert, and want to drastically reduce the latency of your data analytics, I would recommend checking out Materialize!

Materialize - a streaming database

Materialize is a Streaming Database for Real-time Analytics. Materialize is a reactive database that delivers incremental view updates and it helps developers easily build with streaming data using standard SQL.