Skip to content
Log In Sign Up info@muvcloud.com

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_ci does not import into MariaDB before 11.4.5, which added it as an alias. On older MariaDB, replace it with utf8mb4_unicode_ci in a copy of the file.
  • Packet too large or server has gone away: raise max_allowed_packet in the server's configuration (or with SET GLOBAL, which applies to new connections) and pass --max_allowed_packet=512M to 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.

Updated:

Linux VPS with Full Root Access

MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.

Cloud VPS Plans Starting at2.99 $/mo Cheap Linux VPS

No Limits on Service or Support

You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

Support specialist
  • 100% customer satisfaction goal
  • Our own hardware
  • 24/7 ticket and email support
  • 4 countries with locations