本篇文章主要介绍了"使用脚本对mysql状态进行监控",主要涉及到方面的内容,对于MySql感兴趣的同学可以参考一下:
1、mysqladmin 使用mysqladmin extended-status命令可以获得所有MySQL性能指标,即show global status的输...
1、mysqladmin
使用mysqladmin extended-status命令可以获得所有MySQL性能指标,即show global status的输出,不过,因为多数这些指标都是累计值,如果想了解当前的状态,则需要进行一次差值计算,这就是mysqladmin extended-status的一个额外功能,非常实用。
默认的,使用extended-status,看到也是累计值,但是,加上参数-r(--relative),就可以看到各个指标的差值,配合参数-i(--sleep)就可以指定刷新的频率
如果是5.7可以对mysqladmin进行配置
[mysqldump]
user=root
password=123456
简单的命令
mysqladmin -r -i 1extended-status
监控脚本
#!/bin/bash
#author pingzhao1990@163.com
mysqladmin extended-status -i1|awk 'BEGIN{local_switch=0}
$2 ~ /Queries$/ {q=$4-lq;lq=$4;}
$2 ~ /com_commit$/ {c=$4-lc;lc=$4;}
$2 ~ /Com_rollback$/ {r=$4-lr;lr=$4;}
$2 ~ /Com_select$/ {s=$4-ls;ls=$4;}
$2 ~ /Com_update$/ {u=$4-lu;lu=$4;}
$2 ~ /Com_insert$/ {i=$4-li;li=$4;}
$2 ~ /Com_delete$/ {d=$4-ld;ld=$4;}
$2 ~ /Innodb_rows_read$/ {irr=$4-lirr;lirr=$4;}
$2 ~ /Innodb_rows_deleted$/ {ird=$4-lird;lird=$4;}
$2 ~ /Innodb_rows_inserted$/ {iri=$4-liri;liri=$4;}
$2 ~ /Innodb_rows_updated$/ {iru=$4-liru;liru=$4;}
$2 ~ /Innodb_buffer_pool_read_requests$/ {ibprr=$4-libprr;libprr=$4;}
$2 ~ /Innodb_buffer_pool_reads$/ {ibpr=$4-libpr;libpr=$4;}
$2 ~ /Threads_connected$/ {tc=$4;}
$2 ~ /Threads_running$/ {tr=$4;
if(local_switch==0)
{local_switch=1; count=16}
else {
if(count>15) {
count=0;
print "------------------------------------------------------------------------------------------------------------------------------------ ";
print "Time-----| QPS | Commit Rollback TPS | select insert update delete | read inserted updated deleted | logical physical | Tcon Trun";
print "------------------------------------------------------------------------------------------------------------------------------------ ";
}else{
count+=1;
printf "%s | %-5d| %-6d %-7d %-5d| %-7d %-7d %-5d %-6d| %-7d %-7d %-7d %-7d| %-6d %-9d| %-4d %-2d \n", strftime("%H:%M:%S"),q,c,r,c+r,s,u,i,d,irr,ird,iri,iru,ibprr,ibpr,tc,tr;
}
}
}'
输出结果如下
------------------------------------------------------------------------------------------------------------------------------------
Time-----| QPS | Commit Rollback TPS | select insert update delete | read inserted updated deleted | logical physical | Tcon Trun
------------------------------------------------------------------------------------------------------------------------------------
23:52:06 | 1 | 0 0 0 | 0 0 0 0 | 0 0 0 0 | 0 0 | 5 3
23:52:07 | 1 | 0 0 0 | 0 0 0 0 | 0 0 0 0 | 0 0 | 5 3
23:52:08 | 14 | 0 0 0 | 8 1 0 0 | 0 0 0 0 | 123 0 | 5 3
23:52:09 | 1 | 0 0 0 | 0 0 0 0 | 0 0 0 0 | 61 0 | 5 3
:52:10 | 1 | 0 0 0 | 0 0 0 0 | 0 0 0 0 | 0 0 | 5 3
23:52:10 | 1 | 0 0 0 | 0 0 0 0 | 0 0 0 0 | 0 0 | 5 3
23:52:12 | 21 | 0 0 0 | 2 1 0 0 | 1 0 0 1 | 9 0 | 5 3
23:52:13 | 21 | 0 0 0 | 15 1 0 0 | 0 0 0 0 | 2 0 | 5 3
23:52:14 | 1 | 0 0 0 | 0 0 0 0 | 0 0 0 0 | 0 0 | 5 3
23:52:15 | 4 | 0 0 0 | 1 0 0 0 | 0 0 0 0 | 0 0 | 5 3
23:52:16 | 140 | 0 0 0 | 123 1 0 0 | 115659 0 0 0 | 17473 0 | 5 3
23:52:17 | 54 | 0 0 0 | 30 3 0 0 | 71 0 0 0 | 53 0 | 5 3
23:52:18 | 17 | 0 0 0 | 6 2 1 0 | 76148 0 1 1 | 12262 0 | 6 3
23:52:19 | 25 | 0 0 0 | 8 4 4 0 | 192580 0 4 4 | 26304 0 | 6 3
23:52:20 | 35 | 0 0 0 | 11 6 6 0 | 264798 0 6 6 | 36177 0 | 5 3
23:52:21 | 47 | 0 0 0 | 16 7 7 0 | 382750 0 8 7 | 52543 0 | 7 4
2、show命令
附上:python2.7的安装
wget http://www.python.org/ftp/python/2.7.8/Python-2.7.8.tar.xz
xz -d Python-2.7.8.tar.xz
tar -xvf Python-2.7.8.tar
cd Python-2.7.8
./configure --prefix=/usr/local
make && make altinstall
# 检查 Python 版本:
python2.7 -V
export PATH="/usr/local/bin:$PATH"
#安装 setuptools
wget --no-check-certificate https://pypi.python.org/packages/source/s/setuptools/setuptools-1.4.2.tar.gz
tar -xvf setuptools-1.4.2.tar.gz
cd setuptools-1.4.2
# 使用 Python 2.7.8 安装 setuptools
python2.7 setup.py install
#安装 PIP
curl https://raw.githubusercontent.com/pypa/pip/master/contrib/get-pip.py | python2.7 -
修复 yum 工具
which yum
#修改 yum中的python 将第一行 #!/usr/bin/python 改为 #!/usr/bin/python2.6
pip install mysql-python
脚本如下