How to Import an SQL File Using the Command Line in MySQL
Introduction
Importing an SQL file into MySQL using the command line is a common task for database administrators and developers. This guide will show you how to accomplish this step by step.

Prerequisites
- MySQL installed on your system.
- An SQL file ready for import.
- Access to the command line or terminal.
- Valid MySQL user credentials.

Steps to Import an SQL File
Step 1: Open the Command Line
To begin, open your terminal or command prompt. Ensure you have the necessary permissions to run MySQL commands.

Step 2: Log in to MySQL
Log in to your MySQL server using the following command:
mysql -u username -p
Replace username
with your MySQL username. Press Enter, then enter your password when prompted.

Step 3: Select the Database
Once logged in, select the database where you want to import the SQL file:
USE database_name;
Replace database_name
with the name of your target database.
Step 4: Import the SQL File
Run the following command to import your SQL file:
SOURCE /path/to/your/file.sql;
Replace /path/to/your/file.sql
with the actual path to your SQL file.

Common Errors and Troubleshooting
- Error: “Access denied for user” – Ensure your username and password are correct.
- Error: “No database selected” – Ensure you have selected a database using the
USE
command. - Error: “File not found” – Verify the path to your SQL file is correct.
For more information on troubleshooting MySQL errors, visit the official MySQL documentation.

Conclusion
Importing an SQL file into MySQL via the command line is a straightforward process when you follow these steps. With this guide, you can manage your databases more effectively and efficiently.
To learn more about MySQL commands, check out this comprehensive MySQL command tutorial.
