MySQL system running status real-time monitoring (shell version)

When you start to contact MySQL, it is still different from Oracle. You need to accumulate and learn gradually. One difference is that Oracle has some data dictionaries that can display the operating status of the system, but you need to use SQL to retrieve them. In addition, AWR will have some operations. Status information, in contrast, MySQL provides some instructions, which can be displayed directly by executing it, which seems to be more convenient.


There are two ways for MySQL to display the system running status.


Method 1 : Log in to the database and execute the command show global status, as shown below,


Method 2 : Do not log in to the database, use the mysqladmin command, as shown below,

mysqladmin -uroot -p'My@sql' extended-status

The extended-status can be abbreviated with ext.


In order not to display the input password, it can be defined in the configuration file,

[mysqladmin]

host=localhost

user=root

password='My@sql'


Use the following command directly,

mysqladmin extended-status


If you do not use the default configuration file, you need to pay attention to the default parameters of mysqladmin, in the following order,

/etc/my.cnf /etc/mysql/my.cnf /var/mysql/etc/my.cnf ~/.my.cnf


Also need to use the following parameters,

--defaults-extra-file=# Read this file after the global files are read.


The instructions are as follows,

mysqladmin --defaults-extra-file=/DATA/mysql/my.cnf ext


Since the above instructions can help us understand the running status of the MySQL system, we naturally consider whether it can be automated, and almost any language can be used to implement the above instruction process. The following is a monitoring template implemented by shell scripts.


/* 使用awk,截出mysqladmin ext的回显,-i1表示1秒钟,自动刷新一次 */

mysqladmin --defaults-extra-file=/DATA/mysql/my.cnf ext -i1 | awk 'BEGIN{lswitch=0;


/* 打印信息表头 */

print "|QPS        |Commit     |Rollback   |TPS        |Threads_con  |Threads_run |";

print "------------------------------------------------------------------------------";}


/* 打印Queries、Com_commit、Com_rollback、Threads_connected、Threads_running这五个参数,前三个参数,是增量数据,因此需要记录上一次的值 */

$2 ~ /Queries$/ {q=$4-lq; lq=$4;}

$2 ~ /Com_commit$/ {c=$4-lc; lc=$4;}

$2 ~ /Com_rollback$/ {r=$4-lr; lr=$4;}

$2 ~ /Threads_connected$/ {tc=$4;}

$2 ~ /Threads_running$/ {tr=$4;


/* 设置lswitch的原因,为了打印10次出现一次表头 */

if (lswitch==0)

{lswitch=1;

count=0;}

else {


/* 打印10次数据,重新显示表头 */

if (count>10) {

count=0;

print "------------------------------------------------------------------------------";

print "|QPS        |Commit     |Rollback   |TPS        |Threads_con  |Threads_run |";

print "------------------------------------------------------------------------------";

} else {

count+=1;


/* 按照格式符进行打印,其中TPS值为Com_commit、Com_rollback的总和 */

printf "|%-10d |%-10d |%-10d |%-10d |%-12d |%-12d|", q,c,r,c+r,tc,tr;

}

}

}'


每隔1秒,刷新一次,


每隔10次,重新打印表头,


以上完整代码,可以从我的GitHub下载,

https://github.com/bisal-liu/oracle/blob/master/mysql_per_mon.sh



如果您觉得此篇文章对您有帮助,欢迎关注微信公众号:bisal的个人杂货铺,您的支持是对我最大的鼓励!共同学习,共同进步:)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325642306&siteId=291194637