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

MariaDB作为Mysql的一个分支,在开源项目中已经广泛使用,例如大热的openstack,所以,为了保证服务的高可用性,同时提高系统的负载能力,集群部署是必不可少的。

MariaDB Galera Cluster 介绍

MariaDB集群是MariaDB同步多主机集群。它仅支持XtraDB/ InnoDB存储引擎(虽然有对MyISAM实验支持 - 看wsrep_replicate_myisam系统变量)。

主要功能:

  • 同步复制
  • 真正的multi-master,即所有节点可以同时读写数据库
  • 自动的节点成员控制,失效节点自动被清除
  • 新节点加入数据自动复制
  • 真正的并行复制,行级
  • 用户可以直接连接集群,使用感受上与MySQL完全一致

优势:

  • 因为是多主,所以不存在Slavelag(延迟)
  • 不存在丢失事务的情况
  • 同时具有读和写的扩展能力
  • 更小的客户端延迟
  • 节点间数据是同步的,而Master/Slave模式是异步的,不同slave上的binlog可能是不同的

技术:

Galera集群的复制功能基于Galeralibrary实现,为了让MySQL与Galera library通讯,特别针对MySQL开发了wsrep API。

Galera插件保证集群同步数据,保持数据的一致性,靠的就是可认证的复制,工作原理如下图:

当客户端发出一个commit的指令,在事务被提交之前,所有对数据库的更改都会被write-set收集起来,并且将 write-set 纪录的内容发送给其他节点。

write-set 将在每个节点进行认证测试,测试结果决定着节点是否应用write-set更改数据。

如果认证测试失败,节点将丢弃 write-set ;如果认证测试成功,则事务提交。

1.安装环境准备

安装MariaDB集群至少需要3台服务器(如果只有两台的话需要特殊配置,请参照官方文档)

在这里,我列出试验机器的配置:

操作系统版本:centos7

node4:10.128.20.16 node5:10.128.20.17 node6:10.128.20.18

以第一行为例,node4为 hostname ,10.128.20.16为 ip ,在三台机器修改 /etc/hosts文件,我的文件如下:

10.128.20.16 node4
10.128.20.17 node5
10.128.20.18 node6

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

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

systemctl stop firewalld

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

2.安装 MariaDB Galera Cluster

[[email protected] ~]# yum install -y mariadb mariadb-galera-server mariadb-galera-common galera rsync
[[email protected] ~]# yum install -y mariadb mariadb-galera-server mariadb-galera-common galera rsync
[[email protected] ~]# yum install -y mariadb mariadb-galera-server mariadb-galera-common galera rsync

3.配置 MariaDB Galera Cluster

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

