删除多个名称与模式匹配的数据库 [英] Drop multiple databases with names matching a pattern
本文介绍了删除多个名称与模式匹配的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要删除所有以一个词开头的数据库.
I want to drop all the databases starting with a word.
abc
xyz
cms_db1
cms_db2
cms_xyz
pqr
在上面给出的示例中,我想删除所有以"cms"开头的数据库. 我猜maatkit或shell脚本可以做到.最好的方法是什么?
In the example given above, I will like to drop all the Databases starting with the word "cms". I guess maatkit or shell script can do it. What is the best approach?
推荐答案
Linux方式:
#!/bin/bash
DB_STARTS_WITH="cms"
MUSER="root"
MPWD="yourpass"
MYSQL="mysql"
DBS="$($MYSQL -u$MUSER -p$MPWD -Bse 'show databases')"
for db in $DBS; do
if [[ "$db" =~ "^${DB_STARTS_WITH}" ]]; then
echo "Deleting $db"
$MYSQL -u$MUSER -p$MPWD -Bse "drop database $db"
fi
done
当然,使用drop
部分的风险自负;)
Of course use the drop
part at your own risk ;)
这篇关于删除多个名称与模式匹配的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文