连接数据库脚本(哈哈)
#!/bin/bash
#version:v2.0
#Date:2017-04-20
#作用:进入rds使用
sqlCommand=/usr/local/data/mysql/bin/mysql
writeAddr=
read1Addr=
read2Addr=
tongjiAddr=
userWeb=
userTongji=
userPassweb=
userPasstongji=
sqlWrite="$sqlCommand -h$writeAddr -u$userWeb -p$userPassweb"
sqlRead1="$sqlCommand -h$read1Addr -u$userWeb -p$userPassweb"
sqlRead2="$sqlCommand -h$read2Addr -u$userWeb -p$userPassweb"
sqlTongji="$sqlCommand -h$tongjiAddr -u$userTongji -p$userPasstongji"
function write ()
{
$sqlWrite
}
function read1 ()
{
$sqlRead1
}
function read2 ()
{
$sqlRead2
}
function tongji ()
{
$sqlTongji
}
function sum ()
{
write=`$sqlWrite -e "show processlist"|wc -l`
read1=`$sqlRead1 -e "show processlist"|wc -l`
read2=`$sqlRead2 -e "show processlist"|wc -l`
tongji=`$sqlTongji -e "show processlist"|wc -l`
echo -e "\033[31m数据库连接数\n\033[32mwrite:$write\nread1:$read1\nread2:$read2\ntongji:$tongji\033[0m"
}
function killsql ()
{
if [ "$1" == "write" ]
then
for i in `$sqlWrite -e "show processlist"|sed 1d|grep -i "$2"|cut -f 1`;do $sqlWrite -e "kill $i";done
elif [ "$1" == "read1" ]
then
for i in `$sqlRead1 -e "show processlist"|sed 1d|grep -i "$2"|cut -f 1`;do $sqlRead1 -e "kill $i";done
elif [ "$1" == "read2" ]
then
for i in `$sqlRead2 -e "show processlist"|sed 1d|grep -i "$2"|cut -f 1`;do $sqlRead2 -e "kill $i";done
elif [ "$1" == "tongji" ]
then
for i in `$sqlTongji -e "show processlist"|sed 1d|grep -i "$2"|cut -f 1`;do $sqlTongji -e "kill $i";done
else
echo -e "\033[31meg:\n\t\033[32mbash $0 killsql read4 select\033[0m"
fi
}
function help ()
{
echo -e "\033[31mUsage:\n\t\033[32m$0 write|read1|read2|tongji|sum|killsql|help\033[0m"
echo -e "\033[31meg:\n\t\033[32mbash $0 write\n\tbash $0 sum\n\tbash $0 killsql write select\033[0m"
}
case $1 in
write)
write
;;
read1)
read1
;;
read2)
read2
;;
tongji)
tongji
;;
sum)
sum
;;
killsql)
killsql $2 $3
;;
help)
help
;;
*)
echo -e "\033[31mUsage:\n\t\033[32mbash $0 (write|read1|read2|tongji|sum|killsql|help)\033[0m"
echo -e "\033[31meg:\n\t\033[32mbash $0 write\n\tbash $0 sum\n\tbash $0 killsql write select\033[0m"
;;
esac