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.查看所有監控項圖形信息
在這裏插入圖片描述
在這裏插入圖片描述

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