MySQL 数据库监控

MySQL 数据库监控

可用性监控

1.网络监控

使用mysqladmin:

[root@mysql10 mysql]# mysqladmin -uroot -p  ping
Enter password: 
mysqld is alive

使用telnet:
成功则返回:

[root@mysql10 mysql]# telnet localhost 3306
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
N

失败则返回:

[root@mysql10 mysql]# telnet 10.104.103.2 3306
Trying 10.104.103.2...
telnet: connect to address 10.104.103.2: No route to host

2.连接数

查看设置最大连接数:

[root@mysql10 mysql]# mysql -uroot -p -e "show variables like 'max_connections'"
Enter password: 
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| max_connections | 1000  |
+-----------------+-------+

查看当前连接数:

[root@mysql10 mysql]# mysql -uroot -p -e "show global status like 'Threads_connected'"
Enter password: 
+-------------------+-------+
| Variable_name     | Value |
+-------------------+-------+
| Threads_connected | 54    |
+-------------------+-------+

性能监控

1.QPS

QPS,即每秒数据库执行的请求数量。

公式为:

QPS=(Queries2-Queries1)/(Uptime_since_flush_status2-Uptime_since_flush_status1)

2.TPS

TPS,即每秒数据库处理的事务数量。

公式为:

TPS=((Com_insert2+Com_update2+Com_delete2)-(Com_insert1+Com_update1+Com_delete1))/(Uptime_since_flush_status2-Uptime_since_flush_status1)

3.并发请求

查看数据库并发请求数量:

[root@mysql10 mysql]# mysql -uroot -p -e "show global status like 'Threads_running'"
Enter password: 
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| Threads_running | 1     |
+-----------------+-------+

4.Innodb阻塞

可使用下面脚本查看锁住的线程,脚本会返回所有阻塞时间大于10s的线程:

SELECT b.trx_mysql_thread_id AS '被阻塞线程'
      ,b.trx_query AS '被阻塞SQL'
	  ,c.trx_mysql_thread_id AS '阻塞线程'
	  ,c.trx_query AS '阻塞SQL'
	  ,(UNIX_TIMESTAMP()-UNIX_TIMESTAMP(c.trx_started)) AS '阻塞时间'
FROM information_schema.innodb_lock_waits a
JOIN information_schema.innodb_trx b ON a.requesting_trx_id=b.trx_id
JOIN information_schema.innodb_trx c ON a.blocking_trx_id=c.trx_id
WHERE (UNIX_TIMESTAMP()-UNIX_TIMESTAMP(c.trx_started))>10

查看连接号:

mysql> select connection_id();
+-----------------+
| connection_id() |
+-----------------+
|          382666 |
+-----------------+
1 row in set (0.00 sec)

主从复制监控

1.链路状态

使用show slave status命令查看:

Slave_IO_Running: Yes
Slave_SQL_Running: Yes

2.主从复制延迟

使用show slave status命令查看:

Seconds_Behind_Master: 0

再检查主从gtid是否同步:

主库:

mysql> show master status\G
*************************** 1. row ***************************
             File: mysqlbin.000875
         Position: 194
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: c94e3c22-72eb-11e9-b569-005056a86449:1-78241
1 row in set (0.00 sec)

从库:

Master_Log_File: mysqlbin.000875
Read_Master_Log_Pos: 194
Master_UUID: c94e3c22-72eb-11e9-b569-005056a86449
Retrieved_Gtid_Set: c94e3c22-72eb-11e9-b569-005056a86449:9-78241
Executed_Gtid_Set: c94e3c22-72eb-11e9-b569-005056a86449:8-78241

说明主从同步

3.主从一致性检测

使用pt-table-checksum工具,在主库上运行:

pt-table-checksum u=root,p='xxx' \
  --databases mysql \
  --replicate test.checksums
发布了136 篇原创文章 · 获赞 58 · 访问量 36万+

猜你喜欢

转载自blog.csdn.net/sunbocong/article/details/92839518