PXC(Percona XtraDB Cluster)8.0.32集群

PXC简介

PXC是Percona XtraDB Cluster的缩写,是 Percona 公司出品的免费MySQL集群产品。PXC的作用是通过mysql自带的Galera集群技术,将不同的mysql实例连接起来,实现多主集群。在PXC集群中每个mysql节点都是可读可写的,也就是主从概念中的主节点,不存在只读的节点。
Percona Server是MySQL的改进版本,使用 XtraDB 存储引擎,在功能和性能上较 MySQL 有着很显著的提升,如提升了在高负载情况下的 InnoDB 的性能,为 DBA 提供了一些非常有用的性能诊断工具,另外有更多的参数和命令来控制服务器行为。
PXC 是一套 MySQL 高可用集群解决方案,与传统的基于主从复制模式的集群架构相比 PXC 最突出特点就是解决了诟病已久的数据复制延迟问题,基本上可以达到实时同步。而且节点与节点之间,他们相互的关系是对等的。PXC 最关注的是数据的一致性,对待事物的行为时,要么在所有节点上执行,要么都不执行,它的实现机制决定了它对待一致性的行为非常严格,这也能非常完美的保证 MySQL 集群的数据一致性;

PXC特性和优点

A. 完全兼容 MySQL。
B. 同步复制,事务要么在所有节点提交或不提交。
C. 多主复制,可以在任意节点进行写操作。
D. 在从服务器上并行应用事件,真正意义上的并行复制。
E. 节点自动配置,数据一致性,不再是异步复制。
F. 故障切换:因为支持多点写入,所以在出现数据库故障时可以很容易的进行故障切换。
G. 自动节点克隆:在新增节点或停机维护时,增量数据或基础数据不需要人工手动备份提供,galera cluster会自动拉取在线节点数据,集群最终会变为一致;

PXC最大的优势:强一致性、无同步延迟

PXC的局限和劣势

  1. 复制只支持InnoDB 引擎,其他存储引擎的更改不复制
  2. 写入效率取决于节点中最慢的一台

PXC 常用端口

  1. 3306:数据库对外服务的端口号。
  2. 4444:请求SST的端口。
  3. 4567:组成员之间进行沟通的一个端口号
  4. 4568:用于传输IST。

名词解释:
SST(State Snapshot Transfer): 全量传输
IST(Incremental state Transfer):增量传输

实验环境

节点 主机名 IP 描述
节点1 Rocky8-1 10.0.0.11 PXC集群节点1
节点2 Rocky8-2 10.0.0.12 PXC集群节点1
节点3 Rocky8-3 10.0.0.13 PXC集群节点1

检查初始环境:

三个节点均采用最小化安装,配置好yum源,关掉防火墙和selinux

1
2
3
4
5
6
7
8
9
10
11
12
13
14
## Linux版本
[root@Rocky8-2 ~]# cat /etc/centos-release
Rocky Linux release 8.8 (Green Obsidian)

## 防火墙
[root@Rocky8-2 ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)

## selinux
[root@Rocky8-2 ~]# sestatus
SELinux status: disabled

从 Percona 仓库安装

三个节点同时安装,以下以节点3为例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
## 配置yum源
[root@Rocky8-3 ~]# yum install -y https://repo.percona.com/yum/percona-release-latest.noarch.rpm
......
Installed:
percona-release-1.0-27.noarch
Complete!

## 设置仓库
[root@Rocky8-3 ~]# percona-release setup pxc80
Complete!
DNF mysql module was disabled
* Enabling the Percona XtraDB Cluster 8.0 repository
* Enabling the Percona Tools repository
<*> All done!

[root@Rocky8-3 ~]# yum clean all ; yum makecache

