zabbix4.0.19部署-保姆级教程

zabbix的服务端需要LAMP或者LNMP的环境,所以这里的安装方式有很多中了,我们就选择一种比较简单的安装方式,直接用官方提供的源来安装。

一、部署准备

[root@zabbix-server ~]# uname -a
Linux zabbix-server 3.10.0-1062.18.1.el7.x86_64 #1 SMP Tue Mar 17 23:49:17 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
[root@zabbix-server ~]# cat /etc/centos-release
CentOS Linux release 7.7.1908 (Core)

防火墙关闭状态,关闭SELinux

yum -y update

1、安装PHP

yum install -y php php-fpm

vim /etc/php.ini
shift+g 直达末尾追加下面内容:

date.timezone = Asia/Shanghai max_execution_time = 300
post_max_size = 32M
max_input_time = 300
memory_limit = 128M makefile

systemctl start php-fpm

2、安装mariadb

检测冲突

一定要卸载干净MySQL,否则会出现莫名其妙的问题

systemctl stop mysqld
rpm -aq mysql*
yum remove -y `rpm -aq mysql*`

[root@server2 ~]# find / -name mysql
/var/lib/mysql
/var/lib/mysql/mysql
/usr/share/mysql
[root@server1 ~]# rm -rf /var/lib/mysql /var/lib/mysql/mysql /usr/share/mysql

[root@server2 ~]# ll /etc/my.cnf
-rw-r--r-- 1 root root 570 Sep 21  2016 /etc/my.cnf
[root@server2 ~]# rm -rf /etc/my.cnf
 
测试:
[root@server2 ~]# rpm -aq mysql*

准备安装包

yum -y install mariadb mariadb-server
systemctl start mariadb
systemctl enable mariadb

在这里必须要启动一下,启动才能生成日志,有的版本要到日志里面去找密码。

数据库权限初始化

/usr/bin/mysql_secure_installation mysql -uroot mysql -uroot -p

初始化过程:

01 [root@test ~]# /usr/bin/mysql_secure_installation
02 NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
03       SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
04  
05 In order to log into MySQL to secure it, we'll need the current
06 password for the root user.  If you've just installed MySQL, and
07 you haven't set the root password yet, the password will be blank,
08 so you should just press enter here.
09  
10 Enter current password for root (enter for none):<---输入现在的root密码,因为我们还没设置,直接回车
11 OK, successfully used password, moving on...
12  
13 Setting the root password ensures that nobody can log into the MySQL
14 root user without the proper authorisation.
15  
16 Set root password? [Y/n] Y   <---是否设定root密码,当然设置了,输入Y回车
17 New password: <---输入root密码,并回车,输入的过程中不会有任何显示
18 Re-enter new password: <---再次输入root密码,并回车,输入的过程中不会有任何显示
19 Password updated successfully!
20 Reloading privilege tables..
21  ... Success!
22  
23 By default, a MySQL installation has an anonymous user, allowing anyone
24 to log into MySQL without having to have a user account created for
25 them.  This is intended only for testing, and to make the installation
26 go a bit smoother.  You should remove them before moving into a
27 production environment.
28  
29 Remove anonymous users? [Y/n] Y <---是否删除匿名用户,删除,输入Y回车
30  ... Success!
31  
32 Normally, root should only be allowed to connect from 'localhost'.  This
33 ensures that someone cannot guess at the root password from the network.
34  
35 Disallow root login remotely? [Y/n] Y <---是否删禁止root用户远程登录,当然禁止,输入Y回车
36  ... Success!
37  
38 By default, MySQL comes with a database named 'test' that anyone can
39 access.  This is also intended only for testing, and should be removed
40 before moving into a production environment.
41  
42 Remove test database and access to it? [Y/n] <---是否删除测试数据库test,删除,输入Y回车
43  - Dropping test database...
44  ... Success!
45  - Removing privileges on test database...
46  ... Success!
47  
48 Reloading the privilege tables will ensure that all changes made so far
49 will take effect immediately.
50  
51 Reload privilege tables now? [Y/n] Y <---刷新权限,输入Y回车
52  ... Success!
53  
54 Cleaning up...
55  
56 All done!  If you've completed all of the above steps, your MySQL
57 installation should now be secure.
58  
59 Thanks for using MySQL!

更改字符集

vim /etc/my.cnf

在[mysqld]中添加:
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake

vim /etc/my.cnf.d/client.cnf
在[client]中添加
[client]
default-character-set=utf8

vim /etc/my.cnf.d/mysql-clients.cnf
[mysql]
default-character-set=utf8

查看确认字符集

[root@pokes02 ~]# mysql -uroot -p******

MariaDB [(none)]> show variables like "%character%";show variables like "%collation%";
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

+----------------------+-------------------+
| Variable_name        | Value             |
+----------------------+-------------------+
| collation_connection | utf8_general_ci   |
| collation_database   | latin1_swedish_ci |
| collation_server     | latin1_swedish_ci |
+----------------------+-------------------+
3 rows in set (0.00 sec)

3、安装apache

yum install -y httpd

二、安装zabbix

rpm -i https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm
yum install zabbix-server-mysql zabbix-web-mysql zabbix-agent -y

注意看是否有报错。

常见报错处理:

解决Errno 256报错

