删除 MySQL 中的所有数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22301635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Drop all databases in MySQL
提问by Sukhjinder Singh
We have many databases in our MySQL and we want to shrink/purge ibdata1 file in MySQL.
我们的 MySQL 中有很多数据库,我们想缩小/清除 MySQL 中的 ibdata1 文件。
How can we drop all the databases from the MySQL except information_schema
and mysqld
databases?
我们如何从 MySQL 中删除所有数据库,除了information_schema
和mysqld
数据库?
回答by Sukhjinder Singh
The following command drops all databases in the mysql dbms except mysql, information_schema and performance_schema dbs.
以下命令删除 mysql dbms 中除 mysql、information_schema 和 performance_schema dbs 之外的所有数据库。
mysql -uroot -p<password> -e "show databases" | grep -v Database | grep -v mysql| grep -v information_schema| gawk '{print "drop database `" "`;select sleep(0.1);"}' | mysql -uroot -p<password>
Thanks to Mohinish's Blog post
感谢 Mohinish 的博客文章
回答by Sheldon Juncker
If you have cross-database foreign keys, or database names using MySQL reserved words such as 'order', this variation of Sukhjinder Singh's answer will work.
如果您有跨数据库外键或使用 MySQL 保留字(例如“order”)的数据库名称,则 Sukhjinder Singh 的答案的这种变体将起作用。
mysql -u<user> -p<password> -e "show databases" | grep -v Database | grep -v mysql | grep -v information_schema | gawk '{print "SET FOREIGN_KEY_CHECKS = 0;drop database
" $1 ";select sleep(0.1);"}' | mysql -u<user> -p<password>
mysql -u<user> -p<password> -e "show databases" | grep -v Database | grep -v mysql | grep -v information_schema | gawk '{print "SET FOREIGN_KEY_CHECKS = 0;drop database
“$1”;select sleep(0.1);"}' | mysql -u<user> -p<password>
回答by Up_One
Follow this link- it demonstrate how to do this !
按照这个链接- 它演示了如何做到这一点!
The used scrip is :
使用的脚本是:
mysql -uroot -p -e "show databases" | grep -v Database |
grep -v mysql| grep -v information_schema| grep -v test |
grep -v OLD |gawk '{print "drop database " ";select sleep(0.1);"}' |
mysql -uroot -ppassword