[root@nginx-mysqldb home]# yum install mariadb mariadb-server -y
启动mariadb,并且设置开机启动
systemctl start mariadb
systemctl enable mariadb
检查进程是否启动
[root@nginx-mysqldb yum.repos.d]# ps aux|grep mysql查看mysql的进程
mysql 22539 0.7 2.3 1756668 88704 ? Ssl 10:48 0:00 /usr/libexec/mysqld --basedir=/usr
root 22611 0.0 0.0 12320 1076 pts/0 R+ 10:49 0:00 grep --color=auto mysql
[root@nginx-mysqldb yum.repos.d]#
[root@nginx-mysqldb yum.repos.d]# netstat -anplut|grep mysql
tcp6 0 0 :::3306 :::* LISTEN 22539/mysqld
[root@nginx-mysqldb yum.repos.d]# lsof -i:3306
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 22539 mysql 21u IPv6 236875 0t0 TCP *:mysql (LISTEN)
[root@nginx-mysqldb yum.repos.d]#
检查下selinu和防火墙是否关闭
[root@nginx-mysqldb yum.repos.d]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
[root@nginx-mysqldb yum.repos.d]# getenforce
Permissive
[root@nginx-mysqldb yum.repos.d]#
[root@zabbix-client-1 zabbix_agentd.d]# cat userparameter_mysql.conf
UserParameter=mysql.status[*],/etc/zabbix/zabbix_agentd.d/mysql_status.sh $1
UserParameter=mysql.ping,/usr/bin/mysqladmin -ucali -p'cali123456' -h localhost ping 2>/dev/null | grep -c alive
UserParameter=mysql.version,/usr/bin/mysql -V
UserParameter=mysql.bytes_sent,/etc/zabbix/zabbix_agentd.d/mysql_status.sh $1
一行一个对应的参数,可以添加很多行,也就是很多参数,方便zabbix-server和zabbix-agent之间沟通
UserParameter=mysql.bytes_sent 是zabbix-server后面采集数据传递的参数 mysql.bytes_sent
/etc/zabbix/zabbix_agentd.d/mysql_status.sh $1 是接受到mysql.bytes_sent后,zabbix-agent需要执行的命令
在服务器端使用zabbix-get采集数据的参数如下:
[root@zabbix-server-1 ~]# zabbix_get -s 192.168.1.141 -k mysql.status[Uptime]
44373
[root@zabbix-server-1 ~]#
mysql的采集数据的脚本
[root@zabbix-client-1 zabbix_agentd.d]# cat mysql_status.sh
#!/bin/bash
#Desc:zabbix 监控 MySQL 状态
#Date:2020-3-19
#by:cali
#主机
HOST="localhost"
#用户
USER="cali"
#密码
PASSWORD="cali123456"
#端口
PORT="3306"
#MySQL连接
CONNECTION="mysqladmin -h ${HOST} -u ${USER} -P ${PORT} -p${PASSWORD}"
if [ $# -ne "1" ];then
echo "arg error!"
fi
case $1 in
Uptime)
result=`${CONNECTION} status 2>/dev/null |awk '{print $2}'`
echo $result
;;
Questions)
result=`${CONNECTION} status 2>/dev/null |awk '{print $6}'`
echo $result
;;
Com_update)
result=`${CONNECTION} extended-status 2>/dev/null |grep -w "Com_update" |awk '{print $4}'`
echo $result
;;
Slow_queries)
result=`${CONNECTION} extended-status 2>/dev/null |grep -w "Slow_queries" |awk '{print $4}'`
echo $result
;;
Com_select)
result=`${CONNECTION} extended-status 2>/dev/null |grep -w "Com_select" |awk '{print $4}'`
echo $result
;;
Com_rollback)
result=`${CONNECTION} extended-status 2>/dev/null |grep -w "Com_rollback" |awk '{print $4}'`
echo $result
;;
Com_insert)
result=`${CONNECTION} extended-status 2>/dev/null |grep -w "Com_insert" |awk '{print $4}'`
echo $result
;;
Com_delete)
result=`${CONNECTION} extended-status 2>/dev/null |grep -w "Com_delete" |awk '{print $4}'`
echo $result
;;
Com_commit)
result=`${CONNECTION} extended-status 2>/dev/null |grep -w "Com_commit" |awk '{print $4}'`
echo $result
;;
Bytes_sent)
result=`${CONNECTION} extended-status 2>/dev/null |grep -w "Bytes_sent" |awk '{print $4}'`
echo $result
;;
Bytes_received)
result=`${CONNECTION} extended-status 2>/dev/null |grep -w "Bytes_received" |awk '{print $4}'`
echo $result
;;
Com_begin)
result=`${CONNECTION} extended-status 2>/dev/null |grep -w "Com_begin" |awk '{print $4}'`
echo $result
;;
Threads_connected)
result=`${CONNECTION} extended-status 2>/dev/null |grep -w "Threads_connected" |awk '{print $4}'`
echo $result
;;
*)
echo "Usage:$0(Uptime|Questions|Com_update|Slow_queries|Com_select|Com_rollback|Com_insert|Com_delete|Com_commit|Bytes_sent|Bytes_received|Com_begin|Threads_connected)"
;;
esac
[root@zabbix-client-1 zabbix_agentd.d]#
授予可执行权限
[root@nginx-mysqldb zabbix_agentd.d]# chmod +x mysql_status.sh
[root@nginx-mysqldb ~]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.3.17-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> grant all on *.* to 'cali'@'%' identified by 'cali123456';
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)]>
all 代表所有的权限 --》增删改查都可以
. 表示可以访问任何的库,任何库里的表
‘cali’@’%’ 用户名 %代表可以从任何地方连接过来
identified by ‘cali123456’ 设置密码为cali123456
采集数据的核心命令 mysqladmin
[root@nginx-mysqldb ~]# mysqladmin -h localhost -ucali -p'cali123456' -P 3306 version
mysqladmin Ver 9.1 Distrib 10.3.17-MariaDB, for Linux on x86_64
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Server version 10.3.17-MariaDB
Protocol version 10
Connection Localhost via UNIX socket
UNIX socket /var/lib/mysql/mysql.sock
Uptime: 27 min 50 sec
Threads: 8 Questions: 42 Slow queries: 0 Opens: 37 Flush tables: 1 Open tables: 31 Queries per second avg: 0.025
[root@nginx-mysqldb ~]#
刷新zabbix-agent服务,因为修改了配置文件
[root@nginx-mysqldb ~]# service zabbix-agent restart
Redirecting to /bin/systemctl restart zabbix-agent.service
[root@nginx-mysqldb ~]#
在zabbix-server这边测试,是否可以采集数据
[root@zabbix-server-1 ~]# zabbix_get -s 192.168.111.133 -k mysql.status[Uptime]
2754
[root@zabbix-server-1 ~]#
在zabbix-server服务器上的web界面添加监控主机和相关的监控项
自己创建应用集mysql-monitor,添加监控项,然后添加图形,出图
1.mysql是否正常启动
2.mysql接受的字节和发送的字节数
Bytes_sent
Bytes_received
3.有多少用户登录到mysql里
mysql.status[Threads_connected]
4.了解mysql查询的次数
Com_select
自己增加一个监控项目的流程:
1.在zabbix-agent上的userparameter_mysql.conf 里添加参数
UserParameter=mysql.processlist,/usr/bin/mysqladmin -h localhost -ucali -p’cali123456’ processlist|awk ‘{print $4}’|uniq |sort -rn|tail -n +2|wc -w
2.刷新zabbix-agent服务
3.zabbix-server上添加监控项–》出图
真正的难点就是在zabbix-agent上去采集数据的脚本或者命令–》可以使用shell脚本或者python脚本