为 MariaDB Galera 集群创建 SELinux 策略

从那以后,我们在许可模式下配置了 SELinux。
因此,MariaDB 和 Galera 的所有违规行为都已记录在 /var/log/audit/audit.log 中。
我们可以使用它来创建具体的 SELinux 策略。

使用 fgrep 和 audit2allow 命令将策略违规日志提取到文本文件中。

[root@mariadb-01 ~]# fgrep "mysqld" /var/log/audit/audit.log | audit2allow -m MySQL_galera -o MySQL_galera.te

将这些日志编译为 SELinux 策略模块。

[root@mariadb-01 ~]# checkmodule -M -m MySQL_galera.te -o MySQL_galera.mod
checkmodule:  loading policy configuration from galera.te
checkmodule:  policy configuration loaded
checkmodule:  writing binary representation (version 19) to MySQL_galera.mod

创建一个编译好的策略模块包。

[root@mariadb-01 ~]# semodule_package -m MySQL_galera.mod -o MySQL_galera.pp

将此策略导入 SELinux。

[root@mariadb-01 ~]# semodule -i MySQL_galera.pp

将 SELinux 设置为在强制模式下运行。

[root@mariadb-01 ~]# setenforce 1

通过在每个节点上重新启动 MariaDB 服务来测试 SELinux 是否工作正常。

最后,在所有节点上启用 MariaDB 服务。

[root@mariadb-01 ~]# systemctl enable mariadb.service

我们已经在 CentOS 7 上成功配置了一个 MariaDB Galera 集群。
虽然我们配置了一个双节点集群,但是同样的步骤对于配置一个更大规模的 MariaDB Galera 集群来说已经足够了。

www. On IT Road .com

在 CentOS 7 上配置 MariaDB Galera 集群

在 Linux 防火墙中允许 MariaDB 和 Galera 服务端口。

[root@mariadb-01 ~]# firewall-cmd --permanent --add-service=mysql
success
[root@mariadb-01 ~]# firewall-cmd --permanent --add-port={4567,4568,4444}/tcp
success
[root@mariadb-01 ~]# firewall-cmd --reload
success

现在将 SELinux 设置为许可模式,稍后我们将在为 MariaDB Galera 集群创建 SELinux 策略后启用强制模式。

[root@mariadb-01 ~]# setenforce 0

现在编辑 MariaDB 配置文件。

[root@mariadb-01 ~]# vi /etc/my.cnf.d/server.cnf

并按如下方式配置 galera 部分:

[galera]
# Mandatory settings
wsrep_on=ON
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_cluster_address=gcomm://192.168.1.81,192.168.1.82
binlog_format=row
default_storage_engine=InnoDB
innodb_autoinc_lock_mode=2

在每个节点上执行上述步骤。

在 mariadb-01.onitroad.com 上启动 Galera 集群。

[root@mariadb-01 ~]# galera_new_cluster

在所有其他节点上启动 MariaDB 服务。

[root@mariadb-01 ~]# systemctl start mariadb.service

如果服务启动成功,则说明我们已经成功配置了我们的 Galera 集群。

在每个节点上配置 MariaDB 数据库实例。

[root@mariadb-01 ~]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] Y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB 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? [Y/n] 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? [Y/n] Y
 ... Success!
By default, MariaDB 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? [Y/n] 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? [Y/n] Y
 ... Success!
Cleaning up...
All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!

连接任何节点上的 MariaDB 实例并检查 wsrep_cluster_size。

[root@mariadb-01 ~]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 10.3.12-MariaDB MariaDB Server
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 global status like 'wsrep_cluster_size';
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| wsrep_cluster_size | 2     |
+--------------------+-------+
1 row in set (0.083 sec)

wsrep_cluster_size 确认我们所有的节点现在都连接在 Galera 集群中。

在 CentOS 7 上安装 MariaDB 10.3 数据库服务器

以 root 用户身份使用 ssh 连接到 mariadb-01.onitroad.com。

安装 MariaDB 和 MaxScale yum 存储库。

[root@mariadb-01 ~]# curl -sS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | bash
[info] Repository file successfully written to /etc/yum.repos.d/mariadb.repo.
[info] Adding trusted package signing keys...
[info] Succeessfully added trusted package signing keys.

为所有存储库构建 yum 缓存。

[root@mariadb-01 ~]# yum makecache fast

使用 yum 命令安装 MariaDB 10.3 Server 和 Galera。

[root@mariadb-01 ~]# yum install -y mariadb-server galera

在所有其他节点上重复上述步骤。

在 CentOS 7 上安装 MariaDB Galera 集群

MariaDB Galera Cluster 是 MariaDB 的同步多主集群。

它是 Galera Cluster 的一个分支,Galera Cluster 是世界上最先进、免费和开源的集群引擎。

目前仅支持 InnoDB 存储引擎。

MariaDB Galera 集群是一个真正的多主和双活集群。
由于它的同步行为,在节点崩溃的情况下不会丢失数据,因为所有节点始终保持相同的状态。

MariaDB Galera Cluster 还提供自动节点配置。
这意味着在将数据库添加到 Galera 集群之前,我们不必手动备份数据库并在新节点上恢复它。
由于简单的横向扩展和横向扩展操作,此功能提供了云支持的另外好处。

在本文中,我们将在 CentOS 7 上创建 MariaDB 10.3 数据库的两节点 MariaDB Galera 集群。

安装环境

在本文中,我们将使用两台 CentOS 7 服务器作为 Galera Cluster 节点。

主机名:mariadb-01.onitroad.com
IP 地址:192.168.1.81 /24
CPU:2.4 GHz(2 核)
内容:4 GB
操作系统:CentOS 7.6
MariaDB 版本:10.3.12
日期:2020-09-17 00:11:35 来源:oir作者:oir