Sentry Answers>SQL>

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

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

Richard C.

The Problem

We frequently need to run SQL statements from a file in MySQL. This allows you to do tasks like:

  • Import a database backup.
  • Run a database upgrade script.
  • Insert new data from a file into an existing table.

Since the files for the tasks mentioned above contain normal SQL statements, you could copy and paste the text into your favorite SQL database manager and run the query. But this would not allow you to automate the tasks — for instance, to schedule them to run at midnight daily.

So let’s discuss exactly how to import a SQL file using the command line (also called the terminal) and how to avoid any of the common errors that can occur.

The Solution

Open a terminal and cd to the folder containing the file that you want to import. Let’s assume the file name is update.sql, and has some commands to alter tables in a database called mydb.

Run your SQL commands by using the input redirection operator, <, to send the contents of your file to MySQL (this works on Windows, MacOS, and Linux). Here we assume your username is me and your password is letmein.

Click to Copy
mysql mydb -u me -pletmein < update.sql

Notice that there is no space between the -p flag to specify the password, and the password itself. There is a space between the -u user flag and the username.

If you don’t specify in the terminal the database on which you want the commands to execute, you’ll get the error No database selected. To fix this, specify the database in update.sql, by adding this line to the top of your file:

Click to Copy
USE mydb;

Ensure User Privileges

If you encounter an error that your user does not have permission to edit the database, you will need to run the following SQL:

Click to Copy
GRANT ALTER, INSERT ON mydb.* TO 'me'@'%'; FLUSH PRIVILEGES;

You can also grant all permissions to the user with the following command:

Click to Copy
GRANT ALL PRIVILEGES ON mydb.* TO 'me'@'%';

Restore a Database Backup

If the file you import into MySQL is intended to recreate a database you backed up, there are some potential problems to be aware of.

Below is the command to back up your database to a file. (If you use Windows, you need to change the file path at the end.)

Click to Copy
mysqldump mydb -u me -pletmein --triggers --routines > /home/me/backup.sql

We include --triggers and --routines to ensure that we export any stored procedures and stored functions that might not be backed up by default.

When you import this file on a new database server, you will get the error Unknown database 'mydb'. To fix this, add the following lines to the top of your file:

Click to Copy
CREATE DATABASE IF NOT EXISTS mydb; USE mydb;

Increasing Speed

If your file is large and the imports take a long time to run, consider disabling the default transaction handling.

Add this line to the top of your file:

Click to Copy
SET autocommit=0;

Add this line to the bottom of the file:

Click to Copy
COMMIT;

This will instruct MySQL to commit your changes only once, when the file has finished running, and will increase performance.

Importing a File From Within MySQL

If you are already in a MySQL session, you can run commands from a file with the following statement:

Click to Copy
source update.sql

There is no advantage to doing this instead of using the terminal. We mention it just in case you encounter it in the future.

  • Syntax.fmListen to the Syntax Podcast
  • Community SeriesIdentify, Trace, and Fix Endpoint Regression Issues
  • Syntax.fm logo
    Listen to the Syntax Podcast

    Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.

    SEE EPISODES

Considered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.

© 2024 • Sentry is a registered Trademark of Functional Software, Inc.