Zabbix分布式监控实战(二)——CentOS 7.5安装Zabbix-agent 5.0监控Mysql数据库

传送门 ---> Zabbix 5.0监控平台搭建与被监控端的添加

一、被监控端Mysql主机安装Zabbix-agent

永久关闭防火墙

[root@localhost ~]# systemctl stop firewalld

[root@localhost ~]# systemctl disable firewalld     //永久关闭防火墙

Zabbix-agent 5.0下载

注意:
 如果下载的zabbix-agent 5.0安装包使用rpm安装时提示“错误:依赖检测失败:libpcre.so.0()(64bit)”,则可以使用zabbix 5.0 Yum源来安装zabbix-agent 5.0软件。
----> 传送门 安装zabbix-agent5.0 报错解决

1.安装Zabbix Yum源仓库

[root@localhost ~]# rpm -Uvh https://repo.zabbix.com/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm

[root@localhost ~]# yum clean all

使用Zabbix 5.0 Yum源文件安装zabbix-agent 5.0软件,并且将gpgcheck=1修改为gpgcheck=0,防止出现“获取GPG密钥失败”报错。

2.修改Zabbix 5.0 Yum源配置文件

[root@localhost ~]# sed -i "s/gpgcheck=1/gpgcheck=0/g" /etc/yum.repos.d/zabbix.repo

[root@localhost ~]# cat /etc/yum.repos.d/zabbix.repo
[zabbix]
name=Zabbix Official Repository - $basearch
baseurl=http://repo.zabbix.com/zabbix/5.0/rhel/7/$basearch/
enabled=1
gpgcheck=0          //修改gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591

[zabbix-frontend]
name=Zabbix Official Repository frontend - $basearch
baseurl=http://repo.zabbix.com/zabbix/5.0/rhel/7/$basearch/frontend
enabled=1
gpgcheck=0        //修改gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591

[zabbix-debuginfo]
name=Zabbix Official Repository debuginfo - $basearch
baseurl=http://repo.zabbix.com/zabbix/5.0/rhel/7/$basearch/debuginfo/
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
gpgcheck=0        //修改gpgcheck=0

[zabbix-non-supported]
name=Zabbix Official Repository non-supported - $basearch
baseurl=http://repo.zabbix.com/non-supported/rhel/7/$basearch/
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX
gpgcheck=0        //修改gpgcheck=0

[root@localhost ~]# yum -y install zabbix-agent     //安装zabbix-agent 5.0软件
...
已安装:
  zabbix-agent.x86_64 0:5.0.0-1.el7

完毕!

3.修改zabbix-agent配置文件

[root@localhost ~]# vim /etc/zabbix/zabbix_agentd.conf
119 Server=127.0.0.1,192.168.2.128   //Zabbix-server地址,多个用","隔开
160 ServerActive=192.168.2.128     //主动检查本主机数据发送给Zabbix-server
171 #Hostname=Zabbix server
172 Hostname=192.168.2.129     //客户端IP地址

[root@localhost ~]# systemctl start zabbix-agent
[root@localhost ~]# systemctl enable zabbix-agent

[root@localhost ~]# ss -antulp | grep :10050
tcp    LISTEN     0      128       *:10050                 *:*                   users:(("zabbix_agentd",pid=3389,fd=4),("zabbix_agentd",pid=3388,fd=4),("zabbix_agentd",pid=3387,fd=4),("zabbix_agentd",pid=3386,fd=4),("zabbix_agentd",pid=3385,fd=4),("zabbix_agentd",pid=3384,fd=4))
tcp    LISTEN     0      128      :::10050                :::*                   users:(("zabbix_agentd",pid=3389,fd=5),("zabbix_agentd",pid=3388,fd=5),("zabbix_agentd",pid=3387,fd=5),("zabbix_agentd",pid=3386,fd=5),("zabbix_agentd",pid=3385,fd=5),("zabbix_agentd",pid=3384,fd=5))

在这里插入图片描述
在这里插入图片描述

二、Zabbix-agent端配置

1.下载Mysql 5.7 Yum源

Mysql下载

[root@localhost ~]# wget https://repo.mysql.com//mysql57-community-release-el7-11.noarch.rpm

[root@localhost ~]# ls mysql57-community-release-el7-11.noarch.rpm 
mysql57-community-release-el7-11.noarch.rpm

2.安装Mysql 5.7 Yum源

[root@localhost ~]# rpm -ivh mysql57-community-release-el7-11.noarch.rpm

[root@localhost ~]# yum -y install mysql-server    //安装Mysql

3.启动Mysqld服务

[root@localhost ~]# systemctl start mysqld
[root@localhost ~]# systemctl enable mysqld

