我有一台拥有300多个数据库的服务器(SQL Server2005)。我不想逐个右键单击并选择Delete。
如何轻松删除所有数据库?
发布于 2015-12-09 23:50:00
我的数据库出现了一个问题,上面的解决方案都不起作用。
我只是想删除我所有的数据库,但我有一个问题,他们的名字。实际上,我有这样命名的数据库:
093e83d-somename;
39s2ak3-anothername;删除这些数据库(在MariaDB上测试)的更简单方法是执行以下命令:
DROP DATABASE `093e83d-somename`;当我们直接想要从bash文件执行SQL命令时,这种名称似乎是一个问题,因为我们必须在反引号()之间指定数据库的名称。
如果您有相同的问题,并且您有很多数据库,那么您只需创建一个包含所有所需命令的批处理脚本,然后在您的SQL服务器中执行此脚本。
test.sh示例:
#!/bin/bash
# Informations needed
MUSER="root"
MPASS="pass"
# We get the needed binaries
MYSQL=$(which mysql)
AWK=$(which awk)
GREP=$(which grep)
# We get all the DB names in DB
DB=$($MYSQL -u $MUSER -p$MPASS -e 'show databases' | $AWK '{ print $1}' | $GREP -v '^Databases' )
# For each database, we write the drop command in the file test.sql
for t in $DB
do
echo -e "DROP DATABASE \`$t\`;" >> test.sql
done
# We execute the created SQL file with all the DROP commands
$MYSQL -u $MUSER -p$MPASS -e 'source test.sql;'
# We finally delete the created file.
rm test.sql我认为这个脚本在所有情况下都是有效的。希望这能帮上忙。
https://stackoverflow.com/questions/5777483
复制相似问题