Mysql

关注公众号 jb51net

关闭
首页 > 数据库 > Mysql > MySQL Galera Cluster部署

MySQL Galera Cluster部署与使用

作者:ZZDICT

MySQLGaleraCluster是一种高可用、同步复制的数据库解决方案,本文主要介绍了MySQL Galera Cluster 部署与使用,具有一定的参考价值,感兴趣的可以了解一下

MySQL Galera Cluster 是一个高可用、同步复制的数据库解决方案,基于 Galera Library 和 MySQL 数据库。它设计用于提供高可用性、负载均衡和数据一致性,特别适合需要高写入负载和多主节点支持的场景。

主要特点

组件

一. 环境准备

主机名IP系统软件版本配置信息
galera1192.168.226.31Rocky_linux9.4mysql-wsrep-8.0

galera-26.4.14-1

2核4G
galera2192.168.226.32Rocky_linux9.4mysql-wsrep-8.0

galera-26.4.14-1

2核4G
galera3192.168.226.33Rocky_linux9.4mysql-wsrep-8.0

galera-26.4.14-1

2核4G
galera4192.168.226.34Rocky_linux9.4mysql-wsrep-8.0

galera-26.4.14-1

2核4G

同意关闭防火墙和selinux,进行时间同步。

 主机解析:

[root@galera1 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.226.32 galera2
192.168.226.33 galera3
192.168.226.34 galera4
[root@galera2 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.226.31 galera1
192.168.226.33 galera3
192.168.226.34 galera4
[root@galera3 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.226.32 galera2
192.168.226.31 galera1
192.168.226.34 galera4
[root@galera4 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.226.32 galera2
192.168.226.33 galera3
192.168.226.31 galera1

四台虚拟机都导入创建该服务的yum源

[root@galera1 ~]# cat /etc/yum.repos.d/mysql-wsrep.repo
[galera]
name=galera
baseurl=http://releases.galeracluster.com/mysql-wsrep-8.0/redhat/9/x86_64/
enabled=1
gpgcheck=0
[root@galera1 ~]# yum clean all

四台虚拟机都下载mysql-wsrep-8.0和galera

[root@galera1 ~]# yum install -y mysql-wsrep-8.0 galera

mysql-wsrep-8.0:

二. 配置 

四台虚拟器都启动服务,并改个密码

[root@galera1 ~]# systemctl start mysqld
# 获取数据库初始化密码的步骤
[root@galera1 ~]# password=$(grep "temporary password" /var/log/mysqld.log | awk -F': ' '{print $2}')
[root@galera1 ~]# echo $password
D.s#wfreZ8L%

#改密码
[root@galera1 ~]# mysqladmin -p"$password" password "Qaz123456+"

四台服务都配置一个远程用户并授权,采用脚本方式执行

[root@galera1 ~]# vim user.sh   #每台都要执行一次,用来创建远程用户并授权
#!/bin/bash

# MySQL 连接参数
MYSQL_USER="root"
MYSQL_PASS="Qaz123456+"  # 替换为实际的 root 密码
MYSQL_HOST="localhost"           # 或者使用 IP 地址

# 创建远程用户和授予权限的 SQL 命令
SQL_COMMANDS="
CREATE USER 'syncuser'@'%' IDENTIFIED BY 'Qaz123456+';
GRANT ALL PRIVILEGES ON *.* TO 'syncuser'@'%';
FLUSH PRIVILEGES;
"

# 执行 SQL 命令
mysql -u "$MYSQL_USER" -p"$MYSQL_PASS" -h "$MYSQL_HOST" -e "$SQL_COMMANDS"
[root@galera1 ~]# sh user.sh

四台虚拟机服务都先停止

[root@galera1 ~]# systemctl stop mysqld

1. 配置 galera1 主机的my.cnf的文件

[root@galera1 ~]# vim /etc/my.cnf   #在最后行增加即可
server-id=1  # 服务器 ID,用于唯一标识 MySQL 服务器
binlog_format=row  # 二进制日志格式,行级别
innodb_file_per_table=1  # 为每个 InnoDB 表使用一个独立的表空间文件
innodb_autoinc_lock_mode=2  # 自增锁模式,2表示更高效的锁模式

wsrep_on=ON  # 启用 Galera 集群
wsrep_provider=/usr/lib64/galera/libgalera_smm.so  # Galera 提供者库的路径
wsrep_cluster_name='galera'  # Galera 集群的名称
wsrep_cluster_address='gcomm://'  # Galera 集群的地址,通常为 `gcomm://` 表示集群初始节点
wsrep_node_name='galera1'  # 当前节点的名称
wsrep_node_address='192.168.226.31'  # 当前节点的 IP 地址
wsrep_sst_auth=syncuser:'Qaz123456+'  # SST(状态快照传输)认证信息
wsrep_sst_method=rsync  # SST 方法,使用 rsync 进行状态快照传输

启动galera1主机的mysql

[root@galera1 ~]# systemctl start mysqld
[root@galera1 ~]# ss -tnlp
State               Recv-Q              Send-Q                            Local Address:Port                              Peer Address:Port              Process                                         
LISTEN              0                   4096                                    0.0.0.0:4567                                   0.0.0.0:*                  users:(("mysqld",pid=5145,fd=9))               
LISTEN              0                   128                                     0.0.0.0:22                                     0.0.0.0:*                  users:(("sshd",pid=819,fd=3))                  
LISTEN              0                   70                                            *:33060                                        *:*                  users:(("mysqld",pid=5145,fd=33))              
LISTEN              0                   128                                        [::]:22                                        [::]:*                  users:(("sshd",pid=819,fd=4))                  
LISTEN              0                   151                                           *:3306                                         *:*                  users:(("mysqld",pid=5145,fd=35))  


4567: 用于 Galera 集群的内部通信。这是 MySQL 数据库的默认端口,用于客户端连接 MySQL 数据库。所有的 SQL 查询和数据库操作通过这个端口进行。
33060: 用于 MySQL X Protocol。这是 MySQL 的 X Plugin 端口,主要用于 MySQL 的 X Protocol (MySQL Shell、MySQL Router 和其他 MySQL X API 客户端) 的连接。
3306: 用于 MySQL 数据库客户端连接。这是 MySQL 数据库的默认端口,用于客户端连接 MySQL 数据库。所有的 SQL 查询和数据库操作通过这个端口进行。

2. 配置 galera2 主机的my.cnf的文件

[root@galera2 ~]# systemctl start mysqld
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.226.32'
wsrep_sst_auth=syncuser:'Qaz123456+'
wsrep_sst_method=rsync

启动galera2主机的mysql

[root@galera2 ~]# systemctl start mysqld

3. 配置 galera3 主机的my.cnf的文件

[root@galera3 ~]# 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,galera3,galera4'
wsrep_node_name='galera2'
wsrep_node_address='192.168.226.33'
wsrep_sst_auth=syncuser:'Qaz123456+'
wsrep_sst_method=rsync

启动galera3主机的mysql 

[root@galera3 ~]# systemctl start mysqld

4. 在给galera1 主机的my.cnf的文件增加节点

[root@galera1 ~]# vim /etc/my.cnf  #增加上节点,即修改这行内容如下,或见下图所示:

wsrep_cluster_address='gcomm://galera2,galera3,galera4'  # Galera 集群的地址,通常为 `gcomm://` 表示集群初始节点

 重启动galera1主机的mysql 

[root@galera1 ~]# systemctl restart mysqld

5. 写入数据验证同步

在 galera1主机写入数据,创建一个库

[root@galera1 ~]# mysql -uroot -p"Qaz123456+"
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 8.0.37 Galera Cluster for MySQL

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> CREATE DATABASE database_name;
Query OK, 1 row affected (0.00 sec)

在 galera2和galera3主机查看

[root@galera2 ~]# mysql -uroot -p"Qaz123456+"
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 8.0.37 Galera Cluster for MySQL

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| database_name      |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)
[root@galera3 ~]# mysql -uroot -p"Qaz123456+"
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 8.0.37 Galera Cluster for MySQL

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| database_name      |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

可以看到在 galera1主机创建的库,同步写入到了其他节点的mysql中。

6. 配置 galera4 主机的my.cnf的文件

[root@galera4 ~]# 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='galera1'
wsrep_node_address='192.168.226.34'
wsrep_sst_auth=syncuser:'Qaz123456+'
wsrep_sst_method=rsync

 启动galera4主机的mysql  

[root@galera4 ~]# systemctl start mysqld

登录数据库查看数据 

[root@galera4 ~]# mysql -uroot -p"Qaz123456+"
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 8.0.37 Galera Cluster for MySQL

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| database_name      |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

如此验证成功,新启动的mysq即为新加入集群的msyql,启动即发现,并完成同步数据。 

MySQL Galera Cluster 和传统My SQL的区别

1. 集群架构

传统 MySQL:

MySQL Galera Cluster:

2. 一致性和容错

传统 MySQL:

MySQL Galera Cluster:

3. 复制机制

传统 MySQL:

MySQL Galera Cluster:

4. 配置和管理

传统 MySQL:

MySQL Galera Cluster:

5. 事务处理

传统 MySQL:

MySQL Galera Cluster:

6. 数据完整性

传统 MySQL:

MySQL Galera Cluster:

扩展:

如果节点全部关闭,重新都起来的解决办法:

现在将四个节点全部停止,再起来mysql就都起不来了,这是因为集群的特点,并且已经有了集群的信息数据。只需要将一个节点的集群数据删除即可。

[root@galera1 ~]# rm -rf /var/lib/mysql/g*

 在删除这个节点配置文件中配置的其他的节点名

[root@galera1 ~]# vim /etc/my.cnf

将这个行修改wsrep_cluster_address='gcomm://galera2,galera3,galera4' 
修改为此行wsrep_cluster_address='gcomm://'

然后启动这个节点服务

[root@galera1 ~]# systemctl start mysqld

但这个几点起来后,在将其余几个节点启动就可以了。

[root@galera2 ~]# systemctl start mysqld
[root@galera3 ~]# systemctl start mysqld
[root@galera4 ~]# systemctl start mysqld

然后在把刚删除galera1的配置文件中节点名再添加回去并重启服务即可。

[root@galera1 ~]# vim /etc/my.cnf

再将这个行修改wsrep_cluster_address='gcomm://'
修改为此行wsrep_cluster_address='gcomm://galera2,galera3,galera4' 
[root@galera1 ~]# systemctl restart mysqld

这是查验数据数据是否还完整

[root@galera1 ~]# mysql -uroot -p"Qaz123456+"
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 8.0.37 Galera Cluster for MySQL

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| database_name      |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

可以看到这样数据依旧存在。

到此这篇关于MySQL Galera Cluster部署与使用的文章就介绍到这了,更多相关MySQL Galera Cluster部署内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文