Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I received a huge database dump (21 GB) including a CREATE DATABASE and USE statement.

Now I want to import that dump - but into a different database.

This is how the dump looks like (line numbers added by me)

17: --
18: -- Create schema foo
19: --
20:
21: CREATE DATABASE IF NOT EXISTS foo;
22: USE foo;
23:
24: --  
share|improve this question
    

3 Answers 3

up vote 2 down vote accepted

Use head and tail to remove that part from the dump

head foodump.sql -n 16 > processed.sql
tail foodump.sql -n +23 >> processed.sql

And then import processed.sql to the database.

# mysql
mysql> CREATE mydatabase
mysql> exit
Bye
# mysql mydatabase < processed.sql
share|improve this answer

Just change the lines that say Create Database and use:

17: --
18: -- Create schema foo
19: --
20:
21: CREATE DATABASE IF NOT EXISTS foo;
22: USE foo;
23:
24: --  

to:

17: --
18: -- Create schema foo
19: --
20:
21: CREATE DATABASE IF NOT EXISTS DBNAMEYOUWANT;
22: USE DBNAMEYOUWANT;
23:
24: --  

where DBNAMEYOUWANT is the database name you want to create and do the imports to.

Additionaly you can use sed like:

sed -i "s/^CREATE\ DATABASE\ IF\ NOT\ EXISTS\ foo;/CREATE\ DATABASE\ IF\ NOT\ EXISTS\ DBNAMEYOUWANT;/g" /path/to/dump
sed -i "s/^USE\ foo;/USE\ DBNAMEYOUWANT;/g" /path/to/dump
share|improve this answer
    
The question is how to change that as the dump is too large for any text editor :-) –  Alex Sep 10 '12 at 15:36
    
I've added a line about using sed in this case so you don't have to open the file in a text editor. This should help. –  Logic Wreck Sep 10 '12 at 16:04
    
thanks :-) I think we also can combine that in one sed call .. so the huge dump does not have to be processed twice. –  Alex Sep 14 '12 at 13:44

You can manually remove that pair of lines and then just import it to an existing database.

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.