Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have an SQL dump of db1 that contains this line:

"USE db1"

If I import that file into db2 with:

mysql -D db2 -uroot -p < /var/backups/db1.sql.gz

Is there the risk that db1 will be overriden?

share|improve this question
Can manual rewriting the database name be an option? – dezso Nov 13 '12 at 9:24
Unfortunately that SQL dump is very big and I have no possibilities to modify it. – Mich Dart Nov 13 '12 at 9:26
Which operating system are you running on? If it's a unix variant, then a tool like sed should have no problems replacing allUSE db1 statements with USE db2 ones (might take a while, but it'll do it). – Stuart Moore Nov 13 '12 at 10:53
Even on Windows you can use a grep replacement for updating that file. See this SO question. – Marian Nov 13 '12 at 11:42
@Marian sed for Windows – dezso Nov 13 '12 at 13:44

By reading the official documentation, I'll say that your guess is right, the dump script will override the DB1 database.

MySQL Use db syntax

The USE db_name statement tells MySQL to use the db_name database as the default (current) database for subsequent statements. The database remains the default until the end of the session or another USE statement is issued.

The solution is the one proposed by Stuart, use a grep-like tool to find and replace "use db1" with "use db2" inside the dump file. Maybe even other references to db1 inside the dump script (procedures that use complete table names..). There are solutions for both Linux or Windows variants to edit that big text file.

share|improve this answer

Aside from using sed to remove the USE db1 statement, if you can take a new dump of the database:

mysqldump db1 > newdump.sql

should generate the dump without the USE db1 statement.

share|improve this answer

Your Answer


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.