MySQL/MariaDB数据库的Galera高可用性集群实战
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.Galera Cluster概述
1>.什么是Galera Cluster
集成了Galera插件的MySQL集群,是一种新型的,数据不共享的,高度冗余的高可用方案,目前Galera Cluster有两个版本,分别是Percona Xtradb Cluster及MariaDB Cluster,Galera本身是具有多主特性的,即采用multi-master的集群架构,是一个既稳健,又在数据一致性、完整性及高性能方面有出色表现的高可用解决方案。
Galera Cluster官方文档:
http://galeracluster.com/documentation-webpages/galera-documentation.pdf
http://galeracluster.com/documentation-webpages/index.html
https://mariadb.com/kb/en/mariadb/getting-started-with-mariadb-galera-cluster/
Galera Cluster包括两个组件
Galera replication library (galera-3)
WSREP:MySQL extended with the Write Set Replication
WSREP复制实现:
PXC:Percona XtraDB Cluster,是Percona对Galera的实现
MariaDB Galera Cluster
官方仓库:https://downloads.mariadb.org/
参考仓库:https://downloads.mariadb.org/mariadb-galera/5.5.63/#file_type=rpm
注意:
这至少需要三个节点且安装的版本是有要求的,即只能安装支持Galera Cluster版本的软件,不要去安装mariadb-server。
2>.Galera Cluster架构
如下图所示:
三个节点组成了一个集群,与普通的主从架构不同,它们都可以作为主节点,三个节点是对等的,称为multi-master架构,当有客户端要写入或者读取数据时,连接哪个实例都是一样的,读到的数据是相同的,写入某一个节点之后,集群自己会将新数据同步到其它节点上面,这种架构不共享任何数据,是一种高冗余架构
![a2a78d78ecbe2e34aa1185465708a20d.png](https://img8.php1.cn/3cdc5/18ace/711/9e7dd93bb5abd2d9.png)
3>.Galera Cluster特点
多主架构:
真正的多点读写的集群,在任何时候读写数据,都是最新的
同步复制:
集群不同节点之间数据同步,没有延迟,在数据库挂掉之后,数据不会丢失
并发复制:
从节点APPLY数据时,支持并行执行,更好的性能
故障切换:
在出现数据库故障时,因支持多点写入,切换容易
热插拔:
在服务期间,如果数据库挂了,只要监控程序发现的够快,不可服务时间就会非常少。在节点故障期间,节点本身对集群的影响非常小
自动节点克隆:
在新增节点,或者停机维护时,增量数据或者基础数据不需要人工手动备份提供,Galera Cluster会自动拉取在线节点数据,最终集群会变为一致
对应用透明:
集群的维护,对应用程序是透明的
4>.Galera Cluster工作过程
![9b4aecc1297ee2ba0759e4cd31b84aef.png](https://img8.php1.cn/3cdc5/18ace/711/09b2cc5d2e38daab.png)
二.Galera Cluster部署实战
1>.配置yum源
[root@node101.yinzhengjie.org.cn ~]# vim /etc/yum.repos.d/mysql.repo
[root@node101.yinzhengjie.org.cn~]#
[root@node101.yinzhengjie.org.cn~]# cat /etc/yum.repos.d/mysql.repo
[mysql]
baseurl=http://ftp.hosteurope.de/mirror/archive.mariadb.org//mariadb-5.5.63/yum/centos7-amd64/gpgcheck=0[root@node101.yinzhengjie.org.cn~]#
[root@node101.yinzhengjie.org.cn~]# scp /etc/yum.repos.d/mysql.repo 172.30.1.102:/etc/yum.repos.d/mysql.repo100% 106 42.7KB/s 00:00[root@node101.yinzhengjie.org.cn~]#
[root@node101.yinzhengjie.org.cn~]# scp /etc/yum.repos.d/mysql.repo 172.30.1.103:/etc/yum.repos.d/mysql.repo100% 106 46.0KB/s 00:00[root@node101.yinzhengjie.org.cn~]#
![8f900a89c6347c561fdf2122f13be562.png](https://img8.php1.cn/3cdc5/18ace/711/f286da5b927938ea.gif)
![961ddebeb323a10fe0623af514929fc1.png](https://img8.php1.cn/3cdc5/18ace/711/4ee10d6eb4351741.gif)
[root@node101.yinzhengjie.org.cn ~]# yumclean all
Loaded plugins: fastestmirror
Repository'mysql' is missing name in configuration, using idCleaning repos: base extras mysql updates
Cleaning up list of fastest mirrors
[root@node101.yinzhengjie.org.cn~]#
[root@node101.yinzhengjie.org.cn ~]# yum clean all
![8f900a89c6347c561fdf2122f13be562.png](https://img8.php1.cn/3cdc5/18ace/711/f286da5b927938ea.gif)
![961ddebeb323a10fe0623af514929fc1.png](https://img8.php1.cn/3cdc5/18ace/711/4ee10d6eb4351741.gif)
[root@node101.yinzhengjie.org.cn ~]# yumrepolist
Loaded plugins: fastestmirror
Repository'mysql' is missing name in configuration, using idDetermining fastest mirrors*base: mirrors.huaweicloud.com*extras: mirrors.huaweicloud.com*updates: mirrors.huaweicloud.com
base| 3.6 kB 00:00:00extras| 2.9 kB 00:00:00mysql| 2.9 kB 00:00:00updates| 2.9 kB 00:00:00(1/5): extras/7/x86_64/primary_db | 153 kB 00:00:00(2/5): base/7/x86_64/group_gz | 165 kB 00:00:00(3/5): updates/7/x86_64/primary_db | 2.8 MB 00:00:01(4/5): base/7/x86_64/primary_db | 6.0 MB 00:00:01(5/5): mysql/primary_db | 43 kB 00:00:03repoidrepo name status
base/7/x86_64 CentOS-7 - Base 10,097extras/7/x86_64 CentOS-7 - Extras 305mysql mysql34updates/7/x86_64 CentOS-7 - Updates 711repolist:11,147[root@node101.yinzhengjie.org.cn~]#
[root@node101.yinzhengjie.org.cn ~]# yum repolist
![8f900a89c6347c561fdf2122f13be562.png](https://img8.php1.cn/3cdc5/18ace/711/f286da5b927938ea.gif)
![961ddebeb323a10fe0623af514929fc1.png](https://img8.php1.cn/3cdc5/18ace/711/4ee10d6eb4351741.gif)
[root@node101.yinzhengjie.org.cn ~]# grep keepcache /etc/yum.conf
keepcache=0[root@node101.yinzhengjie.org.cn~]#
[root@node101.yinzhengjie.org.cn~]# sed -i 's#keepcache=0#keepcache=1#' /etc/yum.conf
[root@node101.yinzhengjie.org.cn~]#
[root@node101.yinzhengjie.org.cn~]# grep keepcache /etc/yum.conf
keepcache=1[root@node101.yinzhengjie.org.cn~]#
[root@node101.yinzhengjie.org.cn~]#
[root@node101.yinzhengjie.org.cn ~]# sed -i 's#keepcache=0#keepcache=1#' /etc/yum.conf
![8f900a89c6347c561fdf2122f13be562.png](https://img8.php1.cn/3cdc5/18ace/711/f286da5b927938ea.gif)
![961ddebeb323a10fe0623af514929fc1.png](https://img8.php1.cn/3cdc5/18ace/711/4ee10d6eb4351741.gif)
[root@node102.yinzhengjie.org.cn ~]# yum list MariaDB-Galera-server #安装之前可以查看是否有该安装包
Loaded plugins: fastestmirror
Repository'mysql' is missing name in configuration, using idLoading mirror speeds from cached hostfile*base: mirror.jdcloud.com*extras: mirror.jdcloud.com*updates: mirror.jdcloud.com
Available Packages
MariaDB-Galera-server.x86_64 5.5.63-1.el7.centos mysql
[root@node102.yinzhengjie.org.cn~]#
[root@node102.yinzhengjie.org.cn ~]# yum list MariaDB-Galera-server #安装之前可以查看是否有该安装包
2>.安装MariaDB-Galera-server
[root@node102.yinzhengjie.org.cn ~]# yum -y install MariaDB-Galera-server
3>.编辑配置文件
vim /etc/my.cnf.d/server.cnf
[galera]
wsrep_provider= /usr/lib64/galera/libgalera_smm.so #指定的提供文件路径
wsrep_cluster_address="gcomm://172.30.1.101,172.30.1.102,172.30.1.103"#指定集群地址
binlog_format=row #二进制文件格式
default_storage_engine=InnoDB #指定默认的存储引擎
innodb_autoinc_lock_mode=2#加锁的模式
bind-address=0.0.0.0#绑定当前服务器的所有可用地址
下面配置可选项:
wsrep_cluster_name=‘mycluster‘ #默认名称为my_wsrep_cluster
wsrep_node_name= 'node1'#当前节点名称
wsrep_node_address= ‘192.168.8.7’ #当前节点的IP地址
4>.首次启动时,需要初始化集群,在其中一个节点上执行命令
/etc/init.d/mysql start --wsrep-new-cluster
5>.而后正常启动其它节点
service mysql start
6>.查看集群中相关系统变量和状态变量
SHOW VARIABLES LIKE 'wsrep_%‘;
SHOW STATUS LIKE 'wsrep_%‘;
SHOW STATUS LIKE 'wsrep_cluster_size‘;
7>.MariaDB-Galera-server注意事项
在数据量比较大的环境中,使用MariaDB-Galera-server要慎重啊,我们知道它是在所有数据库上都能更改,可能会面临数据冲突的问题。
一旦在MariaDB-Galera-server集群中发现数据冲突,它们就会踢掉一个不一致的节点,从而保证数据的一致性。