目前由于美国对中国科技行业的打压,IT业界正在加速启用国产替代,本文在开源欧拉openEuler22.03LTS系统下,完成以容器化方式部署zabbix数据库实例。
一、获取mariadb的镜像文件
在可以连接到外网的服务器拉取mariadb服务器的docker镜像,本文件以10.5版本为例:
docker pull mariadb/server:10.5
将镜像存为名为mariadb-10.5.18.tar的文件,将文件放到内网的开源欧拉openEuler22.03LTS
系统服务器上。
二、挂载镜像并生成持久化的数据库容器
安装运行环境
[root@localhost opt]# dnf install docker
Last metadata expiration check: 0:36:11 ago on 2023年01月20日 星期五 12时34分25秒.
Dependencies resolved.
=======================================================================================================================================
Package Architecture Version Repository Size
=======================================================================================================================================
Installing:
docker-engine x86_64 18.09.0-300.oe2203 OS 37 M
Installing dependencies:
libcgroup x86_64 0.42.2-1.oe2203 OS 97 k
Transaction Summary
=======================================================================================================================================
Install 2 Packages
Total download size: 37 M
Installed size: 154 M
Is this ok [y/N]: y
Downloading Packages:
(1/2): libcgroup-0.42.2-1.oe2203.x86_64.rpm 10 MB/s | 97 kB 00:00
(2/2): docker-engine-18.09.0-300.oe2203.x86_64.rpm 91 MB/s | 37 MB 00:00
---------------------------------------------------------------------------------------------------------------------------------------
Total 91 MB/s | 37 MB 00:00
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Running scriptlet: libcgroup-0.42.2-1.oe2203.x86_64 1/2
Installing : libcgroup-0.42.2-1.oe2203.x86_64 1/2
Running scriptlet: libcgroup-0.42.2-1.oe2203.x86_64 1/2
Installing : docker-engine-18.09.0-300.oe2203.x86_64 2/2
Running scriptlet: docker-engine-18.09.0-300.oe2203.x86_64 2/2
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.
Verifying : docker-engine-18.09.0-300.oe2203.x86_64 1/2
Verifying : libcgroup-0.42.2-1.oe2203.x86_64 2/2
Installed:
docker-engine-18.09.0-300.oe2203.x86_64 libcgroup-0.42.2-1.oe2203.x86_64
Complete!
加载镜像
[root@localhost opt]# docker load -i dockerfile/mariadb-10.5.18.tar
Loaded image: mariadb:10.5.18
查看导入的镜像
[root@localhost dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mariadb 10.5.18 819fb13996d3 7 weeks ago 402MB
生成容器并启用数据库root用户密码
[root@localhost ~]# docker run -itd --restart=always --name mysqldb -e MARIADB_ROOT_PASSWORD=此处转换为数据库root用户密码 -d -p 3306:3306 mariadb:10.5.18
d59dba9b547db6223ea5fd38fa5817a85b4c1465718609ee58aff32e45acb782
安装数据库客户端
[root@localhost dockerfile]# dnf install mariadb
Last metadata expiration check: 0:46:19 ago on 2023年01月20日 星期五 12时34分25秒.
Dependencies resolved.
=======================================================================================================================================
Package Architecture Version Repository Size
=======================================================================================================================================
Installing:
mariadb x86_64 4:10.5.10-3.oe2203 OS 6.2 M
Installing dependencies:
mariadb-common x86_64 4:10.5.10-3.oe2203 OS 28 k
mariadb-config x86_64 4:10.5.10-3.oe2203 OS 9.1 k
mariadb-connector-c x86_64 3.1.13-1.oe2203 OS 179 k
Transaction Summary
=======================================================================================================================================
Install 4 Packages
Total download size: 6.4 M
Installed size: 39 M
Is this ok [y/N]: y
Downloading Packages:
(1/4): mariadb-common-10.5.10-3.oe2203.x86_64.rpm 1.2 MB/s | 28 kB 00:00
(2/4): mariadb-config-10.5.10-3.oe2203.x86_64.rpm 313 kB/s | 9.1 kB 00:00
(3/4): mariadb-connector-c-3.1.13-1.oe2203.x86_64.rpm 6.3 MB/s | 179 kB 00:00
(4/4): mariadb-10.5.10-3.oe2203.x86_64.rpm 23 MB/s | 6.2 MB 00:00
---------------------------------------------------------------------------------------------------------------------------------------
Total 23 MB/s | 6.4 MB 00:00
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Running scriptlet: mariadb-connector-c-3.1.13-1.oe2203.x86_64 1/1
Preparing : 1/1
Installing : mariadb-connector-c-3.1.13-1.oe2203.x86_64 1/4
Installing : mariadb-config-4:10.5.10-3.oe2203.x86_64 2/4
Installing : mariadb-common-4:10.5.10-3.oe2203.x86_64 3/4
Installing : mariadb-4:10.5.10-3.oe2203.x86_64 4/4
Running scriptlet: mariadb-4:10.5.10-3.oe2203.x86_64 4/4
Verifying : mariadb-4:10.5.10-3.oe2203.x86_64 1/4
Verifying : mariadb-common-4:10.5.10-3.oe2203.x86_64 2/4
Verifying : mariadb-config-4:10.5.10-3.oe2203.x86_64 3/4
Verifying : mariadb-connector-c-3.1.13-1.oe2203.x86_64 4/4
Installed:
mariadb-4:10.5.10-3.oe2203.x86_64 mariadb-common-4:10.5.10-3.oe2203.x86_64 mariadb-config-4:10.5.10-3.oe2203.x86_64
mariadb-connector-c-3.1.13-1.oe2203.x86_64
Complete!
测试客户端连接情况,确认容器工作正常
[root@localhost ~]# mysql -h 127.0.0.1 -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 10.5.18-MariaDB-1:10.5.18+maria~ubu2004 mariadb.org binary distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
MariaDB [(none)]> exit
Bye
准备数据持久化环境,以数据存储目录为/data为例
[root@localhost data]# docker cp mysqldb:/var/lib/mysql .
[root@localhost data]# ll
总用量 0
drwxr-xr-x. 4 root root 215 1月 20 13:44 mysql
[root@localhost data]# mv mysql mysqldata
[root@localhost data]# mkdir /data/mysqlcnf
[root@localhost data]# docker cp mysqldb:/etc/mysql/mariadb.cnf /data/mysqlcnf
[root@localhost data]# ll
总用量 0
drwxr-xr-x. 4 root root 113 12月 9 10:30 mysqlcnf
drwxr-xr-x. 4 systemd-coredump input 215 1月 20 14:03 mysqldata
[root@localhost data]# du * -sh
40K mysqlcnf
149M mysqldata
修改数据库配置文件
[root@localhost data]# cd /data/mysqlcnf
[root@localhost mysqlcnf]# vi mariadb.cnf
[root@localhost mysqlcnf]# cat mariadb.cnf
[mysqld]
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
innodb_file_per_table=1
character-set-server=utf8
innodb_buffer_pool_size=16G
innodb_flush_method=O_DIRECT
innodb_flush_log_at_trx_commit=2
sync_binlog=0
max_connections=1024
max_allowed_packet=128M
character-set-server=utf8
innodb_log_file_size=128M
read_buffer_size=256M
read_rnd_buffer_size=256M
join_buffer_size=128M
sort_buffer_size=256M
删除现有容器,用持久化数据启动新的数据库服务
[root@localhost mysqlcnf]# docker rm -f mysqldb
[root@localhost mysqlcnf]# docker run -itd --restart=always --name mysqldb -v /data/mysqldata:/var/lib/mysql -v /data/mysqlcnf/mariadb.cnf:/etc/mysql/mariadb.cnf -p 3306:3306 mariadb:10.5.18
[root@localhost mysqlcnf]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
944dc7a40ca8 mariadb:10.5.18 "docker-entrypoint.s…" 4 minutes ago Up 3 seconds 0.0.0.0:3306->3306/tcp mysqldb
三、创建zabbix数据库
创建zabbix数据库
[root@localhost mysqlcnf]# mysql -h 127.0.0.1 -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 10.5.18-MariaDB-1:10.5.18+maria~ubu2004 mariadb.org binary distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.001 sec)
MariaDB [(none)]> CREATE DATABASE zabbix character set utf8mb4 collate utf8mb4_bin;
Query OK, 1 row affected (0.001 sec)
MariaDB [(none)]> GRANT ALL PRIVILEGES ON zabbix.* TO zabbix@localhost IDENTIFIED BY '此处输入zabbix帐户密码';
Query OK, 0 rows affected (0.003 sec)
MariaDB [(none)]> GRANT ALL PRIVILEGES ON zabbix.* TO zabbix@'%' IDENTIFIED BY '此处输入zabbix帐户密码';
Query OK, 0 rows affected (0.004 sec)
MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| zabbix |
+--------------------+
4 rows in set (0.001 sec)
MariaDB [(none)]> SET GLOBAL log_bin_trust_function_creators = 1;
Query OK, 0 rows affected (0.000 sec)
MariaDB [(none)]> exit
Bye
导入zabbix数据库生成脚本
[root@localhost mysqlcnf]# zcat /opt/dockerfile/zabbixserverV6.2.1.sql.gz |mysql -h 127.0.0.1 -uzabbix -p zabbix
Enter password:
[root@localhost mysqlcnf]#
至此一个zabbix平台的持久化初始数据库即构建完成。导入备份数据即可完成现有生产系统向国产化操作系统的数据库应用切换工作。