How to Import a MySQL Database Using the Command Line

MySQL is one of the most popular relational database management systems (RDBMS) used by developers around the world. It’s known for its scalability, performance, and ease of use. One of the key features of MySQL is its ability to import and export databases using SQL files. In this blog post, we’ll show you how to import a MySQL database using the command line.

Prerequisites

Before we get started, make sure that you have the following:

  • A MySQL installation on your system
  • A MySQL database to import the data into
  • An SQL file containing the data to import

Importing a MySQL Database Using the Command Line

To import a MySQL database using the command line, follow these steps:

  • Open a terminal window or command prompt.
  • Navigate to the directory containing the SQL file you want to import.
  • Log in to MySQL using the following command:
mysql -u username -p

Replace username with your MySQL username. You will be prompted to enter your MySQL password.

  • Create a new database or select an existing database to import the data into:
CREATE DATABASE database_name;
USE database_name;

Replace database_name with the name of the database you want to create or select.

  • Import the SQL file into the database using the following command:
mysql -u username -p --show-progress database_name < file.sql

Replace username with your MySQL username, database_name with the name of the database you want to import to, and file.sql with the name of the SQL file you want to import.

The --show-progress option will display a progress bar in the terminal window or command prompt, showing you the progress of the import.

  • Once the import is complete, exit MySQL using the following command:
exit;

Conclusion

Importing a MySQL database using the command line is a straightforward process that can be completed in just a few steps. By following the steps outlined in this blog post, you can quickly and easily import data into your MySQL database and get started with building your application.