Richard C.
—We frequently need to run SQL statements from a file in MySQL. This allows you to do tasks like:
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.
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
.
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:
USE mydb;
If you encounter an error that your user does not have permission to edit the database, you will need to run the following SQL:
GRANT ALTER, INSERT ON mydb.* TO 'me'@'%'; FLUSH PRIVILEGES;
You can also grant all permissions to the user with the following command:
GRANT ALL PRIVILEGES ON mydb.* TO 'me'@'%';
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.)
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:
CREATE DATABASE IF NOT EXISTS mydb; USE mydb;
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:
SET autocommit=0;
Add this line to the bottom of the file:
COMMIT;
This will instruct MySQL to commit your changes only once, when the file has finished running, and will increase performance.
If you are already in a MySQL session, you can run commands from a file with the following statement:
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.
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “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.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.