## 安装
[root@Rocky8-1 ~]# yum install -y percona-xtradb-cluster
Installed:
checkpolicy-2.9-1.el8.x86_64 compat-openssl10-1:1.0.2o-4.el8_6.x86_64
mariadb-connector-c-3.1.11-2.el8_3.x86_64 percona-xtradb-cluster-8.0.32-24.2.el8.x86_64
percona-xtradb-cluster-client-8.0.32-24.2.el8.x86_64 percona-xtradb-cluster-icu-data-files-8.0.32-24.2.el8.x86_64
percona-xtradb-cluster-server-8.0.32-24.2.el8.x86_64 percona-xtradb-cluster-shared-8.0.32-24.2.el8.x86_64
percona-xtradb-cluster-shared-compat-8.0.32-24.2.el8.x86_64 perl-DBD-MySQL-4.046-3.module+el8.6.0+904+ef468285.x86_64
perl-DBI-1.641-4.module+el8.6.0+891+677074cb.x86_64 perl-Math-BigInt-1:1.9998.11-7.el8.noarch
perl-Math-Complex-1.59-422.el8.noarch policycoreutils-python-utils-2.9-24.el8.noarch
python2-2.7.18-13.module+el8.8.0+1314+be03569e.1.rocky.0.2.x86_64 python2-libs-2.7.18-13.module+el8.8.0+1314+be03569e.1.rocky.0.2.x86_64
python2-pip-9.0.3-19.module+el8.6.0+793+57002515.noarch python2-pip-wheel-9.0.3-19.module+el8.6.0+793+57002515.noarch
python2-setuptools-39.0.1-13.module+el8.4.0+403+9ae17a31.noarch python2-setuptools-wheel-39.0.1-13.module+el8.4.0+403+9ae17a31.noarch
python3-audit-3.0.7-4.el8.x86_64 python3-libsemanage-2.9-9.el8_6.x86_64
python3-policycoreutils-2.9-24.el8.noarch python3-setools-4.3.0-3.el8.x86_64
qpress-20220819-3.el8.x86_64 socat-1.7.4.1-1.el8.x86_64
Complete!

初始化第一个节点的mysql服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
## 启动 Percona XtraDB Cluster 服务
[root@Rocky8-1 ~]# systemctl start mysqld

## 查找 mysql 数据库的 root 的临时密码
[root@Rocky8-1 ~]# grep 'temporary password' /var/log/mysqld.log
2023-07-14T03:27:22.398733Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: msobz!t#q18J

## 使用临时密码登录 MySQL
[root@Rocky8-1 ~]# mysql -uroot -p'msobz!t#q18J'

## 更改 root 用户的密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.04 sec)

## 重新登录mysql
[root@Rocky8-1 ~]# mysql -uroot -p'123456'
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 20
Server version: 8.0.32-24.2 Percona XtraDB Cluster (GPL), Release rel24, Revision 2119e75, WSREP version 26.1.4.3

Copyright (c) 2009-2023 Percona LLC and/or its affiliates
Copyright (c) 2000, 2023, 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>

## 关闭 MySQL 服务
[root@Rocky8-1 ~]# systemctl stop mysqld

初始化PXC的配置文件

1
[root@Rocky8-1 ~]# vim /etc/my.cnf

20230714133543

主要修改如下配置

1
2
3
4
5
6
7
8
9
# 集群通讯地址
wsrep_cluster_address=gcomm://10.0.0.11,10.0.0.12,10.0.0.13
# 本节点的地址
wsrep_node_address=10.0.0.11
# 本节点的名称
wsrep_node_name=pxc-cluster-node-1

## 在配置最后加上一行如下配置,作用是关闭传输加密
pxc-encrypt-cluster-traffic=OFF

注意:关闭传输加密,参考 Encrypting PXC Traffic
pxc-encrypt-cluster-traffic=OFF

配置文件参考:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
[root@Rocky8-1 ~]# cat /etc/my.cnf
# Template my.cnf for PXC
# Edit to your requirements.
[client]
socket=/var/lib/mysql/mysql.sock

[mysqld]
server-id=1
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

# Binary log expiration period is 604800 seconds, which equals 7 days
binlog_expire_logs_seconds=604800

######## wsrep ###############
# Path to Galera library
wsrep_provider=/usr/lib64/galera4/libgalera_smm.so

