Zabbix 5.0 LTS版本的安装小结
1:准备Zabbix的服务器。
这里可能需要一台或多台服务器,视需求和资源而定。也可以将Zabbix_Server、MySQL、Zabbix Web等安装在同一台服务器上。
这里我们打算安装在一台测试服务器上。操作系统版本为CentOS Linux release 8.2.2004 (Core)。
环境要求
Zabbix 5.0 版本对基础环境的要求有大的变化,最大的就是对 php 版本的要求,最低要求 7.2.0 版本,对 php 扩展组件版本也有要求,详见官网文档https://www.zabbix.com/documentation/current/manual/installation/requirements。 这里有对硬件资源、操作系统、数据库版本等相关的规范要求。
2:关闭SELINXU
如果想关闭SeLinux,又不想重启服务器,那么使用下面命令。但是该命令只能将SeLinux在enforcing、permissive这两种模式之间切换.服务器重启后,又会恢复到/etc/selinux/config下的设置,也就是说setenforce的修改是不能持久的。
# setenforce 0
# getenforce
所以你想永久关闭SeLinux的话,还是必须调整/etc/selinux/config下的参数。
# vi /etc/selinux/config
将SELINUX=enforcing调整为SELINUX=disabled 或Permissive
你也可以使用shell脚本修改参数
# getenforce
Enforcing
# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
调整完参数后,最好重启一下服务器
3:安装MySQL数据库
Zabbix支持MySQL、PostgreSQL、Oracle 等数据库,另外MySQL数据库可以用MySQL社区版或MariaDB。具体的版本要求为:MySQL.5.62 - 8.0.x ,如果是MariaDB则要求10.0.37或后续版本。
下面我们打算安装MySQL社区版。yum安装方式的简单过程如下:
安装MySQL Repository
#rpm -ivh https://dev.mysql.com/get/mysql80-community-release-el8-1.noarch.rpm
验证MySQL Repository是否安装
#yum repolist all | grep mysql | grep enabled
[root@appliance ~]# yum repolist all | grep mysql | grep enabled
mysql-connectors-community MySQL Connectors Community enabled
mysql-tools-community MySQL Tools Community enabled
mysql80-community MySQL 8.0 Community Server enabled
Yum方式安装MySQL
#yum --disablerepo=AppStream install -y mysql-community-server
#systemctl enable mysqld #设置开机启动MySQL
#systemctl start mysqld #启动MySQL服务
#systemctl status mysqld #检查MySQL服务状态
获取MySQL root的临时密码
[root@appliance ~]# cat /var/log/mysqld.log | grep -i 'temporary password'
2020-07-02T07:37:04.495297Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: AgI<w+2,hwyW
[root@appliance ~]#
执行mysql_secure_installation安全配置向导来做一些常规化安全设置
[root@appliance ~]# mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root:
The existing password for the user account root has expired. Please set a new password.
New password:
Re-enter new password:
The 'validate_password' component is installed on the server.
The subsequent steps will run with the existing configuration
of the component.
Using existing password for root.
Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : YES
New password:
Re-enter new password:
Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
- Dropping test database...
Success.
- Removing privileges on test database...
Success.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.
All done!
因为规划将MySQL的数据库放在/mysql下面,所以我们还需要移动MySQL的datadir到/mysql下,关于如何移动MySQL数据库存储目录,参考我这篇博客“CentOS 7上更改MySQL数据库存储目录浅析。” 这里就不展开介绍细节信息了。
如果要安装MariaDB也非常简单。使用下面命令即可快捷方便的安装MariaDB
yum install mariadb-server -y
4:安装Zabbix Repository
#rpm -Uvh https://repo.zabbix.com/zabbix/5.0/rhel/8/x86_64/zabbix-release-5.0-1.el8.noarch.rpm
#dnf clean all
[root@appliance mysql]# rpm -Uvh https://repo.zabbix.com/zabbix/5.0/rhel/8/x86_64/zabbix-release-5.0-1.el8.noarch.rpm
Retrieving https://repo.zabbix.com/zabbix/5.0/rhel/8/x86_64/zabbix-release-5.0-1.el8.noarch.rpm
warning: /var/tmp/rpm-tmp.Am4Zqr: Header V4 RSA/SHA512 Signature, key ID a14fe591: NOKEY
Verifying... ################################# [100%]
Preparing... ################################# [100%]
Updating / installing...
1:zabbix-release-5.0-1.el8 ################################# [100%]
[root@appliance mysql]# dnf clean all
28 files removed
5:安装Zabbix Server、Zabbix Agent、Zabbix Web
Nginx
dnf install zabbix-server-mysql zabbix-web-mysql zabbix-nginx-conf zabbix-agent
Apache
# dnf install zabbix-server-mysql zabbix-web-mysql zabbix-apache-conf zabbix-agent
6:初始化Zabbix的数据库
# mysql -uroot -p
password #输入具体的账号密码
mysql> create database zabbix character set utf8 collate utf8_bin;
mysql> create user zabbix@localhost identified by 'xxxxx'; #实际情况中,输入具体的账号密码(这里在某些数据库版本有个坑,后面讲述这个坑)
mysql> grant all privileges on zabbix.* to zabbix@localhost;
mysql> quit;
导入Zabbix数据库结构和数据
# zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p zabbix
7:配置Zabbix Server连接数据库设置
/etc/zabbix/zabbix_server.conf
一般情况执行配置DBPassword,但是如果你修改了一些默认配置,那么还需设置一些其他参数。例如DBSocket、DBPort等等。
### Option: DBHost
# Database host name.
# If set to localhost, socket is used for MySQL.
# If set to empty string, socket is used for PostgreSQL.
#
# Mandatory: no
# Default:
# DBHost=localhost
DBHost=localhost
### Option: DBName
# Database name.
#
# Mandatory: yes
# Default:
# DBName=
DBName=zabbix
### Option: DBPassword
# Database password.
# Comment this line if no password is used.
#
# Mandatory: no
# Default:
# DBPassword=
DBPassword=xxxxxx
### Option: DBSocket
# Path to MySQL socket.
#
# Mandatory: no
# Default:
# DBSocket=
DBSocket=/mysql_data/mysql/mysql.sock
这里要根据实际情况进行配置,有些参数也可以不配置。有些参数,例如DBSocket,我修改过MySQL数据库的数据文件目录,所以这里必须修改。否则后面会遇到一些错误。
8:Zabbix前端的PHP配置
/etc/php-fpm.d/zabbix.conf
参数date.timezone的默认配置如下,一般我们必须取消注释,将时区修改为Asia/Shanghai
; php_value[date.timezone] = Europe/Riga
9: 修改nginx或Apache的配置
/etc/nginx/conf.d/zabbix.conf 配置端口和指定server_name的IP地址
listen 80;
server_name xxx.xxx.xxx.xx;
如果是Apache暂时不用设置任何配置。
10:设置防火墙端口
#firewall-cmd --permanent --add-port=10050/tcp
#firewall-cmd --permanent --add-port=10051/tcp
#firewall-cmd --permanent --add-port=80/tcp
#firewall-cmd --reload
10: 启动服务并将其设置为开机自启动
Nginx
# systemctl restart zabbix-server zabbix-agent nginx php-fpm
# systemctl enable zabbix-server zabbix-agent nginx php-fpm
Apache
#systemctl restart zabbix-server zabbix-agent httpd php-fpm
#systemctl enable zabbix-server zabbix-agent httpd php-fpm
11:http://xxx.xxx.xxx.xxx/ 进入设置界面。
Nginx
http://192.168.xxx.xxx/setup.php
Apache
http://192.168.xxx.xxx/zabbix/setup.php
配置完成后,登录系统修改账号密码,禁用一些账号。然后就可以开始使用Zabbix了。
问题小结
1:导入Zabbix数据库结构和数据时遇到告警:“No database selected”
安装测试过两次,有一次遇到下面错误“No database selected”,解决办法是,在create.sql.gz中加入一行SQL:
[root@appliance ~]# zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p
Enter password:
ERROR 1046 (3D000) at line 1: No database selected
可以先解压gz文件,加入use zabbix;这一句SQL,或者直接使用vim编辑gz文件文件
# vim create.sql.gz
其实出现上面错误,是因为脚本没有指定数据库,如下书写即可避免这个问题。你也可以通过参数 -D指定数据库。
# zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p zabbix
2:配置DB Connection过程遇到“Cannot Connect to the Database"错误
检查Zabbix Server的日志,发现如下错误信息。
19690:20200702:184818.882 [Z3001] connection to database 'zabbix' failed: [2002] Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
19690:20200702:184818.882 database is down: reconnecting in 10 seconds
19690:20200702:184828.883 [Z3001] connection to database 'zabbix' failed: [2002] Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
19690:20200702:184828.883 database is down: reconnecting in 10 seconds
19690:20200702:184838.883 [Z3001] connection to database 'zabbix' failed: [2002] Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
19690:20200702:184838.883 database is down: reconnecting in 10 seconds
19690:20200702:184848.884 [Z3001] connection to database 'zabbix' failed: [2002] Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
19690:20200702:184848.884 database is down: reconnecting in 10 seconds
19690:20200702:184858.884 [Z3001] connection to database 'zabbix' failed: [2002] Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
19690:20200702:184858.884 database is down: reconnecting in 10 seconds
这个是Zabbix Server连接MySQL有问题。因为我移动过MySQL的数据目录,所以需要修改/etc/zabbix/zabbix_server.conf文件下的参数DBScoket,如下所示
然后Zabbix Server的错误日志中上述错误消失,但是依然报这个错误,如下截图所示, 此时必须将Database Host从localhost改为127.0.0.1
问题解决后,登录系统,修改Zabbix用户密码。
3:配置DB Connection过程遇到“Cannot Connect to the Database"错误,如下截图所示:
检查Zabbix Server的日志,tail -80f /var/log/zabbix/zabbix_server.log 如下所示,提示下面错误
63446:20200707:141746.072 Starting Zabbix Server. Zabbix 5.0.1 (revision c2a0b03480).
63446:20200707:141746.073 ****** Enabled features ******
63446:20200707:141746.073 SNMP monitoring: YES
63446:20200707:141746.073 IPMI monitoring: YES
63446:20200707:141746.073 Web monitoring: YES
63446:20200707:141746.073 VMware monitoring: YES
63446:20200707:141746.073 SMTP authentication: YES
63446:20200707:141746.073 ODBC: YES
63446:20200707:141746.073 SSH support: YES
63446:20200707:141746.073 IPv6 support: YES
63446:20200707:141746.073 TLS support: YES
63446:20200707:141746.073 ******************************
63446:20200707:141746.073 using configuration file: /etc/zabbix/zabbix_server.conf
63446:20200707:141746.080 [Z3001] connection to database 'zabbix' failed: [2059] Plugin caching_sha2_password could not be loaded: lib64/mariadb/plugin/caching_sha2_password.so: cannot open shared object file: No such file or directory
63446:20200707:141746.080 Cannot connect to the database. Exiting
这个是因为MySQL 8数据库的加密方式改变了,可以设置参数default_authentication_plugin='mysql_native_password',使用旧版本中的认证方式。当然也可以使用下面方式修改一下账号
mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.20 |
+-----------+
1 row in set (0.00 sec)
mysql> show variables like 'default_authentication_plugin';
+-------------------------------+-----------------------+
| Variable_name | Value |
+-------------------------------+-----------------------+
| default_authentication_plugin | caching_sha2_password |
+-------------------------------+-----------------------+
1 row in set (0.01 sec)
mysql> select host,user,plugin from mysql.user;
+-----------+------------------+-----------------------+
| host | user | plugin |
+-----------+------------------+-----------------------+
| localhost | mysql.infoschema | caching_sha2_password |
| localhost | mysql.session | caching_sha2_password |
| localhost | mysql.sys | caching_sha2_password |
| localhost | root | caching_sha2_password |
| localhost | zabbix | caching_sha2_password |
+-----------+------------------+-----------------------+
5 rows in set (0.00 sec)
mysql> alter user 'zabbix'@'localhost' identified with mysql_native_password by 'xxxxxx';
Query OK, 0 rows affected (0.02 sec)
mysql>
之所以遇到这种问题,是因为上面创建账号时,使用的是下面这个SQL引起
mysql> create user zabbix@localhost identified by 'xxxxx';
参考资料:
https://www.zabbix.com/documentation/current/manual/installation/install#installing_frontend
https://www.itzgeek.com/how-tos/linux/centos-how-tos/how-to-install-zabbix-server-on-centos-8-rhel-8.html
https://www.zabbix.com/cn/download?zabbix=5.0&os_distribution=centos&os_version=8&db=mysql&ws=apache
https://www.itzgeek.com/how-tos/linux/centos-how-tos/how-to-install-mysql-8-0-on-rhel-8.html
https://bobcares.com/blog/zabbix-cannot-connect-to-the-database/
http://www.zsythink.net/archives/500