shell连接mysql

连接数据库脚本(哈哈)

#!/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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值