# Cluster connection URL contains IPs of nodes
#If no IP is found, this implies that a new cluster needs to be created,
#in order to do that you need to bootstrap this node
wsrep_cluster_address=gcomm://10.0.0.11,10.0.0.12,10.0.0.13

# In order for Galera to work correctly binlog format should be ROW
binlog_format=ROW

# Slave thread to use
wsrep_slave_threads=8

wsrep_log_conflicts

# This changes how InnoDB autoincrement locks are managed and is a requirement for Galera
innodb_autoinc_lock_mode=2

# Node IP address
wsrep_node_address=10.0.0.11
# Cluster name
wsrep_cluster_name=pxc-mysql

#If wsrep_node_name is not specified, then system hostname will be used
wsrep_node_name=pxc-mysql-node-1

#pxc_strict_mode allowed values: DISABLED,PERMISSIVE,ENFORCING,MASTER
pxc_strict_mode=ENFORCING

# SST method
wsrep_sst_method=xtrabackup-v2

pxc-encrypt-cluster-traffic=OFF
[root@Rocky8-1 ~]#

启动集群第一个节点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
## 启动集群第一个node(在前面初始化过mysql的节点执行)
[root@Rocky8-1 ~]# systemctl start mysql@bootstrap

## 登录mysql,查看集群状态
[root@Rocky8-1 ~]# mysql -uroot -p123456

