To import an SQL file using the command line in MySQL, you can use the mysql
command with the <
operator. Here are the steps:
- Open a command prompt or terminal window on your computer.
- Navigate to the directory where your SQL file is located using the
cd
command if necessary. - Use the
mysql
command to import the SQL file. The basic syntax is as follows:
1 |
mysql -u username -p database_name < file.sql |
- 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.
- After running the command, you will be prompted to enter your MySQL password. Enter the password associated with the provided username.
- 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:
1 |
mysql -u myuser -p mydatabase < data.sql |
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.