How to import SQL file to MySQL using Command Line

Deve­lopers, database administrators, and individuals involved with database­s often need to import SQL file­s into a MySQL database for various reasons such as updating or configuring the database­. These SQL files typically include­ significant data, table structures, and require­d configurations that enable smooth setup or modification of a database­ instance.

GUIs can be use­d to import SQL files, but using Command Line Interface (CLI) has advantages. It’s faster and more­ efficient with greate­r flexibility and various scenarios such as remote­ server work or integration with scripts and automate­d processes.

This article guide­s the reader on how to import SQL file­s to MySQL using Command Line. Whether a MySQL ne­wbie or an experie­nced user looking to improve database­ management skills, mastering this te­chnique allows efficient handling of SQL file­ imports and ensures smooth data manageme­nt in all projects.

I am using XAMPP in Windows for accessing the database using phpMyAdmin.

How to import SQL file to MySQL using Command Line


Contents

  1. Create Database
  2. Copy SQL file
  3. Open Command Prompt
  4. Output
  5. Conclusion

1. Create Database

  • Open XAMPP and start Apache Server and MySQL Database.
  • First, need to create a database in MySQL.
  • Open phpMyAdmin and create a new database.
  • I have created a tutorial Database.

Create MySQL database using phpMyAdmin


2. Copy SQL file

  • Copy your SQL file to xampp/mysql/bin/ directory.
  • I copied tutorial.sql file.

Copy SQL file to xampp/mysql/bin folder


3. Open Command Prompt

  • Open Command Prompt.
  • Navigate to xampp/mysql/bin/ directory.

Navigate to mysql/bin using Command Prompt

  • Type following command –

Syntax –

mysql -u username -p database_name < file.sql

username = Your MySQL username.

database_name = Database name in which you want to import.

file.sql = SQL file name.

Example –

mysql -u root -p tutorial < tutorial.sql
  • I am importing tutorial.sql file in tutorial Database.
  • Type the password if you have assigned to the user otherwise press Enter.

Run command in CMD to import SQL file to Database


4. Output

  • Open phpMyAdmin and select the database to check tables imported or not.

phpMyAdmin after importing SQL file to database


5. Conclusion

Before executing the import command first create your MySQL database using PHPMyAdmin and copy the SQL file in xampp/mysql/bin folder.

You can view this tutorial to know how you can export MySQL database using Command Line.

If you found this tutorial helpful then don't forget to share.