[root@localhost ~]# ss -antulp | grep :3306
tcp    LISTEN     0      80     [::]:3306               [::]:*                   users:(("mysqld",pid=54086,fd=21))

4.查看初始密码

[root@localhost ~]# grep 'root@localhost:' /var/log/mysqld.log
2020-03-20T00:57:34.072739Z 1 [Note] A temporary password is generated for root@localhost: hsQ(7(so_cgX

5.使用初始密码登陆

[root@localhost ~]# mysql -hlocalhost -uroot -p'hsQ(7(so_cgX'
mysql: [Warning] Using a password on the command line interface can be insecure.
...
Server version: 5.7.29
...
mysql>

6.修改登陆密码

mysql> alter user root@"localhost" identified by "123qqq...A";

7.使用新密码登陆

[root@localhost ~]# mysql -uroot -p123qqq...A

8.授权Zabbix用户,用于发送数据

[root@localhost ~]# mysql -uroot -p123qqq...A
...
mysql> grant all on *.* to zabbix@"localhost" identified by "123qqq...A";
Query OK, 0 rows affected, 1 warning (0.03 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.11 sec)

9.测试使用授权的zabbix用户登录数据库

[root@localhost ~]# mysql -uzabbix -p123qqq...A
...
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1                |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
6 rows in set (0.00 sec)

mysql> select user();
+------------------+
| user()           |
+------------------+
| zabbix@localhost |
+------------------+
1 row in set (0.00 sec)

10.查看mysql的sock文件路径

[root@localhost ~]# find / -name *.sock
...
/var/lib/mysql/mysql.sock

11.查找并拷贝Zabbix监控Mysql模板文件

[root@test2 ~]# find / -name user*_mysql.conf
/usr/share/doc/zabbix-agent-5.0.0/userparameter_mysql.conf

[root@localhost ~]#cp /usr/share/doc/zabbix-agent-5.0.0/userparameter_mysql.conf /etc/zabbix/zabbix_agentd.d/

[root@localhost ~]#chown -R zabbix:zabbix /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf

12.查看Zabbix自带的监控模板文件

由配置文件可以知道,在Linux系统中需要在zabbix-agent home目录中创建.my.cnf(默认情况下为/var/lib/zabbix),并且文件必须以下有三个字符串:

  • [client]
  • user=zbx_monitor
  • password=<password>
[root@test2 ~]# cat /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf
#template_db_mysql.conf created by Zabbix for "Template DB MySQL" and Zabbix 4.2
#For OS Linux: You need create .my.cnf in zabbix-agent home directory (/var/lib/zabbix by default)
#For OS Windows: You need add PATH to mysql and mysqladmin and create my.cnf in %WINDIR%\my.cnf,C:\my.cnf,BASEDIR\my.cnf https://dev.mysql.com/doc/refman/5.7/en/option-files.html
#The file must have three strings:
#[client]
#user=zbx_monitor
#password=<password>
#
UserParameter=mysql.ping[*], mysqladmin -h"$1" -P"$2" ping
UserParameter=mysql.get_status_variables[*], mysql -h"$1" -P"$2" -sNX -e "show global status"
UserParameter=mysql.version[*], mysqladmin -s -h"$1" -P"$2" version
UserParameter=mysql.db.discovery[*], mysql -h"$1" -P"$2" -sN -e "show databases"
UserParameter=mysql.dbsize[*], mysql -h"$1" -P"$2" -sN -e "SELECT SUM(DATA_LENGTH + INDEX_LENGTH) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='$3'"
UserParameter=mysql.replication.discovery[*], mysql -h"$1" -P"$2" -sNX -e "show slave status"
UserParameter=mysql.slave_status[*], mysql -h"$1" -P"$2" -sNX -e "show slave status"

13.在/var/lib/zabbix目录下创建.my.cnf配置文件

[root@localhost ~]# vim /var/lib/zabbix/.my.cnf
[mysql]
host=localhost
user=zabbix           //数据库中授权的zabbix用户
password=123qqq...A     //授权用户密码
socket=/var/lib/mysql/mysql.sock   

[mysqladmin]
host=localhost
user=zabbix
password=123qqq...A
socket=/vsr/lib/mysql/mysql.sock

14.重启Zabbix-agent服务

[root@localhost ~]# systemctl restart zabbix-agent

三、Zabbix-server Web端配置

1.添加Mysql监控模板
在这里插入图片描述
在这里插入图片描述
2.查看监控数据

 要查看单个监控项的数据图形,直接点击监控项末尾的“图形”即可。
在这里插入图片描述
3.查看所有监控项图形信息
在这里插入图片描述
在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章