shell批量删除指定mysql中的表
今天在自己的测试服务器上搭建了个测试环境,但web和mysql之间连接上有点问题,我有点懒,不想把mysql中的这个测试库删除了重新创建和给权限,就只想把mysql中的表给删除了,网上到处找怎么只删除mysql的表,终于让我找到个用shell脚本就可以把指定mysql库中的表全部删除,下面是脚本内容:
cat /root/soft_shell/drop_mysql_table.sh
#!/bin/bash # 删除mysql中所有表 # 示例: # Usage: ./script user password dbnane # Usage: ./script user password dbnane server-ip # Usage: ./script user password dbnane mysql.nixcraft.in # --------------------------------------------------- MUSER="$1" MPASS="$2" MDB="$3" MHOST="localhost" [ "$4" != "" ] && MHOST="$4" # 设置命令路径 MYSQL=$(which mysql) AWK=$(which awk) GREP=$(which grep) # help if [ ! $# -ge 3 ] then echo "Usage: $0 {MySQL-User-Name} {MySQL-User-Password} {MySQL-Database-Name} [host-name]" echo "Drops all tables from a MySQL" exit 1 fi # 连接mysql数据库 $MYSQL -u $MUSER -p$MPASS -h $MHOST -e "use $MDB" &>/dev/null if [ $? -ne 0 ] then echo "Error - 用户名或密码无效,无法连接mysql数据库" exit 2 fi TABLES=$($MYSQL -u $MUSER -p$MPASS -h $MHOST $MDB -e 'show tables' | $AWK '{ print $1}' | $GREP -v '^Tables' ) # make sure tables exits if [ "$TABLES" == "" ] then echo "Error - 在数据库中 $MDB 未发现相关表" exit 3 fi # let us do it for t in $TABLES do echo "Deleting $t table from $MDB database..." $MYSQL -u $MUSER -p$MPASS -h $MHOST $MDB -e "drop table $t" done
有了这个脚本,对mysql表的清除就很方便,也方便我们重新对网站进行安装.
ps:http://www.ahlinux.com/shell/11853.html
ps:http://www.ahlinux.com/shell/11853.html
评论: