Home >Database >Mysql Tutorial >How to Import SQL Files into MySQL via the Command Line?

How to Import SQL Files into MySQL via the Command Line?

Barbara Streisand
Barbara StreisandOriginal
2024-12-22 01:03:20457browse

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:

  • -u username: Your MySQL username
  • -p: Prompts you to enter your password interactively
  • database_name: The name of the database to import into
  • file.sql: The path to your SQL file

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn