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
cdcommand if necessary.
- Use the
mysqlcommand to import the SQL file. The basic syntax is as follows:
mysql -u username -p database_name < file.sql
usernamewith your MySQL username.
database_namewith the name of the database where you want to import the SQL data.
file.sqlwith 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:
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.