MySQLSRV管理脚本

分享一个自己写mysql起停脚本,方便测试环境多实例其他操作

[root@node2 ~]# cat /sbin/mysqlsrv 
#!/bin/bash

mysql_dir=/usr/local/mysql/
mysql_dat=/data
if [ "$2" = "" ] 
then
        port=3306
else
        port="$2"
fi

function mysql_process_cnt()
(ps -ef|grep -v "grep"|grep  mysql$port|wc -l)

function mysql_start()
(/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my$port.cnf &)

function mysql_kill()
(
#ps -ef|grep mysql|grep $port|awk -F' ' '{print $2}'|xargs kill -9
mysqladmin  -uroot -pshunzi -S /data/mysql$port/tmp/mysql$port.sock shutdown
)

function mysql_stat()
(mysqladmin  -uroot -pshunzi -S /data/mysql$port/tmp/mysql$port.sock  -r -i 1 ext |\
awk -F"|" \
"BEGIN{ count=0; }"\
'{ if($2 ~ /Variable_name/ && ((++count)%20 == 1)){\
    print "----------|---------|--- MySQL Command Status --|----- Innodb row operation ----|-- Buffer Pool Read --";\
    print "---Time---|---QPS---|select insert update delete|  read inserted updated deleted|   logical    physical";\
}\
else if ($2 ~ /Queries/){queries=$3;}\
else if ($2 ~ /Com_select /){com_select=$3;}\
else if ($2 ~ /Com_insert /){com_insert=$3;}\
else if ($2 ~ /Com_update /){com_update=$3;}\
else if ($2 ~ /Com_delete /){com_delete=$3;}\
else if ($2 ~ /Innodb_rows_read/){innodb_rows_read=$3;}\
else if ($2 ~ /Innodb_rows_deleted/){innodb_rows_deleted=$3;}\
else if ($2 ~ /Innodb_rows_inserted/){innodb_rows_inserted=$3;}\
else if ($2 ~ /Innodb_rows_updated/){innodb_rows_updated=$3;}\
else if ($2 ~ /Innodb_buffer_pool_read_requests/){innodb_lor=$3;}\
else if ($2 ~ /Innodb_buffer_pool_reads/){innodb_phr=$3;}\
else if ($2 ~ /Uptime / && count >= 2){\
  printf(" %s |%9d",strftime("%H:%M:%S"),queries);\
  printf("|%6d %6d %6d %6d",com_select,com_insert,com_update,com_delete);\
  printf("|%6d %8d %7d %7d",innodb_rows_read,innodb_rows_inserted,innodb_rows_updated,innodb_rows_deleted);\
  printf("|%10d %11d\n",innodb_lor,innodb_phr);\
}}')

case $1 in 
        start)
        if [ mysql_process_cnt -gt  0 ]
                then
                        echo 'Mysql Server is running!'
        else
                        mysql_start
                        echo 'Begin startup mysql...'
                        sleep 5;
                        if [ mysql_process_cnt -gt  0 ]
                        then
                                echo 'Mysql Server is startup!'
                        else
                                echo 'Mysql Server is startup error!'
                        fi
        fi
;;
        stop)
        if [ mysql_process_cnt -gt 0 ]
                then 
                        mysql_kill
                        echo 'Begin stop mysql...'
                        sleep 5;
                        if [ mysql_process_cnt -gt 0 ]
                        then
                                echo 'Mysql Server is stop error!'
                        else
                                echo 'Mysql Server is stop ok!'
                        fi
        else
                        echo 'Mysql Server is not run!'
        fi
;;
        cnt)
        echo mysql_process_cnt "$2" mysql$port
;;
        mysql)
        mysql -uroot -pshunzi -S /data/mysql$port/tmp/mysql$port.sock
;;
        stat)
        mysql_stat
;;
        *)
        echo "###  Usage:  ### default port 3306"
        echo "==================================================="
        echo "mysqlsrv start port #start the port's mysql server!"
        echo "mysqlsrv stop port  #stop  the port's mysql server!"
        echo "mysqlsrv cnt port   #view  the port's process info!"
        echo "mysqlsrv stat port  #view  the port's server status!"
        echo "==================================================="
;;
esac
继续阅读
广告也精彩
shunzi
  • 本文由 发表于 2018-02-0812:31:47
  • 除非特殊声明,本站文章均为原创,转载请务必保留本文链接
Oracle Spool数据 BLOG

Oracle Spool数据

昨天有个需求,让我导出数据到txt文本,数据有150w 数据库是oracle,试了下spool方式导出非常快: Oracle Spool常用设置 命令 描述 set arraysize 1000; 此...
Mysqld_multi Stop Bug Database

Mysqld_multi Stop Bug

最近在做mysqld_multi管理多实例的时候,发现stop不了节点 这是什么原因呢? 多实例相关配置如下: mysqld = /usr/local/mysql/bin/mysqld_safe my...
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: