您好,登錄后才能下訂單哦!
#!/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
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。