Today I had to load database on mysql. There`s nothing new or exciting about it, but I encountered few problems. First one is that dump was quite big (few gigabytes) and it was compressed with gzip. (G)Unzipping it simply to a file would take some time, waste space on disk and it wasn`t the right way
So here how I managed to decompress it and load it at the same time:
gunzip -c mydbdump.sql.gz|mysql -umyuser -pmypass mydb
But then I found another problem – in my dump there was hardcoded database name. I wasn`t recovering that database, but just wanted to load it to a diffrent one. Names weren`t the same so it failed to load. I looked at the begining of that file using head command, as opening so huge file in vi(m) would probably kill the server. I found that there was two sql commands that creates database itself and use it (sql use command). So with a little help of sed I managed to modify my command so it looked like this:
gunzip -c mydbdump.sql.gz| sed -e '1,30s/old_dbname/mydb/'| mysql -umyuser -pmypass mydb
I limited sed`s search&replace to first 30 lines, because database name was at the start of the dump file and I didn`t want to mess with the rest of the file