How to Import *.sql File Into a MySQL Database Using CLI?

You can import an exported SQL file using the CLI (command line) in the following steps:

  1. Open terminal, and login to MySQL CLI utility:
    mysql -u <username> -p
    

    Replace <username> with your MySQL username. You will be prompted to enter your MySQL password after executing this command.

  2. Once you're logged in, specify the database you want to use:
    use <database_name>;
    

    Replace <database_name> with the name of the database where you want to import the SQL file.

  3. Specify the SQL file to import using the source command:
    SOURCE data.sql;
    

    Replace "data.sql" with the actual name of the SQL file you want to import.

Alternatively, you can also use the following one-liner command to achieve the same:

mysql -u <username> -p <database_name> < data.sql

This command will log you into the MySQL CLI utility and import the "data.sql" file into the specified database.


This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.