连接数据库脚本(哈哈)
vim conn_mysql.sh
#!/bin/bash
#date:2017-03-21
#
function she () #函数定义第一个数据库
{
db_write=写库地址
db_read=读库地址
db_tongji=统计库地址
db_user=用户名
db_pass=密码1
db_pass2=密码2
}
function bao () #函数定义第二个数据库
{
db_write=写库地址
db_read=读库地址
db_tongji=统计库地址
db_user=用户名
db_pass=密码1
db_pass2=密码2
}
function two () #定义数据库操作类型具体执行内容
{
MYSQLCMD=/usr/local/data/mysql/bin/mysql
###
if [ "$b" = write ]
then
$MYSQLCMD -h$db_write -u$db_user -p$db_pass
elif [ "$b" = read ]
then
$MYSQLCMD -h$db_read -u$db_user -p$db_pass
elif [ "$b" = tongji ]
then
$MYSQLCMD -h$db_tongji -u$db_user -p$db_pass2
elif [ "$b" = num ]
then
write_num=`$MYSQLCMD -h$db_write -u$db_user -p$db_pass -e "show processlist;"|wc -l`
read_num=`$MYSQLCMD -h$db_read -u$db_user -p$db_pass -e "show processlist;"|wc -l`
tongji_num=`$MYSQLCMD -h$db_tongji -u$db_user -p$db_pass2 -e "show processlist;"|wc -l`
echo -e "\033[31m数据库连接数如下:\033[0m"
echo -e "\033[31mwrite: \033[32m$write_num\033[0m"
echo -e "\033[31mread: \033[32m$read_num\033[0m"
echo -e "\033[31mtongji: \033[32m$tongji_num\033[0m"
else
:
fi
}
function one () #定义数据库操作类型
{
while true
do
stty erase '^H'
read -p "$a>>" b
if [ "$b" = ? -o "$b" = help -o "$b" = h ]
then
echo -e "\033[31mUsage:\n\033[32mwrite\nread\ntongji\nnum\033[0m"
elif [ "$b" = q -o "$b" = Q -o "$b" = exit -o "$b" = quit ]
then
echo "googbye $b"
break
elif [ "$b" = write ]
then
two
elif [ "$b" = read ]
then
two
elif [ "$b" = tongji ]
then
two
elif [ "$b" = num ]
then
two
elif [ "$b" = clear -o "$b" = c -o "$b" = "cls" ]
then
clear
else
:
fi
done
}
while true
do
stty erase '^H'
read -p "db>>" a
if [ "$a" = q -o "$a" = q -o "$a" = exit -o "$a" = quit ]
then
echo "goodbye"
break
elif [ "$a" = ? -o "$a" = help -o "$a" = h ]
then
echo -e "\033[31mUsage: $0\033\0m\n\033[32m699pic\n888pic\033[0m"
elif [ "$a" = 699pic ]
then
she
one
elif [ "$a" = 888pic ]
then
bao
one
elif [ "$a" = clear -o "$a" = c -o "$a" = "cls" ]
then
clear
else
:
fi
done
#stty erase ^H:作用是可以按退格键删除输入错误的字符