Docker CE版本,以及mysql zabbix nginx镜像准备好,这些镜像都可以去使用docker pull去官方仓库拉取下来
[root@www ~]# docker version
Server: Docker Engine - Community
Engine:
Version: 19.03.12
API version: 1.40 (minimum version 1.12)
Go version: go1.13.10
Git commit: 48a66213fe
Built: Mon Jun 22 15:45:28 2020
#注:不要下载最新版本的mysql,不然后面server 无法在mysql上进行注册,会报 “MySQL server is not available. ” 错误
[root@www ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 718a6da099d8 2 weeks ago 448MB
zabbix/zabbix-server-mysql latest 07e5a956d0c2 11 days ago 65.9MB
zabbix/zabbix-web-nginx-mysql latest 700da459fac6 12 days ago 162MB
#注: 启动命令中不要添加 MYSQL_DATABASE="zabbix" 参数,不然后期同样在server上注册不上,会报user表为空的错误。同时--restart=always写在-d mysql:5.7的前面,要不然容器启动失败
[root@www ~]# docker run --name mysql-server -t \
> -p 3306:3306 \
> -e MYSQL_USER="zabbix" \
> -e MYSQL_PASSWORD="zabbix" \
> -e MYSQL_ROOT_PASSWORD="zabbix" \
> -v /home/zabbix/data:/var/lib/mysql \
> --restart=always \
> -d mysql:5.7 \
> --character-set-server=utf8 \
> --collation-server=utf8_bin
2d2c59d6900f226cf68259ac9ebed05eb4ea0e7ee2325bd789d31de86fb50952
[root@www ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2d2c59d6900f mysql:5.7 "docker-entrypoint.s?? 3 minutes ago Up 3 minutes 0.0.0.0:3306->3306/tcp, 33060/tcp mysql-server
[root@www ~]# ll /home/zabbix/data/
total 188484
-rw-r----- 1 polkitd ssh_keys 56 Aug 19 14:27 auto.cnf
-rw------- 1 polkitd ssh_keys 1676 Aug 19 14:27 ca-key.pem
-rw-r--r-- 1 polkitd ssh_keys 1112 Aug 19 14:27 ca.pem
-rw-r--r-- 1 polkitd ssh_keys 1112 Aug 19 14:27 client-cert.pem
-rw------- 1 polkitd ssh_keys 1680 Aug 19 14:27 client-key.pem
-rw-r----- 1 polkitd ssh_keys 1353 Aug 19 14:27 ib_buffer_pool
-rw-r----- 1 polkitd ssh_keys 79691776 Aug 19 14:27 ibdata1
-rw-r----- 1 polkitd ssh_keys 50331648 Aug 19 14:27 ib_logfile0
-rw-r----- 1 polkitd ssh_keys 50331648 Aug 19 14:27 ib_logfile1
-rw-r----- 1 polkitd ssh_keys 12582912 Aug 19 14:27 ibtmp1
drwxr-x--- 2 polkitd ssh_keys 4096 Aug 19 14:27 mysql
drwxr-x--- 2 polkitd ssh_keys 8192 Aug 19 14:27 performance_schema
-rw------- 1 polkitd ssh_keys 1680 Aug 19 14:27 private_key.pem
-rw-r--r-- 1 polkitd ssh_keys 452 Aug 19 14:27 public_key.pem
-rw-r--r-- 1 polkitd ssh_keys 1112 Aug 19 14:27 server-cert.pem
-rw------- 1 polkitd ssh_keys 1680 Aug 19 14:27 server-key.pem
drwxr-x--- 2 polkitd ssh_keys 8192 Aug 19 14:27 sys
root@e1953f899da5 :/# mysql -uzabbix -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.31 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
+--------------------+
1 row in set (0.00 sec)
root@e1953f899da5 :/# mysql -uroot -p
mysql> select user,host from mysql.user;
+---------------+-----------+
| user | host |
+---------------+-----------+
| root | % |
| zabbix | % |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+---------------+-----------+
5 rows in set (0.01 sec)
定义了数据库名zabbix,数据库用户zabbix,密码zabbix,在本地目录/home/zabbix/data中持久化保存mysql容器运行中的数据,确保容器退出运行后,数据不会丢失,最后两条是定义了数据库的字符集为utf8,解决了mysql数据库保存中文的问题
[root@www ~]# docker run --name zabbix-server-mysql -itd \
> -e DB_SERVER_HOST="mysql-server" \
> -e MYSQL_DATABASE="zabbix" \
> -e MYSQL_USER="zabbix" \
> -e MYSQL_PASSWORD="zabbix" \
> -e MYSQL_ROOT_PASSWORD="zabbix" \
> --restart=always \
> --link mysql-server:mysql \
> -p 10051:10051 \
> zabbix/zabbix-server-mysql:latest
4efa86f7b73a6e5ecaf01d300b38c14eb4a1fd13514b58bdbf80b55d6dc75f31
[root@www ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4efa86f7b73a zabbix/zabbix-server-mysql:latest "/sbin/tini -- /usr/?? 2 seconds ago Up 2 seconds 0.0.0.0:10051->10051/tcp zabbix-server-mysql
e1953f899da5 mysql:5.7 "docker-entrypoint.s?? 10 seconds ago Up 9 seconds 0.0.0.0:3306->3306/tcp, 33060/tcp mysql-server
#查看zabbix-server容器日志,看看有什么报错,下面输出没有报错
[root@www ~]# docker logs -f 4efa86f7b73a
** Preparing Zabbix server
** Using MYSQL_USER variable from ENV
** Using MYSQL_PASSWORD variable from ENV
** Using MYSQL_ROOT_PASSWORD variable from ENV
********************
* DB_SERVER_HOST: mysql-server
* DB_SERVER_PORT: 3306
* DB_SERVER_DBNAME: zabbix
Starting Zabbix Server. Zabbix 5.0.2 (revision 352ca05).
Press Ctrl+C to exit.
6:20200819:070225.777 Starting Zabbix Server. Zabbix 5.0.2 (revision 352ca05).
6:20200819:070225.777 ****** Enabled features ******
6:20200819:070225.777 SNMP monitoring: YES
6:20200819:070225.777 IPMI monitoring: YES
#查看zabbix-server配置文件,只修改两个地方(这些都是在启动容器的时候环境变量指定的),其他都是默认
[root@www ~]# docker exec -it 4efa86f7b73a /bin/bash
bash-5.0$ cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2 mysql e1953f899da5 mysql-server #--link的作用体现出来了
172.17.0.3 4efa86f7b73a
#zabbix server数据库配置信息如下:
#指定数据库主机
bash-5.0$ cd /etc/zabbix/
bash-5.0$ grep DBHost zabbix_server.conf
DBHost=mysql-server
#指定密码
bash-5.0$ grep DBPassword zabbix_server.conf
DBPassword=zabbix
启动zabbix server服务,并关联这个服务到上面已经启动的mysql数据库实例上,确保zabbix的数据可以保存到mysql数据库,如果想要数据持久化根据生产环境实际情况灵活决定是否挂载,上面我没有挂载,即没有使用卷持久化。
[root@www ~]# docker run --name zabbix-web-nginx-mysql -t \
> -e DB_SERVER_HOST="mysql-server" \
> -e MYSQL_DATABASE="zabbix" \
> -e MYSQL_USER="zabbix" \
> -e MYSQL_PASSWORD="zabbix" \
> -e MYSQL_ROOT_PASSWORD="zabbix" \
> -e PHP_TZ="Asia/Shanghai" \
> --link mysql-server:mysql \
> --link zabbix-server-mysql:zabbix-server\
> -p 8080:8080 \
> -d zabbix/zabbix-web-nginx-mysql
d3dc509209b9349267b48f80ce8aa36e051c30199a98dc2199d37f3314ea6795
[root@www ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d3dc509209b9 zabbix/zabbix-web-nginx-mysql "docker-entrypoint.sh" 2 seconds ago Up 2 seconds 0.0.0.0:8080->8080/tcp, 8443/tcp zabbix-web-nginx-mysql
4efa86f7b73a zabbix/zabbix-server-mysql:latest "/sbin/tini -- /usr/?? 16 minutes ago Up 16 minutes 0.0.0.0:10051->10051/tcp zabbix-server-mysql
e1953f899da5 mysql:5.7 "docker-entrypoint.s?? 16 minutes ago Up 16 minutes 0.0.0.0:3306->3306/tcp, 33060/tcp mysql-server
[root@www ~]# docker exec -it d3dc509209b9 /bin/bash
bash-5.0$ find / -name zabbix.conf.php
/etc/zabbix/web/zabbix.conf.php
#web端数据库信息配置如下:
bash-5.0$ cat /etc/zabbix/web/zabbix.conf.php
link mysql-server:mysql 连接数据库实例,mysql-server必须要与第一步的--name mysql-server完全一致
--link zabbix-server-mysql:zabbix-server 连接zabbix服务器实例,zabbix-server-mysql必须要与第二步的--name zabbix-server-mysql完全一致
可以看到大功告成!
最后将这三个容器的启动命令记录一下:
docker run --name mysql-server -t \
-p 3306:3306 \
-e MYSQL_USER="zabbix" \
-e MYSQL_PASSWORD="zabbix" \
-e MYSQL_ROOT_PASSWORD="zabbix" \
-v /home/zabbix/data:/var/lib/mysql \
--restart=always \
-d mysql:5.7 \
--character-set-server=utf8 \
--collation-server=utf8_bin
docker run --name zabbix-server-mysql -itd \
-e DB_SERVER_HOST="mysql-server" \
-e MYSQL_DATABASE="zabbix" \
-e MYSQL_USER="zabbix" \
-e MYSQL_PASSWORD="zabbix" \
-e MYSQL_ROOT_PASSWORD="zabbix" \
--restart=always \
--link mysql-server:mysql \
-p 10051:10051 \
zabbix/zabbix-server-mysql:latest
docker run --name zabbix-web-nginx-mysql -t \
-e DB_SERVER_HOST="mysql-server" \
-e MYSQL_DATABASE="zabbix" \
-e MYSQL_USER="zabbix" \
-e MYSQL_PASSWORD="zabbix" \
-e MYSQL_ROOT_PASSWORD="zabbix" \
-e PHP_TZ="Asia/Shanghai" \
--link mysql-server:mysql \
--link zabbix-server-mysql:zabbix-server\
-p 8080:8080 \
-d zabbix/zabbix-web-nginx-mysql