#!/bin/bash
#菜單
function menu(){
echo -e "\t\t\t************************"
echo -e "\t\t\t* 操作數(shù)據(jù)庫 *"
echo -e "\t\t\t************************"
echo -e "\t\t\t* 1、登錄數(shù)據(jù)庫 *"
echo -e "\t\t\t* 2、創(chuàng)建用戶或增加權(quán)限*"
echo -e "\t\t\t* 3、sql語句操作數(shù)據(jù)庫 *"
echo -e "\t\t\t* 4、更改用戶密碼 *"
echo -e "\t\t\t* 0、退出 *"
echo -e "\t\t\t************************"
}
#輸入mysql的用戶名和密碼
function input(){
echo -e "\t\t\t請登錄數(shù)據(jù)庫"
read -p "請輸入mysql用戶名:" user
read -s -p "請輸入mysql的密碼:" passwd
}
#判斷輸入的用戶名密碼是否正確
function panduan(){
mysql -u$user -p$passwd -e exit 1>/dev/null 2>&1
if [ $?!= 0 ]
then
echo -e "\n\t\t\t用戶名或密碼錯誤,請重新輸入!"
input
else
echo -e "\n\t\t\t數(shù)據(jù)庫登錄成功!"
fi
}
#創(chuàng)建用戶并授權(quán)
function createuser(){
echo -e "已存在的用戶如下,如果輸入的用戶已存在,則增加該用戶到權(quán)限"
mysql -u$user -p$passwd -e "select user,host from mysql.user;"
read -p "請輸入要創(chuàng)建的用戶名:" newuser
read -s -p "請輸入新用戶的密碼:" newpasswd
echo -e "請輸入此用戶的主機名(%代表所有的主機都能用此賬戶訪問數(shù)據(jù)庫,具體到某幾個主機名中間用英文的逗號隔開,如localhost,192.168.1.1):"
read hostname
echo -e "請輸入授予此用戶操作數(shù)據(jù)庫的權(quán)限(all代表全部,具體到某幾個權(quán)限中間用英文的逗號隔開,如create,select):"
read grant
echo -e "請輸入授予此用戶的訪問的庫和表的權(quán)限(*.*代表全部的庫和表,具體到某幾個庫中間用英文的逗號隔開,如database1.table1,database2.table2):"
read database
mysql -u$user -p$passwd -e "grant $grant on $database to '$newuser'@'$hostname' identified by '$newpasswd';" 2>/tmp/error.txt
if [ -s /tmp/error.txt ]
then
echo -e "\t\t數(shù)據(jù)庫的權(quán)限或庫名、表名輸入錯誤!"
else
echo -e "\n\t\t\t用戶創(chuàng)建成功!"
fi
}
#命令行操作數(shù)據(jù)庫
function orders(){
while true
do
echo -e "\n"
echo -e "請輸入mysql語句:"
read order
mysql -u$user -p$passwd -e "$order" 2>/dev/null
if [ $?!=0 ]
then
echo -e "\t\t\tsql語句輸入錯誤!"
fi
done
}
#修改密碼
function change(){
echo -e "數(shù)據(jù)庫中已存在的用戶如下:"
tuser=`mysql -u$user -p$passwd -e "select distinct User from mysql.user;"|awk '{print $1}'|sed -n '2,$p'`
echo $tuser
while true
do
echo -e "請輸入要修改的用戶名:"
read cuser
echo -e "請輸入新密碼"
read -s cpasswd
echo -e "請再次輸入新密碼"
read -s zpasswd
echo $tuser|grep $cuser>/tmp/tuser.txt
if [ "$cpasswd" = ""$zpasswd"" -a -s /tmp/tuser.txt ]
then
mysql -u$user -p$passwd <<EOF
update mysql.user set password=password('$cpasswd') where user='$cuser';
flush privileges;
EOF
echo -e "\t\t\t$cuser用戶的密碼修改成功!"
elif [ "$cpasswd" != ""$zpasswd"" ]
then
echo -e "\t\t\t兩次輸入的密碼不一致!"
else
echo -e "\t\t\t你輸入的用戶不存在!"
fi
done
}
while true;do
menu
read -p "請輸入數(shù)字(0~4):" i
case $i in
1)
while true
do
input
mysql -u$user -p$passwd 2>/tmp/mysql.txt
if [ -s /tmp/mysql.txt ]
then
echo -e "\n\t\t\t用戶名或密碼錯誤,請重新輸入!"
else
break
fi
done
;;
2)
input
panduan
createuser
;;
3)
input
orders
;;
4)
input
panduan
change
;;
0)
exit 0;;
*)
echo -e "\t\t\t請輸入有效數(shù)字!"
;;
esac
done

專注于為中小企業(yè)提供成都網(wǎng)站設(shè)計、
網(wǎng)站制作服務(wù),電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)
金州免費做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了近1000家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
網(wǎng)頁題目:shell循環(huán)和函數(shù)引用-創(chuàng)新互聯(lián)
本文來源:http://sd-ha.com/article12/shpdc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計、網(wǎng)站制作、面包屑導航、做網(wǎng)站、ChatGPT、Google
廣告
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源:
創(chuàng)新互聯(lián)