Practice: determine the number of connections of the current mysql user-group filtering # connets. sh #! Binsh # ocpyang@126.com # displays the number of connections of users in the corresponding user name or database name based on the input parameter u or d. # You can also enter the specific user name of u or the specific database name of d for further grouping and filtering # setmysqlevnMYSQL_USERsystem # mys
Practice: determine the number of connections of the current mysql user-group filtering # connets. sh #! /Bin/sh # ocpyang@126.com # according to the input parameter u or d to display the corresponding user name or the number of users in the database name. # You can also enter the specific user name of u or the specific database name of d for further grouping and filtering # set mysql evn MYSQL_USER = system # mys
Practice: determine the number of connections of the current mysql user-group Filtering
# Connets. sh
#! /Bin/sh
# Ocpyang@126.com
# The number of connections of the user name or user in the database name is displayed based on the input parameter u or d.
# You can also enter the specific user name of u or the specific database name of d for further grouping and filtering.
# Set mysql evn
MYSQL_USER = system # mysql Username
MYSQL_PASS = 'Password' # mysql logon User password
MYSQL_HOST = 192.168.2.188
Export black = '\ 033 [0m'
Export boldblack = '\ 033 [1; 0m'
Export red = '\ 033 [31m'
Export boldred = '\ 033 [1; 31m'
Export green = '\ 033 [32m'
Export boldgreen = '\ 033 [1; 32m'
Export yellow = '\ 033 [33m'
Export boldyellow = '\ 033 [1; 33m'
Export blue = '\ 033 [34m'
Export boldblue = '\ 033 [1; 34m'
Export magenta = '\ 033 [35m'
Export boldmagenta = '\ 033 [1; 35m'
Export cyan = '\ 033 [36m'
Export boldcyan = '\ 033 [1; 36m'
Export white = '\ 033 [37m'
Export boldwhite = '\ 033 [1; 37m'
Cecho ()
# -- Function to easliy print colored text --##
# Color-echo.
# Parameter $1 = message
# Parameter $2 = color
{
Local default_msg = "No message passed ."
Message =$ {1:-$ default_msg} # If $1 is not input, the default value is default_msg.
Color = $ {2:-black} # If $1 is not input, the default value is black.
Case $ color in
Black)
Printf "$ black ";;
Boldblack)
Printf "$ boldblack ";;
Red)
Printf "$ red ";;
Boldred)
Printf "$ boldred ";;
Green)
Printf "$ green ";;
Boldgreen)
Printf "$ boldgreen ";;
Yellow)
Printf "$ yellow ";;
Boldyellow)
Printf "$ boldyellow ";;
Blue)
Printf "$ blue ";;
Boldblue)
Printf "$ boldblue ";;
Magenta)
Printf "$ magenta ";;
Boldmagenta)
Printf "$ boldmagenta ";;
Cyan)
Printf "$ cyan ";;
Boldcyan)
Printf "$ boldcyan ";;
White)
Printf "$ white ";;
Boldwhite)
Printf "$ boldwhite ";;
Esac
Printf "% s \ n" "$ message"
Tput sgr0 # tput sgr0 restores the default value
Printf "$ black"
Return
}
Cechon ()
# Color-echo.
# Parameter 1 $1 = message
# Parameter 2 $2 = color
{
Local default_msg = "No message passed ."
# Doesn't really need to be a local variable.
Message =$ {1:-$ default_msg} # If $1 is not input, the default value is default_msg.
Color = $ {2:-black} # If $1 is not input, the default value is black.
Case $ color in
Black)
Printf "$ black ";;
Boldblack)
Printf "$ boldblack ";;
Red)
Printf "$ red ";;
Boldred)
Printf "$ boldred ";;
Green)
Printf "$ green ";;
Boldgreen)
Printf "$ boldgreen ";;
Yellow)
Printf "$ yellow ";;
Boldyellow)
Printf "$ boldyellow ";;
Blue)
Printf "$ blue ";;
Boldblue)
Printf "$ boldblue ";;
Magenta)
Printf "$ magenta ";;
Boldmagenta)
Printf "$ boldmagenta ";;
Cyan)
Printf "$ cyan ";;
Boldcyan)
Printf "$ boldcyan ";;
White)
Printf "$ white ";;
Boldwhite)
Printf "$ boldwhite ";;
Esac
Printf "% s" "$ message"
Tput sgr0 # tput sgr0 restores the default value
Printf "$ black"
Return
}
If ["$ #"-lt 1]; then
Echo "**********************************"
Echo "you must input paraters"
Echo "**********************************"
Echo "USAGE01: $0 d | $0 d database_name"
Echo "eg01: $0 d | $0 d mysql"
Echo "USAGE02: $0 u | $0 u username"
Echo "eg02: $0 u | $0 u wind"
Exit 1;
Fi
# Case conversion
Ipt = 'echo $1 | tr' [a-z] ''[A-Z]''
# Source/usr/local/mysql/scripts/mysql_env.ini
Logfiledate_init = "tmpinit. 'date when policyymm1_d1_h1_m1_s'.txt"
Logfiledate_midd = "tmpmidd. 'date too many yymm1_d1_h1_m1_s'.txt"
Judegedate_01 = "judegedate01. 'date too many yymm1_d1_hsf-m=s'.txt"
Judegedate_02 = "judegedate02. 'date parameter policyymm1_d1_h1_m1_s'.txt"
Mysql-h $ {MYSQL_HOST}-u $ {MYSQL_USER}-p $ {MYSQL_PASS}-e "show processlist;" >$ {logfiledate_init}
If ["$ #"-eq 1]; then
If ["$ ipt" = 'D']; then
Awk '{tt [$4] ++} BEGIN {printf "%-20 s %-20s \ n", "dbname", "connect ";} END {for (I in tt) printf "%-20 s %-20s \ n", I, tt [I]} '$ {logfiledate_init} | grep-v "NULL"
Elif ["$ ipt" = 'U']; then
Awk '{tt [$2] ++} BEGIN {printf "%-20 s %-20s \ n", "username", "connect ";} END {for (I in tt) printf "%-20 s %-20s \ n", I, tt [I]} '$ {logfiledate_init} | grep-v "NULL"
Else
Cechon "input error! "Red
Echo ""
Fi
Elif ["$ #"-eq 2]; then
Grep-I $2 $ {logfiledate_init} >$ {logfiledate_midd}
If ["$ ipt" = 'D']; then
SCHEMA_JUDEGE01 = "select schema_name from information_schema.schemata where schema_name = '$2 ';"
Mysql-h $ {MYSQL_HOST}-u $ {MYSQL_USER}-p $ {MYSQL_PASS}-e "$ {SCHEMA_JUDEGE01}" >$ {judegedate_01}
If [! -S "$ {judegedate_01}"]; then
Cechon "you input schema_name $2 not exits, pleae check your schema_name" red
Rm-rf $ {SCHEMA_JUDEGE01}
Exit 0
Else
Awk '{tt [$4] ++} BEGIN {printf "%-20 s %-20s \ n", "dbname", "connect ";} END {for (I in tt) printf "%-20 s %-20s \ n", I, tt [I]} '$ {logfiledate_midd} | grep-v "NULL"
Fi
Elif ["$ ipt" = 'U']; then
SCHEMA_JUDEGE02 = "select user from mysql. user where user = '$2 ';"
Mysql-h $ {MYSQL_HOST}-u $ {MYSQL_USER}-p $ {MYSQL_PASS}-e "$ {SCHEMA_JUDEGE02}" >$ {judegedate_02}
If [! -S "$ {judegedate_02}"]; then
Cechon "you input username $2 not exits, pleae check your user_name" red
Rm-rf $ {SCHEMA_JUDEGE02}
Exit 0
Else
Awk '{tt [$2] ++} BEGIN {printf "%-20 s %-20s \ n", "username", "connect ";} END {for (I in tt) printf "%-20 s %-20s \ n", I, tt [I]} '$ {logfiledate_midd} | grep-v "NULL"
Fi
Else
Cechon "input error! "Red
Echo ""
Fi
Fi
# Clearing temporary files
Rm-rf $ {logfiledate_init}
Rm-rf $ {logfiledate_midd}
Rm-rf $ {judegedate_01}
Rm-rf $ {judegedate_02}