標籤:int 自動 傳參數 date cas log arp admin miss
前言:新公司因為部分業務原因有好幾百組win機器裝MySQL授權登入比較麻煩,簡單的寫了一個shell傳值自動授權的指令碼,儲存複用。
#!/bin/bash#author [email protected]function helpfunc(){ echo -e "\033[36m Please check your parameters, -D for db name, -M main library IP, -I for authorized IP address options: -D Need to authorize the db name -M Execute on this IP -I Authorized IP -T Authorization type 1 is the program account, 2 read-only account, 3 administrator account ; you can specify multiple to ‘,‘ separation eg : mysqlha_winreplication.sh -D -M -I -T Generate 3 accounts: 1st: program link account. permissions: SELECT, INSERT, UPDATE, DELETE, EXECUTE, no DROP, CREATE; 2nd: read-only account. permissions: Select 3rd: administrator account. permissions: ALL \033[0m"}############################################type=2if [ $# -lt 1 ] ; then helpfunc exit 1else while getopts "M:D:I:T:" Option do case $Option in D) db=$OPTARG;; M) master_ip=$OPTARG;; I) grant_ip=$OPTARG;; T) type=$OPTARG;; *) helpfunc; exit 1; ;; esac donefitype=$(echo $type |tr ‘,‘ ‘ ‘)function GetPwd(){status=$1if [ $status -eq 0 ];then A=`head -c 500 /dev/urandom | tr -dc A-Z |head -c 1` B=`head -c 500 /dev/urandom | tr -dc [:alnum:]| head -c 6` C=`echo $RANDOM$RANDOM|cut -c 2` echo $A$B$Celse A=`head -c 500 /dev/urandom | tr -dc A-Z |head -c 1` B=`head -c 500 /dev/urandom | tr -dc [:alnum:]| head -c 12` C=`echo $RANDOM$RANDOM|cut -c 2` echo $A$B$Cfi} # echo -e "\033[42;37m \033[0m"function GetProgram(){pro_pwd=$(GetPwd 0)#ssh [email protected]${master_ip} "cd /cygdrive/d/data/mysqld_master && cmd /c sql.bat \"$sql\"##讀寫pro_sql="GRANT SELECT,INSERT,UPDATE,DELETE,EXECUTE ON ${db}.* TO ${db}@‘"localhost"‘ IDENTIFIED BY ‘"$pro_pwd"‘; "pro_sql1="GRANT SELECT,INSERT,UPDATE,DELETE,EXECUTE ON ${db}.* TO ${db}@‘"${grant_ip}"‘ IDENTIFIED BY ‘"$pro_pwd"‘; "for sql in "$pro_sql" "$pro_sql1"do if ssh [email protected]${master_ip} "cd /cygdrive/d/data/mysqld_master && cmd /c sql.bat \"$sql\" ";then : else echo -e "\033[31m To grant authorization{IP:${master_ip} 許可權:讀寫 } faild \033[0m" fidoneecho -e "\033[32m To grant authorization{IP:${master_ip} 許可權:讀寫 } sucess \033[0m"echo -e "\033[32m 讀寫賬戶:${db} \033[0m"echo -e "\033[32m 讀寫密碼:${pro_pwd} \033[0m"}function GetSelect(){select_pwd=$(GetPwd 1)##唯讀select_sql="GRANT SELECT ON ${db}.* TO ${db}[email protected]‘"localhost"‘ IDENTIFIED BY ‘"$select_pwd"‘;"select_sql1="GRANT SELECT ON ${db}.* TO ${db}[email protected]‘"${grant_ip}"‘ IDENTIFIED BY ‘"$select_pwd"‘;"for sql in "$select_sql" "$select_sql1"do if ssh [email protected]${master_ip} "cd /cygdrive/d/data/mysqld_master && cmd /c sql.bat \"$sql\" ";then : else echo -e "\033[31m To grant authorization{IP:${master_ip} 許可權:唯讀 } faild \033[0m" fi else echo -e "\033[31m To grant authorization{IP:${master_ip} 許可權:唯讀 } faild \033[0m" fidoneecho -e "\033[32m To grant authorization{IP:${master_ip} 許可權:唯讀 } sucess \033[0m"echo -e "\033[32m 唯讀賬戶:${db}_r \033[0m"echo -e "\033[32m 唯讀密碼:${select_pwd} \033[0m"}function GetAdmin(){admin_pwd=$(GetPwd 2)#管理員admin_sql="grant select,insert,update,delete,create,drop,alter,index,lock tables ,CREATE TEMPORARY TABLES ON ${db}.* TO ${db}[email protected]‘"localhost"‘ IDENTIFIED BY ‘"$admin_pwd"‘;"admin_sql="grant select,insert,update,delete,create,drop,alter,index,lock tables ,CREATE TEMPORARY TABLES ON ${db}.* TO ${db}[email protected]‘"127.0.0.1"‘ IDENTIFIED BY ‘"$admin_pwd"‘;"for sql in "$admin_sql" "$admin_sql1"do if ssh [email protected]${master_ip} "cd /cygdrive/d/data/mysqld_master && cmd /c sql.bat \"$sql\" ";then : else echo -e "\033[31m To grant authorization{IP:${master_ip} 許可權:管理員 } faild \033[0m" fidoneecho -e "\033[32m To grant authorization{IP:localhost 許可權:管理員 } sucess \033[0m"echo -e "\033[32m 管理員賬戶:${db}_admin \033[0m"echo -e "\033[32m 管理員密碼:${admin_pwd} \033[0m"}function main(){for stu in $typedo if [ $stu -eq 1 ];then GetProgram elif [ $stu -eq 2 ];then GetSelect elif [ $stu -eq 3 ];then GetAdmin else echo -e "\033[31m Please check if your type parameter is in range \033[0m" fidone}main
調用win sql.bat
@echo offset allparam=%1D:\data\mysqld_master\bin\mysql.exe -uroot -e %allparam%
or +port
@echo offset allparam=%2set port=%1echo %1echo %2D:\data\mysqld_slave\bin\mysql.exe -uroot -P%port% -e %allparam%
eg : mysqlha_winreplication.sh -D -M -I -T
Shell +Cygwinterminal+WinMySQL 傳參數授權