The Command
mysql appdb < dump.sql
The database appdb must already exist. The same command works with MariaDB, where the client is also called mariadb. Credentials come from ~/.my.cnf, from the system root account through sudo on Ubuntu and Debian, or from a prompt: mysql -u app_user -p appdb < dump.sql asks for the password instead of taking it on the command line.
Before You Import: Back Up the Target
Warning first: a dump made by mysqldump normally contains a DROP TABLE IF EXISTS statement before each table. Importing it into a database that already has tables with the same names replaces them, and their current data is gone. Look at the file and dump the target before you import over it:
grep -c 'DROP TABLE' dump.sql
grep -n -m 5 -E '^(CREATE DATABASE|USE )' dump.sql
sudo mysqldump --single-transaction appdb > appdb-before-import.sql
If the second command finds CREATE DATABASE or USE lines, the dump was made with --databases or --all-databases and writes into the original database names, whatever name you give on the command line.
Step 1: Create the Database
sudo mysql -e "CREATE DATABASE appdb CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci"
sudo mysql -e "GRANT ALL PRIVILEGES ON appdb.* TO 'app_user'@'localhost'"
The account 'app_user'@'localhost' must exist already; see how to list MySQL users.
Step 2: Import
sudo mysql appdb < dump.sql
No output means success. A compressed dump is unpacked on the fly:
gunzip < dump.sql.gz | sudo mysql appdb
zstd -dc dump.sql.zst | sudo mysql appdb
Show Progress on Large Files
The pv tool shows how much of the file has been read (install the pv package; on AlmaLinux and Rocky Linux it comes from EPEL):
pv dump.sql | sudo mysql appdb
pv dump.sql.gz | gunzip | sudo mysql appdb
Run long imports inside tmux, so a dropped SSH connection does not stop them; see the tmux cheat sheet.
Import from Inside the Client
USE appdb;
SOURCE /home/user/dump.sql;
The path is on the machine where the client runs. This form prints a line per statement, which helps you see where an error occurs.
Character Set
If accented letters or other non-ASCII text look garbled after the import, set the connection character set to match the dump:
sudo mysql --default-character-set=utf8mb4 appdb < dump.sql
Dumps made by mysqldump usually set this themselves near the top of the file.
Common Errors
- ERROR 1049, Unknown database: create the database first.
- ERROR 1044 or 1045, Access denied: the account lacks rights on that database or the password is wrong.
- ERROR 1227 about DEFINER: views or triggers name an account that does not exist here. Import as an administrative account, or create that account.
- Unknown collation: a dump from MySQL 8 or later that uses
utf8mb4_0900_ai_cidoes not import into MariaDB before 11.4.5, which added it as an alias. On older MariaDB, replace it withutf8mb4_unicode_ciin a copy of the file. - Packet too large or server has gone away: raise
max_allowed_packetin the server's configuration (or withSET GLOBAL, which applies to new connections) and pass--max_allowed_packet=512Mto the client. The limit is 1G on both sides.
Check the Result
sudo mysql -e "SHOW TABLES" appdb
Compare table and row counts with the source. Making the dump in the first place is covered in how to back up MySQL with mysqldump.