Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

share|improve this question
1  
FYI you really do not want to pass the mysql password on the command line. This leads to it being stored in ~/.bash_history for all to see. If you just put -p it will prompt you to enter your password which is much safer (and doesn't break the zcat pipe either) –  thefreeman Jan 21 '13 at 23:36
    
thank you, good hint –  apelliciari Jan 22 '13 at 8:59
    
You orignal command would work if gunzip < foo.sql.gz | mysql -uroot -ppassword foo –  Yada Feb 1 '13 at 20:49

2 Answers 2

up vote 19 down vote accepted
zcat foo.sql.gz | mysql -uroot -ppassword foo

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

share|improve this answer
1  
thanks, it works! –  apelliciari Mar 19 '12 at 13:35

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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