If your database backup ends in .sql.gz, .sql.zip, .sql.bz2, or another compressed format, you do not need to fully unpack it first in many cases. The fastest method is usually to stream the decompressed SQL directly into the MySQL client, because mysql supports batch execution from standard input and script files.
This guide shows the cleanest way to import compressed SQL dumps into MySQL on Linux, macOS, and Windows, with ready-to-use commands for the most common archive types.
Why import compressed SQL backups directly?
Compressed SQL backups save disk space and bandwidth. For formats that support output to standard output, you can pipe the decompressed stream straight into mysql, which avoids creating another large temporary .sql file on disk. The mysql client supports noninteractive input, and common compression tools such as gzip, bzip2, xz, and zstd support decompression to standard output.
Basic MySQL import syntax
The plain import pattern is:
mysql -u USER -p DATABASE < backup.sqlFor compressed files, you replace the plain file with a decompression command that sends SQL to standard output and pipe that into MySQL.
Before you start
Make sure the destination database already exists unless your dump file creates it. Also avoid putting the password directly on the command line when possible. MySQL says that the safest methods are to let the client prompt for the password or to use a protected option file or login path such as .mylogin.cnf.
A safer connection example is:
mysql -u USER -p DATABASEOr with a saved login path:
mysql --login-path=local DATABASEHow to import every common compressed SQL format
Import .sql.gz files
Use gunzip -c or gzip -dc:
gunzip -c backup.sql.gz | mysql -u USER -p DATABASEgzip -dc backup.sql.gz | mysql -u USER -p DATABASEGNU gzip -d for decompress and -c for writing to standard output.
Import .sql.bz2 files
Use bzcat or bzip2 -dc:
bzcat backup.sql.bz2 | mysql -u USER -p DATABASEbzip2 -dc backup.sql.bz2 | mysql -u USER -p DATABASEImport .sql.xz files
Use xz -dc:
xz -dc backup.sql.xz | mysql -u USER -p DATABASEImport .sql.lzma files
Use xz -dc if your system supports .lzma through xz:
xz -dc backup.sql.lzma | mysql -u USER -p DATABASEThe xz tool supports both .xz and legacy .lzma streams.
Import .sql.zst or .sql.zstd files
Use zstd -dc:
zstd -dc backup.sql.zst | mysql -u USER -p DATABASEThe zstd CLI uses command-line syntax similar to gzip and xz, and -d -c is the standard pattern for decompressing to standard output.
Import .zip files
ZIP archives often contain one SQL file inside. A practical way to stream the first file to MySQL is with funzip, which is designed to extract the first member to standard output. You can also use unzip -p on many systems.
funzip < backup.zip | mysql -u USER -p DATABASEunzip -p backup.zip | mysql -u USER -p DATABASEIf you don’t have unzip installed, you can follow this tutorial on How to Install Zip and Unzip on Linux:
Import .tar.gz or .tgz files
A .tar.gz file is an archive plus compression, so first stream the inner SQL file out of the tarball:
tar -xOzf backup.tar.gz | mysql -u USER -p DATABASEThis works best when the tarball contains a single SQL dump. If it contains several files, list them first and extract the exact SQL file you want.
Import .tar.bz2 files
tar -xOjf backup.tar.bz2 | mysql -u USER -p DATABASEImport .tar.xz files
tar -xOJf backup.tar.xz | mysql -u USER -p DATABASEImport .7z files
For 7-Zip archives, the command-line version supports extraction to standard output via -so. In practice, the clean pattern is to extract the SQL stream and pipe it to MySQL.
7z e -so backup.7z | mysql -u USER -p DATABASEImport .rar files
RAR can also print a file to standard output using the p command.
unrar p -inul backup.rar | mysql -u USER -p DATABASEWindows examples
On Windows, the easiest route is usually one of these:
Using Git Bash, WSL, or a Unix-like shell
gunzip -c backup.sql.gz | mysql -u USER -p DATABASEUsing 7-Zip on Windows
7z e -so backup.sql.gz | mysql -u USER -p DATABASEIf you only have a graphical archive tool and no streaming CLI installed, extract the file first and then run:
mysql -u USER -p DATABASE < backup.sqlFastest way to import a compressed SQL dump
For most server backups, .gz is the most common format, and this is the command most people need:
gunzip -c backup.sql.gz | mysql -u USER -p DATABASEIt is simple, disk-efficient, and aligns with MySQL’s documented batch input model.
Common errors and fixes
ERROR 1049 (42000): Unknown database
The target database does not exist yet. Create it first:
CREATE DATABASE your_database;Then rerun the import.
Access denied for user
Double-check the username, password, host, and permissions.
mysql: [Warning] Using a password on the command line interface can be insecure
This warning appears when the password is passed directly in the command itself. MySQL recommends prompting for the password or using a protected option file or login path instead.
The archive contains multiple files
List the archive contents first, then extract only the SQL file you need. This matters most for ZIP, TAR, 7Z, and RAR files.
Best practices for importing large compressed SQL files
Use screen or tmux on Linux servers for long-running imports, make sure the database uses the right character set, and confirm there is enough disk space for MySQL temporary work even if the SQL stream itself is piped. After the import, validate row counts or check a few critical tables manually.
Final takeaway
If you want the simplest answer to how to import a compressed SQL file into MySQL, use the decompressor’s stdout mode and pipe it into the MySQL client. That works especially well for .gz, .bz2, .xz, and .zst backups, while archive containers such as ZIP, TAR, 7Z, and RAR may need you to target the SQL file inside the archive first. MySQL’s batch import support is what makes the whole workflow so efficient.
FAQ
Can MySQL import a .gz file directly?
Not by filename alone. The normal approach is to decompress to standard output and pipe the SQL into mysql, for example with gunzip -c backup.sql.gz | mysql -u USER -p DATABASE.
What is the best command to import a .sql.gz file into MySQL?
For most systems, this is the go-to command:
gunzip -c backup.sql.gz | mysql -u USER -p DATABASEHow do I import a ZIP SQL backup into MySQL?
If the ZIP contains a single SQL file, stream it with funzip or unzip -p and pipe it into MySQL. funzip is specifically documented for outputting the first archive member to stdout.
Should I use mysqlimport for SQL dump files?
Usually no. MySQL documents mysqlimport as a command-line interface to LOAD DATA, which is for text-file data import rather than replaying a full SQL dump containing statements like CREATE TABLE and INSERT.
Is it safe to put the MySQL password in the command line?
MySQL says that specifying a password on the command line should be considered insecure. Prompting for it or using a protected option file or login path is safer.
