因为公司项目紧急召唤,一个未上线的系统需要临时给领导演示,项目组负责人为了不影响演示效果,临时安排了我们几个同事对系统进行人肉监控。数据库层面主要监控数据库日志,数据库连接数,主从同步状态,系统资源负载等。
数据库连接数监控
# cat check_connects2.sh
#!/bin/bash
while true
do
sleep 5
date
result=$(mysql -uroot -ppwd -e "SHOW STATUS LIKE 'Threads_connected';" | awk '{print $2}')
echo "当前MySQL连接数为:$result"
done
数据库主从同步状态监控
# cat check_slave2.sh
#!/bin/bash
while true
do
sleep 5
date
bash /root/scripts/check_slave3.sh
done
# cat check_slave3.sh
#!/bin/bash
mysql -uroot -ppwd -e "show slave status\G" | grep -E 'Slave_IO_Running|Slave_SQL_Running' |grep -v Slave_SQL_Running_State
效果
# /root/scripts/check_connects2.sh
Sat Aug 17 16:09:44 CST 2024
mysql: [Warning] Using a password on the command line interface can be insecure.
当前MySQL连接数为:Value
146
Sat Aug 17 16:09:49 CST 2024
mysql: [Warning] Using a password on the command line interface can be insecure.
当前MySQL连接数为:Value
146
Sat Aug 17 16:09:54 CST 2024
mysql: [Warning] Using a password on the command line interface can be insecure.
当前MySQL连接数为:Value
146
# ./check_slave2.sh
Sat Aug 17 16:11:47 CST 2024
mysql: [Warning] Using a password on the command line interface can be insecure.
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Sat Aug 17 16:11:52 CST 2024
mysql: [Warning] Using a password on the command line interface can be insecure.
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Sat Aug 17 16:11:57 CST 2024
mysql: [Warning] Using a password on the command line interface can be insecure.
Slave_IO_Running: Yes
Slave_SQL_Running: Yes