I'm trying to import a gzipped SQL file into mysql directly. Is this the right way?

mysql -uroot -ppassword mydb > myfile.sql.gz
  • 16
    Even ignoring the gzip side of the question, your arrow is pointing the wrong way... – Matt Fletcher Sep 27 '16 at 9:47

11 Answers 11

zcat /path/to/file.sql.gz | mysql -u 'root' -p your_database

> will write the output of the mysql command on stdout into the file myfile.sql.gz which is most probably not what you want. Additionally, this command will prompt you for the password of the MySQL user "root".

| improve this answer | |
  • 17
    As a good security practice, I would put my password on the command line, I would let mysql ask for it. – Prof. Moriarty May 3 '10 at 8:19
  • 12
    Or even better: create ~/.my.cnf with the credentials. ;) – joschi May 3 '10 at 10:46
  • 7
    As @Prof. Moriarty explans, you can modify the command to not use the password via zcat /path/to/file.sql.gz | mysql -u 'root' -p your_database. It will know the last parameter is the database you wish to use, not your password. – bafromca Jan 20 '15 at 0:20
  • 7
    To slightly correct @Prof.Moriarty's comment, a good security practice would be to not put my password on the command line (where it will get stored in history, or seen over your shoulder), and let MySQL ask for it. The -p flag alone will cause MySQL to ask at a prompt for the password. – George Aug 3 '16 at 8:00
  • 4
    +1 for something that leaves the damn database dump compressed – Dmitri DB Jan 12 '17 at 19:18

To display a progress bar while importing a sql.gz file, download pv and use the following:

pv mydump.sql.gz | gunzip | mysql -u root -p <database name>

In CentOS/RHEL, you can install pv with yum install pv.

In Debian/Ubuntu apt-get install pv.

In MAC, brew install pv

| improve this answer | |
  • 2
    pv seems to be in the Ubuntu repos too (at least in 12.04 LTS it is), but again you need to do sudo apt-get install pv to get it. Thanks Banjer, this is perfect for big database imports! – toon81 Feb 19 '15 at 15:25
  • I had to run pv mydump.sql.gz | gunzip | mysql -u root my_database_name. That was because I was importing tables and I don't have a password set for my root user – Cristiano Mendonça Feb 9 '16 at 22:19
  • In MAC, brew install pv – score Apr 28 '16 at 19:30

The simplest way is to unzip the database file before importing. Also as mentioned by @Prof. Moriarty you shouldn't be specifying the password in the command (you'll be asked for the password). This command taken from webcheatsheet will unzip and import the database in one go:

gunzip < myfile.sql.gz | mysql -u root -p mydb
| improve this answer | |
  • 1
    Also, mydb needs to be created before importing. This doesn't create the db for you. – Siddhartha Apr 20 '15 at 23:00
  • i found my piping gunzip on a 10GB compressed file caused my import to freeze. not sure if that's due to memory constraints or something but i'd err on the side of doing one step at a time in the future. – ryantuck Jan 7 '16 at 16:42
  • @RyanTuck That is pushing the limits of these kind of processes :) – icc97 Jan 7 '16 at 19:37
  • 1
    @Siddhartha That depends on the sql dump file. Sometimes they include create database statements. – rooby Aug 30 '17 at 22:55
  • @rooby that makes sense. – Siddhartha Sep 1 '17 at 0:41

If you get an error from zcat, in which the error message contains the file name with an extra suffix .Z, then try using gzcat instead, as described at https://stackoverflow.com/questions/296717/zcat-wont-unzip-files-properly

| improve this answer | |

On macOS, I used this:

zcat < [Database].sql.gz | mysql -u root -p [Database Name in MySQL]

Enter your password, and voila!

| improve this answer | |

Also check if there is any USE-statement in the SQL file. Specifying the database at the command line doesn't guarantee that the data ends up there if a different destination is specified within the SQL file.

| improve this answer | |
  • 2
    You just need to extend the command like so: pv mydump.sql.gz | gunzip | mysql -u root -p your_database. The accepted answer uses this approach. – bafromca Jan 20 '15 at 0:17

For bzip2 compressed files (.sql.bz2), use:

bzcat <file> | mysql -u <user> -p <database>


pv <file> | bunzip2 | mysql -u <user> -p <database>

to see progress bar.

| improve this answer | |

You can use -c, --stdout, --to-stdout option of gunzip command

for example:

gunzip -c file.sql.gz | mysql -u root -p database
| improve this answer | |

To export in .sql.qz command is :-

mysqldump -u username -p database | gzip > database.sql.gz

To import the .sql.qz file command is:-

gunzip < database.sql.gz | mysql -u usrname -p newdatabase

Shailesh Thapa

| improve this answer | |

Lets say you need to populate user_data with mysql, try this:

export DEBIAN_FRONTEND=noninteractive \
    apt-get update -yq
    dpkg -l | grep mysql-server || (echo "Installing MySQL..." \
    && apt-get install -yq mysql-server \
    && echo -e "\n[mysqld]\nbind-address=\nskip-name-resolve=1" | tee -a /etc/mysql/my.cnf \
    && aws s3 cp --quiet s3://your-bucket/mysqldump_all_databases.sql.gz - | zcat | mysql -uroot\
    && systemctl restart mysql)
| improve this answer | |

On MacOS I've been using the following one-liner with no need of installing additional programs, except for the MySQL client itself.

$ cat /path/to/file.sql.gz | gzip -d | mysql -u root <db_name>

The first command, cat, prints the file. Its output, the file contents, is sent as the input to the next command, gzip. gzip with the the -d option decompresses the input passed to it and outputs the result, which is finally used as input for the MySQL client, the mysql program. The output -> input sending is brought to us by the | (pipe) operator on bash and other shell.

This script can also be used in some popular Linux distros, such as Ubuntu. I'm not sure whether gzip is always available. But it can be easily installed, if not, with:

$ sudo apt install gzip
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.