mysql> show status like 'wsrep%';
+----------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+
| Variable_name | Value |
+----------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+
| wsrep_local_state_uuid | 5dcd8a83-21f6-11ee-a89b-1b2bd4cfd52b |
| wsrep_protocol_version | 10 |
| wsrep_last_applied | 3 |
| wsrep_last_committed | 3 |
| wsrep_monitor_status (L/A/C) | [ (2, 2), (3, 3), (3, 3) ] |
| wsrep_replicated | 0 |
| wsrep_replicated_bytes | 0 |
| wsrep_repl_keys | 0 |
| wsrep_repl_keys_bytes | 0 |
| wsrep_repl_data_bytes | 0 |
| wsrep_repl_other_bytes | 0 |
| wsrep_received | 2 |
| wsrep_received_bytes | 152 |
| wsrep_local_commits | 0 |
| wsrep_local_cert_failures | 0 |
| wsrep_local_replays | 0 |
| wsrep_local_send_queue | 0 |
| wsrep_local_send_queue_max | 2 |
| wsrep_local_send_queue_min | 0 |
| wsrep_local_send_queue_avg | 0.5 |
| wsrep_local_recv_queue | 0 |
| wsrep_local_recv_queue_max | 2 |
| wsrep_local_recv_queue_min | 0 |
| wsrep_local_recv_queue_avg | 0.5 |
| wsrep_local_cached_downto | 1 |
| wsrep_flow_control_paused_ns | 0 |
| wsrep_flow_control_paused | 0 |
| wsrep_flow_control_sent | 0 |
| wsrep_flow_control_recv | 0 |
| wsrep_flow_control_active | false |
| wsrep_flow_control_requested | false |
| wsrep_flow_control_interval | [ 100, 100 ] |
| wsrep_flow_control_interval_low | 100 |
| wsrep_flow_control_interval_high | 100 |
| wsrep_flow_control_status | OFF |
| wsrep_cert_deps_distance | 0 |
| wsrep_apply_oooe | 0 |
| wsrep_apply_oool | 0 |
| wsrep_apply_window | 0 |
| wsrep_apply_waits | 0 |
| wsrep_commit_oooe | 0 |
| wsrep_commit_oool | 0 |
| wsrep_commit_window | 0 |
| wsrep_local_state | 4 |
| wsrep_local_state_comment | Synced |
| wsrep_cert_index_size | 0 |
| wsrep_cert_bucket_count | 1 |
| wsrep_gcache_pool_size | 1912 |
| wsrep_causal_reads | 0 |
| wsrep_cert_interval | 0 |
| wsrep_open_transactions | 0 |
| wsrep_open_connections | 0 |
| wsrep_ist_receive_status | |
| wsrep_ist_receive_seqno_start | 0 |
| wsrep_ist_receive_seqno_current | 0 |
| wsrep_ist_receive_seqno_end | 0 |
| wsrep_incoming_addresses | 10.0.0.11:3306 |
| wsrep_cluster_weight | 1 |
| wsrep_desync_count | 0 |
| wsrep_evs_delayed | |
| wsrep_evs_evict_list | |
| wsrep_evs_repl_latency | 0/0/0/0/0 |
| wsrep_evs_state | OPERATIONAL |
| wsrep_gcomm_uuid | aa3fd407-2209-11ee-bbf7-c23e28e1454d |
| wsrep_gmcast_segment | 0 |
| wsrep_cluster_capabilities | |
| wsrep_cluster_conf_id | 1 |
| wsrep_cluster_size | 1 |
| wsrep_cluster_state_uuid | 5dcd8a83-21f6-11ee-a89b-1b2bd4cfd52b |
| wsrep_cluster_status | Primary |
| wsrep_connected | ON |
| wsrep_local_bf_aborts | 0 |
| wsrep_local_index | 0 |
| wsrep_provider_capabilities | :MULTI_MASTER:CERTIFICATION:PARALLEL_APPLYING:TRX_REPLAY:ISOLATION:PAUSE:CAUSAL_READS:INCREMENTAL_WRITESET:UNORDERED:PREORDERED:STREAMING:NBO: |
| wsrep_provider_name | Galera |
| wsrep_provider_vendor | Codership Oy <info@codership.com> (modified by Percona <https://percona.com/>) |
| wsrep_provider_version | 4.14(779b689) |
| wsrep_ready | ON |
| wsrep_thread_count | 9 |
+----------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+
79 rows in set (0.05 sec)

## 查看当前节点数量、状态
mysql> show status like 'wsrep_cluster_size';
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| wsrep_cluster_size | 1 |
+--------------------+-------+
1 row in set (0.00 sec)

mysql> show status like 'wsrep_cluster_status';
+----------------------+---------+
| Variable_name | Value |
+----------------------+---------+
| wsrep_cluster_status | Primary |
+----------------------+---------+
1 row in set (0.01 sec)

mysql> show status like 'wsrep_connected';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| wsrep_connected | ON |
+-----------------+-------+
1 row in set (0.00 sec)

添加其它节点

  1. 所有其他节点的数据和配置都会被第一个节点的数据覆盖
  2. 不要同时加入多个节点,避免数据或网络开销过大
1
[root@Rocky8-2 ~]# systemctl start mysqld
1
[root@Rocky8-3 ~]# systemctl start mysqld

查看集群状态

1
2
3
4
5
6
7
8
mysql> show status like 'wsrep_cluster_size';
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| wsrep_cluster_size | 3 |
+--------------------+-------+
1 row in set (0.00 sec)

停止从节点

1
2
3
4
5
6
7
8
9
10
[root@Rocky8-3 ~]# systemctl stop mysqld

## 集群状态
mysql> show status like 'wsrep_cluster_size';
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| wsrep_cluster_size | 2 |
+--------------------+-------+
1 row in set (0.00 sec)

停止主节点

1
[root@Rocky8-1 ~]# systemctl stop mysql@bootstrap

参考:

官方文档:Install Percona XtraDB Cluster - Percona XtraDB Cluster

CentOS 8 安装 Percona XtraDB Cluster 8.0 - Zhanming’s blog (qizhanming.com)

分布式高可用Mysql数据库Percona XtraDB Cluster 8.0 与 Proxysql 史上最详尽用法指南 - 个人文章 - SegmentFault 思否


PXC(Percona XtraDB Cluster)8.0.32集群
https://www.xcjyc.top/2023/07/14/MySQl多主集群PXC/
作者
XCJYC
发布于
2023年7月14日
许可协议