当前位置:Gxlcms > 数据库问题 > MariaDB Galera Cluster 部署(如何快速部署 MariaDB 集群)

MariaDB Galera Cluster 部署(如何快速部署 MariaDB 集群)

时间:2021-07-01 10:21:17 帮助过:5人阅读

为了保证节点间相互通信,需要禁用防火墙设置(如果需要防火墙,则参照官方网站增加防火墙信息设置)

在三个节点分别执行命令:

systemctl stop firewalld

然后将/etc/sysconfig/selinux 的 selinux 设置成 disabled ,这样初始化环境就完成了。

2.安装 MariaDB Galera Cluster

[root@node4 ~]# yum install -y mariadb mariadb-galera-server mariadb-galera-common galera rsync
[root@node5 ~]# yum install -y mariadb mariadb-galera-server mariadb-galera-common galera rsync
[root@node6 ~]# yum install -y mariadb mariadb-galera-server mariadb-galera-common galera rsync

3.配置 MariaDB Galera Cluster

初始化数据库服务,只在一个节点进行

[root@node4 mariadb]# systemctl start mariadb
[root@node4 mariadb]# 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]
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] n
 ... skipping.

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] n
 ... skipping.

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!

关闭数据库,修改 /etc/my.cnf.d/galera.cnf

[root@node4 mariadb]# systemctl stop mariadb
[root@node4 ~]# vim /etc/my.cnf.d/galera.cnf

修改以下内容:

[mysqld]
......
wsrep_provider = /usr/lib64/galera/libgalera_smm.so
wsrep_cluster_address = "gcomm://node4,node5,node6"
wsrep_node_name = node4
wsrep_node_address=10.128.20.16
#wsrep_provider_options="socket.ssl_key=/etc/pki/galera/galera.key; socket.ssl_cert=/etc/pki/galera/galera.crt;"

提示:如果不用 ssl 的方式认证的话,请把wsrep_provider_options 注释掉。

将此文件复制到node5、node6,注意要把 wsrep_node_name和 wsrep_node_address改成相应节点的 hostnameip

4.启动 MariaDB Galera Cluster 服务

[root@node4 ~]# /usr/libexec/mysqld --wsrep-new-cluster --user=root &

观察日志:

[root@node4 ~]# tail -f /var/log/mariadb/mariadb.log

150701 19:54:17 [Note] WSREP: wsrep_load(): loading provider library ‘none‘
150701 19:54:17 [Note] /usr/libexec/mysqld: ready for connections.
Version: ‘5.5.40-MariaDB-wsrep‘  socket: ‘/var/lib/mysql/mysql.sock‘  port: 3306  MariaDB Server, wsrep_25.11.r4026

出现 ready for connections ,证明我们启动成功,继续启动其他节点:

[root@node5 ~]# systemctl start mariadb
[root@node6 ~]# systemctl start mariadb

可以查看/var/log/mariadb/mariadb.log,在日志可以看到节点均加入了集群中。

警告?:--wsrep-new-cluster 这个参数只能在初始化集群使用,且只能在一个节点使用。

5.查看集群状态

技术分享

我们可以关注几个关键的参数:

wsrep_connected = on 链接已开启

wsrep_local_index = 1在集群中的索引值

wsrep_cluster_size =3集群中节点的数量

wsrep_incoming_addresses = 10.128.20.17:3306,10.128.20.16:3306,10.128.20.18:3306 集群中节点的访问地址

6.验证数据同步

我们在node4上新建数据库 galera_test ,然后在node5 和node6 上查询,如果可以查询到 galera_test 这个库,说明数据同步成功,集群运行正常。

[root@node4 ~]# mysql  -uroot  -proot  -e  "create database galera_test"
[root@node5 ~]# mysql  -uroot  -proot  -e  "show databases"
+--------------------+
| Database           |
+--------------------+
| information_schema |
| galera_test        |
| mysql              |
| performance_schema |
+--------------------+
[root@node6 ~]# mysql  -uroot  -proot  -e  "show databases"
+--------------------+
| Database           |
+--------------------+
| information_schema |
| galera_test        |
| mysql              |
| performance_schema |
+--------------------+

至此,我们的 MariaDB Galera Cluster 已经成功部署。

MariaDB Galera Cluster 部署(如何快速部署 MariaDB 集群)

标签:

人气教程排行