[[email protected] mariadb]# systemctl start mariadb
[[email protected] 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

[[email protected] mariadb]# systemctl stop mariadb
[[email protected] ~]# 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_namewsrep_node_address改成相应节点的 hostnameip

4.启动 MariaDB Galera Cluster 服务

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

观察日志:

[[email protected] ~]# 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 ,证明我们启动成功,继续启动其他节点:

[[email protected] ~]# systemctl start mariadb
[[email protected] ~]# 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 ,然后在node5node6 上查询,如果可以查询到 galera_test 这个库,说明数据同步成功,集群运行正常。

[[email protected] ~]# mysql  -uroot  -proot  -e  "create database galera_test"
[[email protected] ~]# mysql  -uroot  -proot  -e  "show databases"
+--------------------+
| Database           |
+--------------------+
| information_schema |
| galera_test        |
| mysql              |
| performance_schema |
+--------------------+
[[email protected] ~]# mysql  -uroot  -proot  -e  "show databases"
+--------------------+
| Database           |
+--------------------+
| information_schema |
| galera_test        |
| mysql              |
| performance_schema |
+--------------------+

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

参考文章:
[1]http://galeracluster.com/documentation-webpages/
[2]https://mariadb.com/kb/en/mariadb/getting-started-with-mariadb-galera-cluster/

本文系OneAPM工程师原创文章。OneAPM是中国基础软件领域的新兴领军企业,能帮助企业用户和开发者轻松实现:缓慢的程序代码和SQL语句的实时抓取。想阅读更多技术文章,请访问OneAPM官方技术博客

时间: 2024-12-22 08:36:39

MariaDB Galera Cluster 部署(如何快速部署MariaDB集群)的相关文章

MariaDB Galera Cluster(mariadb10.1.22)部署

一.Galera Cluster简介 1.1 galera cluster 介绍 Galera Cluster是MariaDB的一个双活多主集群,其可以使得MariDB的所有节点保持同步,Galera为MariaDB提供了同步复制(相对于原生的异步复制),因此其可以保证HA,且其当前仅支持XtraDB/InnoDB存储引擎(扩展支持MyISAM),并且只可在Linux下使用.从MariaDB 10.1开始,在Galera Cluster中默认已经包含了wsrep API.在MariaDB 10.

CentOS7+MySQL/MariaDB+Galera+HAProxy+Keepalived构建高可用数据库集群

方案优势: Galera能够实现MySQL/MariaDB数据库的主主复制和多主复制等模式,这些复制模式都是同步进行的,同步时间非常短 每一个节点都可以同时写入和读取,当某一节点发生故障时,可自动从集群中自动剔除 HAProxy能提供负载均衡和故障判断等功能解决服务器系统存在的单点故障 Keepalived能提供客户端连接数据库时使用的虚拟IP地址(VIP) 关于HAProxy的负载均衡算法 轮询方式(roundrobin):不适合用于backend为web服务器的情况,因为session.co

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

MariaDB Galera Cluster 部署(如何快速部署 MariaDB 集群)  OneAPM蓝海讯通7月3日 发布 推荐 4 推荐 收藏 14 收藏,1.1k 浏览 MariaDB 作为 Mysql 的一个分支,在开源项目中已经广泛使用,例如大热的 openstack,所以,为了保证服务的高可用性,同时提高系统的负载能力,集群部署是必不可少的. MariaDB Galera Cluster 介绍 MariaDB 集群是 MariaDB 同步多主机集群.它仅支持 XtraDB/ Inn

MariaDB Galera Cluster 部署

原文  http://code.oneapm.com/database/2015/07/02/mariadb-galera-cluster/MariaDB作为Mysql的一个分支,在开源项目中已经广泛使用,例如大热的openstack,所以,为了保证服务的高可用性,同时提高系统的负载能力,集群部署是必不可少的. MariaDB Galera Cluster 介绍 MariaDB集群是MariaDB同步多主机集群.它仅支持XtraDB/ InnoDB存储引擎(虽然有对MyISAM实验支持 - 看w

MariaDB Galera Cluster集群企业版编译安装与配置

安装环境 系统:CentOS 6.8 x86_64 软件:MariaDB 10.1.16 节点一:192.168.11.132 4C 8GB 节点二:192.168.11.133 4C 8GB 软件获取 访问MariaDB企业版下载地址 https://mariadb.com/my_portal/download/mariadb-enterprise 登录帐号后选择 10.1.16GA版本 源代码包下载. 从MariaDB Enterprise 10.1版本开始,企业版软件包与集群功能集成到一起

Centos7安装mariadb galera cluster数据库集群 & 详解

#Galera集群特点 集群之间无延时,同步复制.而master-slave主从异步复制,存在延迟. active-active多主,集群内部服务器都是同时写,必须等所有集群内所有数据库都完成数据写入,才会反馈完成,所以不存在数据丢失的情况. 集群节点自动故障转移,如果集群中单个节点故障,失效节点会自动被清除. 扩展方便,只要将新的节点添加到集群,新节点自动复制数据. #Galera集群原理     #主要通过galera插件保证数据的一致性,该数据复制的过程是可认证的复制,原理如下: #解析

Install MariaDB Galera Cluster 10.0.20

安装环境: OS:Centos 6.6 [[email protected] ~]# uname -a Linux heartbeat1 2.6.32-504.el6.x86_64 #1 SMP Wed Oct 15 04:27:16 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux 数据库软件:MariaDB Galera Cluster 10.0.20 主机配置: Heartbeat1 10.0.0.7 Heartbeat2 10.0.0.8 Heartbeat

MariaDB Galera Cluster环境搭建及高可用测试

一.服务器概况Galera Cluster需要至少三个节点,在此次实验过程中,三个节点IP地址:192.168.56.101192.168.56.102192.168.56.103OS为centos 7.2服务器配置:4G 内存,2核CPU,20G数据磁盘空间. 关闭防火墙: service iptables stop systemctl stop firewalld chkconfig iptables off 关闭SELinux: /etc/selinux/config中的SELINUX=d

MariaDB Galera Cluster集群

一.MariaDB Galera Cluster概要: 1.简述: MariaDB Galera Cluster 是一套在mysql innodb存储引擎上面实现multi-master及数据实时同步的系统架构,业务层面无需做读写分离工作,数据库读写压力都能按照既定的规则分发到 各个节点上去.在数据方面完全兼容 MariaDB 和 MySQL. 2.特性: (1).同步复制 Synchronous replication (2).Active-active multi-master 拓扑逻辑 (