How to import SQL file to MySQL using Command Line

Importing any big size sql file in your database (phpMyAdmin) is very difficult to import directly in mysql or database, So we need to import the sql file using the command line.

1. Open your command prompt or terminal:

On Windows, press Win+R, type cmd and hit enter. On macOS or Linux, open your terminal application.

2. Navigate to the directory containing the SQL file:

Use the cd command to navigate to the directory that contains the SQL file you want to import. For example, if your SQL file is located in the C:\Users\JohnDoe\Downloads directory on Windows, you can navigate to it by typing the following command:

cd C:\Users\JohnDoe\Downloads

3. Log in to MySQL:

To import an SQL file, you must be logged in to MySQL. Use the following command to log in:

mysql -u username -p

Replace your username with your MySQL username. You will be prompted to enter your password. Enter it and hit enter.

4. Create a database

If you haven’t already created a database to import the SQL file to, you can create one using the following command:

CREATE DATABASE database_name;

Replace database_name with the name you want to give your database.

5. Import the SQL file:

Use the following command to import the SQL file to your database:

mysql -u username -p database_name < file.sql

Replace username with your MySQL username, database_name with the name of the database you created in step 4, and file.sql with the name of your SQL file.

The < symbol is used to redirect the contents of the SQL file to the mysql command, which then imports it into the specified database.

Once the import process is complete, you can verify that the SQL file was imported successfully by logging in to MySQL and running a query to select data from the imported tables.

Leave a Comment