超實用--刪除MYSQL中指定的資料的全部表

來源:互聯網
上載者:User

標籤:style   http   color   os   strong   ar   for   資料   art   

作過的人都知道,重複測試資料庫的苦惱。

用法:# Usage: ./script user password dbnane mysql.nixcraft.in

~~~~~~~~~~~~~

#!/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中指定的資料的全部表

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.