yum安装zabbix-server-mysql zabbix-web-mysql zabbix-agent 出现以下报错:

Error downloading packages: 
zabbix-web-4.0.19-1.el7.noarch: [Errno 256] No more mirrors to try. 
zabbix-agent-4.0.19-1.el7.x86_64: [Errno 256] No more mirrors to try. 
zabbix-server-mysql-4.0.19-1.el7.x86_64: [Errno 256] No more mirrors to try.

原因:zabbix源本身的问题,因为服务器在国外。

[root@localhost ~]# cd /etc/yum.repos.d
[root@localhost yum.repos.d]# ls
CentOS-Base.repo  CentOS-Debuginfo.repo  CentOS-Media.repo    CentOS-Vault.repo
CentOS-CR.repo    CentOS-fasttrack.repo  CentOS-Sources.repo  zabbix.repo
[root@localhost yum.repos.d]# vim zabbix.repo 

添加下面内容:

[zabbix]
name=Zabbix Official Repository - $basearch
baseurl=https://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591

[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=1

三、导入zabbix数据

zabbix提供了一个数据库文件,需要将这个文件导入到数据库中

1、创建库并授权

导入数据之前,必须先新建zabbix库,创建管理zabbix库的用户

创建zabbix库,创建zabbix用户,并授权。

[root@pokes02 ~]# mysql -uroot -p
password :123456
mysql> create database zabbix character set utf8 collate utf8_bin;      #创建zabbix库
mysql> grant all privileges on zabbix.* to zabbix@localhost identified by '******';     #创建zabbix用户,密码为*****
mysql> grant all on zabbix.* to 'zabbix'@'localhost' identified by '*******' with grant option;  #授权zabbix用户拥有管理zabbix库的权限
mysql> flush privileges;    #刷新权限
mysql> quit;

以上已经创建库和用户、授权。现在我们去找到要导入的数据

2、查看zabbix提供的数据库并导入

[root@pokes02 ~]# ls /usr/share/doc/zabbix-server-mysql-4.0.38/create.sql.gz 
/usr/share/doc/zabbix-server-mysql-4.0.38/create.sql.gz
[root@pokes02 ~]# gzip -d /usr/share/doc/zabbix-server-mysql-4.0.38/create.sql.gz
[root@pokes02 ~]# mysql -uzabbix -p******* zabbix < /usr/share/doc/zabbix-server-mysql-4.0.38/create.sql    # *****是密码

注意:这里导入数据时如果报错,是因为你没有授权成功或者没有建zabbix库,或者是zabbix用户密码错误等等。

3、重启服务

systemctl restart zabbix-server.service
systemctl restart zabbix-agent.service 
systemctl restart httpd.service 

四、Zabbix配置

1、修改DBPassword密码

数据库导入完成后需要给Zabbix配置数据库密码,修改/etc/zabbix/zabbix_server.conf中

vim /etc/zabbix/zabbix_server.conf    #敲/搜索DBPassword
DBPassword=********			#这里的密码是数据库zabbix用户的密码,不是root密码

默认是加了#号是注释,取消注释添加密码

2、配置PHP时区

为zabbix配置PHP时区: 修改编辑文件/etc/httpd/conf.d/zabbix.conf,取消注释并为您设置正确的时区。

vim /etc/httpd/conf.d/zabbix.conf
php_value date.timezone Asia/Shanghai

3、启动Zabbix服务

重启服务:
systemctl restart zabbix-server
systemctl restart zabbix-agent 
systemctl restart httpd.service

配置开机启动:
systemctl enable zabbix-server
systemctl enable zabbix-agent 
systemctl enable httpd

如果启动失败!则让zabbix以root用户启动:

[root@xxx xxxx]# cat /usr/lib/systemd/system/zabbix-agent.service
[Unit]
Description=Zabbix Agent
After=syslog.target
After=network.target

[Service]
Environment="CONFFILE=/etc/zabbix/zabbix_agentd.conf"
EnvironmentFile=-/etc/sysconfig/zabbix-agent
Type=forking
Restart=on-failure
PIDFile=/var/run/zabbix/zabbix_agentd.pid
#PIDFile=/run/zabbix/zabbix_agentd.pid
KillMode=control-group
ExecStart=/usr/sbin/zabbix_agentd -c $CONFFILE
ExecStop=/bin/kill -SIGTERM $MAINPID
RestartSec=10s
#User=zabbix
#Group=zabbix
User=root
Group=root
[Install]
WantedBy=multi-user.target

4、访问图形化界面

启动后既可以运行了:http://IP/zabbix 进入zabbix的配置界面了。

默认的登录账户和密码是:Admin、zabbix

五、安装Zabbix agent 服务

1、Windwos安装Zabbix agent 服务

zabbix4.0.19部署-保姆级教程_第1张图片

2、Centos7安装Zabbix agent 服务

rpm -i https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm
yum install zabbix-agent -y

修改配置文件
打开vim /etc/zabbix/zabbix_agentd.conf,修改以下三项:
Server=zabbix服务器IP          # zabbix server端IP
ServerActive=zabbix服务器IP    # zabbix server端IP
Hostname=mysqserver           # zabbix客户端主机名,和添加主机时的名字要保持一致

systemctl start zabbix-agent
systemctl enable zabbix-agent

你可能感兴趣的:(zabbix,mysql,linux,centos)