How do I import an SQL file using the command line in MySQL?

Created by bansiLinked to 25.1m issues across 76 teams


First, make sure you have the full path of the SQL file file.sql. It is important to use the full path to ensure the file is imported correctly.

Next, open the command line and type in the following command:

`mysql -u username -p database_name < file.sql`

This command will import the SQL file into the specified database.

If you want to keep the routines and triggers of the original database, use the -R and --triggers options with mysqldump.

If the database does not exist yet, you may have to create it from MySQL before you can import the SQL file. To do this, make sure the exported SQL does not contain the CREATE DATABASE command (exported with --no-create-db or -n option).

Once you have followed these steps, you should be able to successfully import an SQL file using the command line in MySQL.

We use cookies to personalize content
run ads, and analyze traffic.