shell连接mysql

连接数据库脚本(哈哈)

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:作用是可以按退格键删除输入错误的字符

转载于:https://my.oschina.net/2726276586/blog/864243

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值