31

I have a .gz sql dump file (example: foo.sql.gz) that i want import in my database with the classic mysql command.

gunzip -c foo.sql.gz > foo.sql

mysql -uroot -ppassword foo < foo.sql

foo is the database.

How can i pipe these two commands in a single one?

Tried

gunzip -c foo.sql.gz | mysql -uroot -ppassword foo

but doesn't seem to work; i get gzip: stdout: Broken pipe

3 Answers3

43
zcat foo.sql.gz | mysql -uroot -ppassword foo

This will also leave foo.sql.gz as it is.

Nifle
  • 34,998
14

For those on Max OSX there is a bug with zcat so you'll need to use gzcat instead.

gzcat foo.sql.gz | mysql -uroot -ppassword foo
SammyK
  • 241
11

All other answers are recommending writing the password in the command. This is a very bad practice and poses security risks. Please DON'T DO that.

You can leave the password empty in the command, than it will ask you to enter the password interactively. This way the password is not saved in the bash history.

gunzip < dump.sql.gz | mysql -u username -p databasename
Elin Y.
  • 208