3 Ways To Import SQL File In MySQL (Step-By-Step Guide)

Welcome to a tutorial on how to import SQL files. So you have downloaded an SQL file from the Internet, and don’t know how to import it?

There are 3 common ways to import an SQL file into MYSQL:

  1. Run mysql -u USER -p DATABASE_NAME < PATH/TO/FILE.sql in the command line (or terminal).
  2. Use MySQL Workbench to import SQL files.
  3. Use a web-based database manager such as phpMyAdmin.

Let us walk through these methods step-by-step in this guide – Read on!

 

 

TABLE OF CONTENTS

 

IMPORT SQL FILE IN COMMAND LINE

The easiest method, doesn’t even need to install any tools – Just use the command line to import SQL files.

 

IMPORT COMMAND EXAMPLE

D:\http\>mysql -u root -p test < d:\http\test.sql
Enter password:

D:\http\>

Yep, it’s that simple, just run the command mysql -u USER -p DATABASE < PATH/FILE.SQL.

 

WINDOWS “UNKNOWN COMMAND”

D:\http\>mysql -u root -p test < d:\http\test.sql
'mysql' is not recognized as an internal or external command,
operable program or batch file.

For Windows users, if you get the “MySQL is not recognized” error, you will need to add the MySQL folder to the system path.

 

 

START > SEARCH FOR “SYSTEM ENVIRONMENT”

 

ENVIRONMENTAL VARIABLES

 

SYSTEM VARIABLES > PATH

 

ADD MYSQL TO THE PATH

 

 

IMPORT SQL FILE WITH MYSQL WORKBENCH

If you are uncomfortable with the command line or have to manage multiple databases, Oracle offers a free management tool called MySQL Workbench.

 

DOWNLOAD & INSTALLATION

You can get the latest version of MySQL Workbench from the official MySQL website. Installation is very straightforward, just launch the installer and hit “next”.

 

HOW TO IMPORT WITH MYSQL WORKBENCH

ADD MYSQL CONNECTION

Launch MySQL Workbench. Add a connection to your database, then click on it.

 

SERVER > DATA IMPORT

After connecting to your database, Server > Data Import.

 

SELECT FILE > SELECT DATABASE > START IMPORT

Lastly, just choose the SQL file to import, and where to import it into.

 

 

PHPMYADMIN IMPORT SQL FILE

This final method is another tool that is made in PHP – If you have installed the XAMPP server, then you already have it.

 

DOWNLOAD & INSTALLATION

If you have installed the XAMPP server, simply access http://localhost/phpmyadmin. If not, you can also get the latest version of phpMyAdmin from their official website. There is no installation required, just unzip it into your “public HTML” folder.

 

HOW TO IMPORT WITH PHPMYADMIN

You can probably figure this one on your own… Select a database > Import > Choose File > Go.

 

UPLOAD FILE SIZE LIMIT

php.ini
upload_max_filesize = 100M
post_max_size = 100M

Please take note that phpMyAdmin is limited by the upload limit set in the php.ini file – You will want to avoid large SQL files, or at least zip them before uploading. Alternatively,  you can change the limits in php.ini .

 

 

EXTRAS

That’s all for this project, and here is a small section on some extras and links that may be useful to you.

 

WHICH IS THE BEST METHOD?

Well, everyone has their preferences – Some prefer to not install anything and import SQL files in the command line, while a few like to use the Workbench to manage multiple databases at the same time. All 3 methods work fine, just test it out yourself and stick with the one you are most comfortable with.

 

LINKS & REFERENCES

 

THE END

Thank you for reading, and we have come to the end of this guide. I hope that it has helped you in your project, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!