Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid "use <database>" statement on mysqldump backups?

Tags:

mysql

backup

I'm using this command syntax :

mysqldump -u<username> -p<password> --no-create-db --databases mydatabase > /var/backups_DB/MyDatabase-$(date +"%d-%m-%Y-%H:%M:%S").sql

But I could not find how to prevent the line "use mydatabase" to be inserted in the generated file. Which option should I use?

like image 745
Lano Avatar asked Jun 15 '15 00:06

Lano


2 Answers

Exactly. Remove --databases...

https://www.computerhope.com/unix/mysqldum.htm

--databases
-B
Dump several databases. Normally, mysqldump treats the first name argument on the command line as a database name and following names as table names. With this option, it treats all name arguments as database names. CREATE DATABASE and USE statements are included in the output before each new database.

like image 163
nink Avatar answered Oct 17 '22 18:10

nink


As @wchiquito said in comments, in order to avoid to have the "USE databasename" statement in dump file you must remove the --databases option.

If you don't have the choice, you can remove it afterwards using a Unix sed command :

cat sourcefile.sql | sed '/USE `databasename`;/d' > destfile.sql

Thomas

like image 6
tdaget Avatar answered Oct 17 '22 19:10

tdaget