1、Galera Cluster介绍
- Galera是一个MySQL(也支持MariaDB,Percona)的同步多主集群软件。
- 主要功能
- 同步复制
- Active-active的多主拓扑结构
- 真正的multi-master,即所有节点可以同时读写数据库
- 自动成员资格控制,失败节点从群集中删除
- 新节点加入数据自动复制
- 真正的并行复制,行级
2、集群部署
1. 准备环境
1、主机解析
[root@admin ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.245.133 galera1
192.168.245.134 galera2
192.168.245.135 galera3
192.168.245.136 galera4
2、关闭防火墙和selinux
[root@admin ~]# setenforce 0 && systemctl stop firewalld
3、时间同步,修改时区
[root@admin ~]# ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
[root@admin ~]# timedatectl set-timezone Asia/Shanghai
4、准备时间服务器
[root@admin ~]# vim /etc/ntp.conf
server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 10
[root@admin ~]# systemctl restart ntpd
5、客户端同步时间
[root@galera2 ~]# ntpdate galera1
2. 安装Galera(每台都需要安装)
Galera有独立的补丁包,也有在mysql基础上打好补丁的完整包,我们要下载带wsrep扩展补丁的版本
删除原版本mysql
[root@wing yum.repos.d]# yum erase `rpm -qa | grep mysql` -y
配置yum源
[root@wing yum.repos.d]# cat galera.repo
[galera]
name=galera
baseurl=http://releases.galeracluster.com/mysql-wsrep-5.7/centos/7/x86_64/
enabled=1
gpgcheck=0
[root@wing yum.repos.d]# yum list | grep 'galera'
galera.x86_64 25.3.12-2.el7 epel
mysql-wsrep-5.7.x86_64 5.7.20-25.13.el7 galera
mysql-wsrep-client-5.7.x86_64 5.7.20-25.13.el7 galera
mysql-wsrep-common-5.7.x86_64 5.7.20-25.13.el7 galera
mysql-wsrep-devel-5.7.x86_64 5.7.20-25.13.el7 galera
mysql-wsrep-libs-5.7.x86_64 5.7.20-25.13.el7 galera
mysql-wsrep-libs-compat-5.7.x86_64 5.7.20-25.13.el7 galera
mysql-wsrep-server-5.7.x86_64 5.7.20-25.13.el7 galera
注意:需要epel源提供galera包
安装
[root@wing yum.repos.d]# yum install mysql-wsrep-5.7.x86_64 galera rsync -y
如果是本地安装,注意安装顺序
yum install -y \
mysql-wsrep-libs-compat-5.7-5.7.29-25.21.el7.x86_64.rpm \
mysql-wsrep-common-5.7-5.7.29-25.21.el7.x86_64.rpm \
mysql-wsrep-libs-5.7-5.7.29-25.21.el7.x86_64.rpm \
mysql-wsrep-client-5.7-5.7.29-25.21.el7.x86_64.rpm \
mysql-wsrep-server-5.7-5.7.29-25.21.el7.x86_64.rpm \
mysql-wsrep-devel-5.7-5.7.29-25.21.el7.x86_64.rpm \
galera-25.3.12-2.el7.x86_64.rpm \
boost-program-options-1.53.0-27.el7.x86_64.rpm \
nmap-ncat-6.40-19.el7.x86_64.rpm
特别注意:
1.timezone,ceph和rsync
2./var/lib/mysql下3个文件:grastate.dat gvwstate.dat galera.cache 如果出了错,把他们统统干掉
3.如果一起添到集群中不行就一个节点一个节点的添加
3. 修改密码
[root@galera1 ~]# mysql_pass=`grep 'password is generated' /var/log/mysqld.log |awk '{print $NF}' |awk 'END{print}'` && echo $mysql_pass
[root@galera1 ~]# mysqladmin -u root -p${mysql_pass} password 'llf123!'
[root@galera1 ~]# mysql -u root -p'llf123!'
4. 每台机器创建用于数据同步的用户
mysql> grant all on *.* to 'syncuser'@'%' identified by 'llf123456';
mysql> flush privileges;
5. 配置Galera Replication
galera1配置
[root@galera1 ~]# vim /etc/my.cnf
追加如下内容:
server-id=1
binlog_format=row
innodb_file_per_table=1
innodb_autoinc_lock_mode=2
wsrep_on=ON
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_cluster_name='galera'
wsrep_cluster_address='gcomm://'
wsrep_node_name='galera1'
wsrep_node_address='192.168.245.133'
wsrep_sst_auth=syncuser:'llf123456'
wsrep_sst_method=rsync
galera2配置
[root@galera1 ~]# vim /etc/my.cnf
server-id=2
binlog_format=row
innodb_file_per_table=1
innodb_autoinc_lock_mode=2
wsrep_on=ON
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_cluster_name='galera'
wsrep_cluster_address='gcomm://galera1,galera3,galera4'
wsrep_node_name='galera2'
wsrep_node_address='192.168.245.134'
wsrep_sst_auth=syncuser:'llf123456'
wsrep_sst_method=rsync
galera3配置
[root@galera1 ~]# vim /etc/my.cnf
server-id=3
binlog_format=row
innodb_file_per_table=1
innodb_autoinc_lock_mode=2
wsrep_on=ON
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_cluster_name='galera'
wsrep_cluster_address='gcomm://galera1,galera2,galera4'
wsrep_node_name='galera3'
wsrep_node_address='192.168.245.135'
wsrep_sst_auth=syncuser:'llf123456'
wsrep_sst_method=rsync
galera4配置
[root@galera1 ~]# vim /etc/my.cnf
server-id=4
binlog_format=row
innodb_file_per_table=1
innodb_autoinc_lock_mode=2
wsrep_on=ON
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_cluster_name='galera'
wsrep_cluster_address='gcomm://galera1,galera2,galera3'
wsrep_node_name='galera4'
wsrep_node_address='192.168.245.136'
wsrep_sst_auth=syncuser:'llf123456'
wsrep_sst_method=rsync
6. 重启服务:每台机器
[root@galera1 ~]# systemctl restart mysqld
7. 查看端口
[root@galera1 ~]# ss -auntpl | grep -E '3306|4567'
tcp LISTEN 0 128 *:4567 *:* users:(("mysqld",pid=11068,fd=12))
tcp LISTEN 0 80 :::3306 :::* users:(("mysqld",pid=11068,fd=39))
8. 测试
mysql> show status like 'wsrep%';
wsrep_incoming_addresses | 192.168.245.134:3306,192.168.245.135:3306,192.168.245.136:3306 |
3、阶段测试
在任何一台机器上写数据,在其他机器上全部会同步