How to import SQL files using the command line in MySQL?

To import an SQL file using the command line in MySQL, you can use the mysql command with the < operator. Here are the steps:

  1. Open a command prompt or terminal window on your computer.
  2. Navigate to the directory where your SQL file is located using the cd command if necessary.
  3. Use the mysql command to import the SQL file. The basic syntax is as follows:
  • Replace username with your MySQL username.
  • Replace database_name with the name of the database where you want to import the SQL data.
  • Replace file.sql with the name of the SQL file you want to import.
  1. After running the command, you will be prompted to enter your MySQL password. Enter the password associated with the provided username.
  2. The SQL file will be executed, and the data will be imported into the specified database.

For example, if you have a MySQL user named “myuser,” a database named “mydatabase,” and an SQL file named “data.sql” in the current directory, you can import it like this:

Make sure you have the necessary privileges to import data into the specified database. Additionally, be cautious when importing SQL files from untrusted sources, as they may contain malicious code.