简体   繁体   中英

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

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM