others linux服务器运维 django3 监控 k8s golang 数据库 大数据 前端 devops 理论基础 java oracle 运维日志

mariadb galera cluster centos7 yum 安装

访问量:1639 创建时间:2020-05-13

概述

MariaDB Galera集群是一个同步多主集群,只在Linux上运行。从MariaDB 10.1开始,它就成为了服务器的标准部分。本文安装版本mariadb-10.4 galera-4。

特征

优点

缺点

版本:在MariaDB 10.4和以后的版本中,MariaDB Galera集群使用了Galera 4

但是,实际上,同步数据库复制传统上是通过所谓的“两阶段提交”或分布式锁定来实现的,事实证明这很慢。同步复制的低性能和复杂性实施导致了异步复制仍然是数据库性能可伸缩性和可用性的主要手段的情况。广泛采用的开源数据库(例如MySQL或PostgreSQL)仅提供异步或半同步复制解决方案。

Galera的复制并非完全同步。有时称为虚拟同步复制

查看官方说明的局限性:请移步 查看官方使用Galera的使用实施注意事项:请移步

centos7 yum 安装galera cluster

192.168.117.128 192.168.117.129

[root@localhost ~]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.117.129 mg2
192.168.117.128 mg1
[root@localhost ~]# curl -sS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup |  sudo bash -s -- --mariadb-maxscale-version="2.4"  --mariadb-server-version="mariadb-10.4"
[info] Repository file successfully written to /etc/yum.repos.d/mariadb.repo
[info] Adding trusted package signing keys...
[info] Successfully added trusted package signing keys
[root@localhost ~]# yum install MariaDB-server MariaDB-client galera-4 MariaDB-backup 
[root@localhost ~]# vim /etc/my.cnf.d/server.cnf 
[galera]
# Mandatory settings
wsrep_on=ON
wsrep_provider=/usr/lib64/galera-4/libgalera_smm.so
wsrep_cluster_address="gcomm://192.168.117.128,192.168.117.129"
binlog_format=row
default_storage_engine=InnoDB
innodb_autoinc_lock_mode=2
innodb_flush_log_at_trx_commit=0
wsrep_cluster_name=galeracluster
#注意其他节点替换IP
wsrep_node_name=192.168.117.128
wsrep_node_address=192.168.117.128

官方系统变量配置地址

[root@localhost ~]# galera_new_cluster 
[root@localhost ~]# 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
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n] n
 ... skipping.

You already have your root account protected, so you can safely answer 'n'.

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

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!
[root@localhost ~]# systemctl start mariadb
[root@localhost ~]# systemctl enable mariadb

注意第二个节点启动不需要初始化mariadb,在第一个节点启动后第二个节点可以直接启动。

[root@localhost ~]# systemctl start mariadb
[root@localhost ~]# systemctl enable mariadb

重启集群

如果您同时关闭所有节点,则实际上已经终止了集群。当然,集群的数据仍然存在,但是正在运行的集群不再存在。发生这种情况时,您将需要重新引导集群。 在某些情况下,如果Galera检测到本机可能不是群集中最高级的节点,它将拒绝引导节点。Galera可以确定该节点是否不是集群中的最后一个要关闭的节点,或者该节点是否崩溃。在这些情况下,需要手动干预。

[root@localhost ~]# galera_new_cluster
#节点重新引导后会生成/var/lib/mysql/gvwstate.dat
[root@localhost ~]# find / -name gvwstate.dat
/var/lib/mysql/gvwstate.dat
[root@localhost ~]# systemctl start mariadb

查看集群状态

MariaDB [(none)]>  SHOW  GLOBAL  STATUS  LIKE  'wsrep_%'\G;
...省略输出...
MariaDB [(none)]> show status like "wsrep_incoming_addresses";
+--------------------------+-----------+
| Variable_name            | Value     |
+--------------------------+-----------+
| wsrep_incoming_addresses | AUTO,AUTO |
+--------------------------+-----------+
1 row in set (0.001 sec)

MariaDB [(none)]> show status like "wsrep_cluster_size";
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| wsrep_cluster_size | 2     |
+--------------------+-------+
1 row in set (0.001 sec)

节点加入集群数据同步

新节点加入集群会同步数据来初始化本节点。

如果某个节点仅离开集群一会儿,则IST通常比SST快。

集群端口

Galera Cluster需要访问以下端口:

登陆评论: 使用GITHUB登陆