How to Import a SQL File into MySQL
Posted by Kyle Hankinson
Short answer
From a terminal:
mysql -u root -p database_name < /path/to/file.sql
You are asked for the password, then every statement in the file runs against database_name. The database must already exist:
mysql -u root -p -e "CREATE DATABASE database_name CHARACTER SET utf8mb4"
If you are already inside the mysql client, use SOURCE instead:
USE database_name;
SOURCE /path/to/file.sql;
Command-line options you will need
| Situation | Add |
|---|---|
| Server on another machine | -h db.example.com |
| Non-default port | -P 3307 (capital P; lowercase -p is the password) |
| File has accents, emoji or non-Latin text | --default-character-set=utf8mb4 |
| Keep going past errors | --force (errors are printed, the rest still runs) |
| See each statement as it runs | -v (or -vvv for more detail) |
| Compressed dump | gunzip < file.sql.gz \| mysql -u root -p database_name |
A complete example for a remote server:
mysql -u admin -p -h db.example.com -P 3306 \
--default-character-set=utf8mb4 shop < shop_backup.sql
On Windows, run the same command in Command Prompt. In PowerShell the < redirect does not work; use Get-Content file.sql | mysql -u root -p database_name, or cmd /c "mysql -u root -p database_name < file.sql".
See progress on a large import
The mysql client prints nothing until it finishes. To see progress, pipe the file through pv (install it with Homebrew or your package manager):
pv /path/to/file.sql | mysql -u root -p database_name
Making large imports faster
A mysqldump file already turns off unique and foreign key checks at the top of the file and uses multi-row INSERTs, so it usually imports well as is. For other large files:
SET autocommit = 0;
SET unique_checks = 0;
SET foreign_key_checks = 0;
SOURCE /path/to/file.sql;
SET foreign_key_checks = 1;
SET unique_checks = 1;
COMMIT;
Only turn the checks off for data you trust: rows that break a foreign key or a unique index will not be caught.
Common errors
- ERROR 1049 (42000): Unknown database: create the database first, as above.
- ERROR 2006: MySQL server has gone away or Got a packet bigger than 'max_allowed_packet': a single statement in the file is bigger than the server allows. Raise the server limit (as a privileged user:
SET GLOBAL max_allowed_packet = 1073741824;, which applies to new connections), and pass--max-allowed-packet=1Gto the client. - ERROR 1064: You have an error in your SQL syntax: often a dump from a newer MySQL or from MariaDB, or a file that is not plain SQL (a
.gz, or a PostgreSQL dump). Check the first lines of the file. - Garbled characters after the import: the file and the connection disagreed on the character set. Re-import with
--default-character-set=utf8mb4(see fixing incorrect string value: utf8 vs utf8mb4). - ERROR 1227: Access denied; you need (at least one of) the SUPER privilege(s): the dump contains
DEFINER=clauses orSET @@GLOBALlines from the original server, which managed services such as Amazon RDS block. Remove theDEFINER=...parts from the file, and if the error points at aGTID_PURGEDline, create the dump again with--set-gtid-purged=OFF.
Export and import in one go
mysqldump writes the file that mysql reads back:
mysqldump -u root -p --single-transaction --routines --triggers shop > shop.sql
mysql -u root -p shop_copy < shop.sql
--single-transaction takes a consistent snapshot of InnoDB tables without locking them, and --routines --triggers includes stored procedures, functions and triggers.
With a GUI client
In MySQL Workbench, use Server > Data Import and choose "Import from Self-Contained File".
In SQLPro for MySQL, connect to the server, right-click the database in the sidebar and choose Import > From SQL..., then pick the file. The same Import menu also loads CSV and JSON files into a table.
Related guides
About the author - Kyle Hankinson is the founder and sole developer of SQLPro for MySQL and the Hankinsoft Development suite of database tools. He has been building native macOS and iOS applications since 2010, and now ships SQLPro on Windows and Android as well.
Try SQLPro for MySQL - A native MySQL and MariaDB client for macOS, iOS, and Android. No Java required.
Download Free Trial View Pricing Compare