How to Import SQL File to MySQL using Command Line

1. Log in to your MySQL server from the command line.

2. Create a database to which you will import the SQL file.

3. Use the command line to navigate to the folder containing the SQL file.

4. Enter the following command to import the SQL file: mysql -u username -p database_name < file.sql 5. Enter the password for the MySQL user when prompted. 6. The SQL file will be imported into the specified database. [ad_1]

If you have to make any changes in any table in your existing database. Or have to do any update. And you are executing query for this for creating, updating and modify in any database table. So instead you can import sql file in mysql database by command line.

In this tutorial, you will learn how to import sql file to mysql database using command line or terminal.

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

By using the following steps, you can easily import sql files to mysql database using command line:

  • Step 1: Connect to MySQL Server
  • Step 2: Create a New Database
  • Step 3: Select the Database
  • Step 4: Import the SQL File
  • Step 5: Verify the Import

Step 1: Connect to MySQL Server

Firstly, open your terminal or command line and execute the following command into it to connect to the MySQL server:

mysql -u <username> -p

Replace <username> with your MySQL username. You will be prompted to enter your MySQL password after running this command. Enter your password and press Enter.

Step 2: Create a New Database

If you already have a database in which you want to import sql file. So, you can skip this step. Otherwise, execute the following commands on the terminal or command line to create the database first:

CREATE DATABASE <database_name>;

Replace with the name you want to create the database.

Step 3: Select the Database

When you have created the database, you need to select it before importing the SQL file. You can do this by running the following command:

USE <database_name>;

Replace <database_name> with the name of the database you just created or the one you want to import the SQL file into.

Step 4: Import the SQL File

Execute the following commands on the terminal or command line to import the SQL file into your MySQL database:

SOURCE /path/to/file.sql;

Replace /path/to/file.sql with the path to the SQL file you want to import.

Note: Make sure that the SQL file is in a location that is accessible from the MySQL server. If the SQL file is on your local machine, you may need to transfer it to the server using a file transfer protocol such as FTP or SCP.


Step 5: Verify the Import

Execute the following command on terminal or command line to verify that the data imported successfully:

SELECT * FROM <table_name>;

Replace <table_name> with the name of the table you want to verify.

Conclusion

That’s it; In this tutorial, you have covered how to import an SQL file using the command line in MySQL. The process involves connecting to the MySQL server, creating a new database (if necessary), selecting the database, importing the SQL file, and verifying the import. Importing an SQL file is a straightforward process that can save you a lot of time and effort, especially when dealing with large databases.

Recommended Tutorials


Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?