Home >Database >Mysql Tutorial >How to Import SQL Files into MySQL via the Command Line?
Importing SQL Files through Command Line in MySQL
This article addresses the common issue of importing SQL files using the command line in MySQL. It provides detailed instructions on how to properly achieve this task, ensuring a successful import without encountering syntax errors.
To import an SQL file, you can utilize the following command:
mysql -u username -p database_name < file.sql
Where:
However, if you are encountering syntax errors, you may need to create the destination database first. This can be done using the following command:
CREATE DATABASE database_name;
Additionally, it is recommended to use the full path of the SQL file for best results. Note that MySQL options like -R and --triggers can be employed with mysqldump to preserve routines and triggers if desired.
In certain scenarios, you may need to manually create the database if it does not already exist. Similarly, if the exported SQL file does not include CREATE DATABASE commands (due to the use of --no-create-db or -n options), you will need to manually create the empty database in MySQL before importing.
The above is the detailed content of How to Import SQL Files into MySQL via the Command Line?. For more information, please follow other related articles on the PHP Chinese website!