MySQL部署PXC集群-全网最详细
MySQL部署PXC集群
一,PXC了解
1.PXC介绍
-
Percona XtraDB Cluster(简称PXC)
-
是基于Galera的MySQL高可用集群解决方案
-
Galera Cluster是Codership公司开发的一套免费开源的高可用方案
-
PXC集群主要由两部分组成:Percona Server with XtraDB(数据存储插件)和 Write Set Replication patches(同步、多主复制插件)
-
官网:http://galeracluster.com
2.PXC特点
-
数据强一致性,无同步延迟(写入主服务器的数据,所有从服务器必须马上也得有)
-
没有主从切换操作,无需使用虚拟IP(无需一主多从的结构,无需vip地址)
-
支持InnoDB存储引擎
-
多线程复制(多线程同步工作)
-
部署使用简单
-
支持节点自动加入,无需手动拷贝数据(服务器会自动同步宕机期间的数据,无需手动配置)
3.PXC架构图
二,开始部署PXC
1.实施环境
IP地址 | 主机名 |
---|---|
192.168.2.1 | my01 |
192.168.2.2 | my02 |
192.168.2.3 | my03 |
2.设置主机名和主机名解析
每一台都需要做
[root@localhost ~]# hostnamectl set-hostname my01[root@localhost ~]# bash[root@my01 ~]# vi /etc/hosts192.168.2.1 my01192.168.2.2 my02192.168.2.3 my03
3.安装软件
percona-xtrabackup和Percona-XtraDB-Cluster登录下面地址找到相关版本来进行下载软件包下载地址:https://www.percona.com/downloadsqpress-1.1-14.11.x86_64.rpm需要使用wget来下载
软件 | 作用 |
---|---|
percona-xtrabackup-24-2.4.28-1.el7.x86_64.rpm | 在线热备程序 |
qpress-1.1-14.11.x86_64.rpm | 递归压缩程序 |
Percona-XtraDB-Cluster-5.7.41-31.65-r654-el7-x86_64-bundle.tar | 集群服务程序 |
1.将所有软件包上传到服务器端(三台相同)
[root@my01 ~]# cd /usr/local/class="lazy" data-src/[root@my01 class="lazy" data-src]# wget ftp://ftp.pbone.net/mirror/ftp5.gwdg.de/pub/opensuse/repositories/home%3A/AndreasStieger%3A/branches%3A/Archiving/RedHat_RHEL-6/x86_64/qpress-1.1-14.11.x86_64.rpm[root@my01 class="lazy" data-src]# lspercona-xtrabackup-24-2.4.28-1.el7.x86_64.rpm Percona-XtraDB-Cluster-5.7.41-31.65-r654-el7-x86_64-bundle.tar qpress-1.1-14.11.x86_64.rpm#上传[root@my01 class="lazy" data-src]# scp -r /usr/local/class="lazy" data-src/ 192.168.2.2:/usr/local/[root@my01 class="lazy" data-src]# scp -r /usr/local/class="lazy" data-src/ 192.168.2.3:/usr/local/
2.安装percoa-xtrabackup(三台相同)
[root@my01 per-cluster]# yum -y install openssl[root@my01 per-cluster]# yum -y install openssl-devel[root@my01 per-cluster]# yum -y install socat[root@my01 class="lazy" data-src]# yum -y localinstall percona-xtrabackup-24-2.4.28-1.el7.x86_64.rpm
3.安装qpress(三台相同)
[root@my01 class="lazy" data-src]# yum -y localinstall qpress-1.1-14.11.x86_64.rpm
4.安装Percona-xtradb-cluster(三台相同)
[root@my01 class="lazy" data-src]# mkdir per-cluster[root@my01 class="lazy" data-src]# tar -xf Percona-XtraDB-Cluster-5.7.41-31.65-r654-el7-x86_64-bundle.tar -C per-cluster/[root@my01 class="lazy" data-src]# cd per-cluster/[root@my01 per-cluster]# lsPercona-XtraDB-Cluster-57-5.7.41-31.65.1.el7.x86_64.rpm Percona-XtraDB-Cluster-garbd-57-5.7.41-31.65.1.el7.x86_64.rpmPercona-XtraDB-Cluster-57-debuginfo-5.7.41-31.65.1.el7.x86_64.rpm Percona-XtraDB-Cluster-server-57-5.7.41-31.65.1.el7.x86_64.rpmPercona-XtraDB-Cluster-client-57-5.7.41-31.65.1.el7.x86_64.rpm Percona-XtraDB-Cluster-shared-57-5.7.41-31.65.1.el7.x86_64.rpmPercona-XtraDB-Cluster-devel-57-5.7.41-31.65.1.el7.x86_64.rpm Percona-XtraDB-Cluster-shared-compat-57-5.7.41-31.65.1.el7.x86_64.rpmPercona-XtraDB-Cluster-full-57-5.7.41-31.65.1.el7.x86_64.rpm Percona-XtraDB-Cluster-test-57-5.7.41-31.65.1.el7.x86_64.rpm[root@my01 per-cluster]# rpm -ivh ./* --nodeps --force
4.修改配置文件
1.修改mysql的配置(三台都需要修改)
[root@my01 per-cluster]# vi /etc/percona-xtradb-cluster.conf.d/mysqld.cnf# Template my.cnf for PXC# Edit to your requirements.[client]socket=/var/lib/mysql/mysql.sock[mysqld]server-id=1#只需要修改这里 保证每台都不同datadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.socpid-file=/var/run/mysqld/mysqld.pidlog-binlog_slave_updatesexpire_logs_days=7# Disabling symbolic-links is recommended to prevent assorted security riskssymbolic-links=0
2.修改PXC集群配置文件(三台都需要修改)
配置要求
wsrep_cluster_address=gcomm:// #集群成员列表,3台必须相同wsrep_node_address=192.168.70.63 #本机IP地址wsrep_cluster_name=pxc-cluster #集群名称,可自定义,3台必须相同wsrep_node_name=pxc-cluster-node #本机主机名wsrep_sst_auth="sstuser:s3cretPass" #SST数据同步用户授权,3台必须相同
1.my01的配置
[root@my03 per-cluster]# vi /etc/percona-xtradb-cluster.conf.d/wsrep.cnf[mysqld]# Path to Galera librarywsrep_provider=/usr/lib64/galera3/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 nodewsrep_cluster_address=gcomm://192.168.2.1,192.168.2.2,192.168.2.3# In order for Galera to work correctly binlog format should be ROWbinlog_format=ROW# MyISAM storage engine has only experimental supportdefault_storage_engine=InnoDB# Slave thread to usewsrep_slave_threads= 8wsrep_log_conflicts# This changes how InnoDB autoincrement locks are managed and is a requirement for Galerainnodb_autoinc_lock_mode=2# Node IP addresswsrep_node_address=192.168.2.1# Cluster namewsrep_cluster_name=pxc-cluster#If wsrep_node_name is not specified, then system hostname will be usedwsrep_node_name=my01#pxc_strict_mode allowed values: DISABLED,PERMISSIVE,ENFORCING,MASTERpxc_strict_mode=ENFORCING# SST methodwsrep_sst_method=xtrabackup-v2#Authentication for SST methodwsrep_sst_auth="sstuser:1234.Com"
将配置文件上传到其他服务器
[root@my01 per-cluster]# scp -p /etc/percona-xtradb-cluster.conf.d/wsrep.cnf 192.168.2.2:/etc/percona-xtradb-cluster.conf.d/root@192.168.2.2's password:wsrep.cnf100% 1081 1.1KB/s 00:00[root@my01 per-cluster]# scp -p /etc/percona-xtradb-cluster.conf.d/wsrep.cnf 192.168.2.3:/etc/percona-xtradb-cluster.conf.d/root@192.168.2.3's password:wsrep.cnf
2.my02和my03的配置
[root@my02 per-cluster]# vi /etc/percona-xtradb-cluster.conf.d/wsrep.cnfwsrep_node_address=192.168.2.2wsrep_node_name=my02[root@my03 per-cluster]# vi /etc/percona-xtradb-cluster.conf.d/wsrep.cnfwsrep_node_address=192.168.2.3wsrep_node_name=my03
5.服务启动
1.在my01上去执行
[root@my01 per-cluster]# systemctl start mysql@bootstrap.service
2.查询MySQL的初始化密码
[root@my01 per-cluster]# grep pass /var/log/mysqld.log2023-05-30T13:54:20.274979Z 1 [Note] A temporary password is generated for root@localhost: L(sz/!ua,6h0[root@my01 per-cluster]# mysql -uroot -p'L(sz/!ua,6h0'mysql> alter user root@"localhost" identified by "123456";Query OK, 0 rows affected (0.01 sec) mysql> grant reload,lock tables,replication client,process on *.* to sstuser@"localhost" identified by "12345.Com";Query OK, 0 rows affected, 1 warning (0.00 sec)
添加授权用户,数据会自动同步到主机my02和03上。reload装载数据的权限;lock tables锁表的权限;replication client查看服务状态的权限;process管理服务的权限(查看进程信息);授权用户和密码必须是配置文件中指定的。
3.其他两台服务上开启MySQL
[root@my02 per-cluster]# systemctl start mysql
三,测试配置
1.查看集群信息
mysql> show status like "%wsrep%"; wsrep_incoming_addresses | 192.168.2.3:3306,192.168.2.1:3306,192.168.2.2:3306 |#集群| wsrep_cluster_weight | 3 || 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 | 242efe13-ff69-11ed-9a0c-de8812f5f691 || wsrep_cluster_conf_id | 7 || wsrep_cluster_size | 3 |#集群服务器台数| wsrep_cluster_status | Primary |#集群状态| wsrep_connected | ON | #连接状态| wsrep_ready | ON | #服务状态
2.测试集群功能
- 在任意一台服务器上添加访问数据的授权用户
- 在客户端使用授权用户连接任意数据库服务器都可以存储数据,且可以查看到同样的数据
- 建表时,必须有主键字段
3.测试集群高可用
- 测试故障自动恢复
- 任何一台数据库服务器宕机都不影响用户存取数据
- 服务器运行后自动同步宕机期间的数据
四,常见错误
1.报错1
gcs/class="lazy" data-src/gcs_group.cpp:gcs_group_handle_join_msg():766: Will never receive state.
解决:
- 检查SST用户的用户名和密码是否正确,并且有权限(wsrep_sst_auth)
- 检查防火墙(selinux, firewalld)
- 检查节点xtrabackup是否安装并且能正常备份
- 参数文件中wsrep_node_address是否配置的是本机IP地址和其他配置
2.报错2
which: no socat in (/usr/sbin:/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin) 2023-05-31T02:58:56.691238Z WSREP_SST: [ERROR] ******************* FATAL ERROR ********************** 2023-05-31T02:58:56.691850Z WSREP_SST: [ERROR] socat not found in path: /usr/sbin:/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin 2023-05-31T02:58:56.692347Z WSREP_SST: [ERROR] ******************************************************2023-05-31T02:58:56.692551Z 0 [ERROR] WSREP: Failed to read 'ready ' from: wsrep_sst_xtrabackup-v2 --role 'joiner' --address '192.168.2.2' --datadir '/var/lib/mysql/' --defaults-file '/etc/my.cnf' --defaults-group-suffix '' --parent '18678' --mysqld-version '5.7.25-28-57' '' Read: '(null)'2023-05-31T02:58:56.692574Z 0 [ERROR] WSREP: Process completed with error: wsrep_sst_xtrabackup-v2 --role 'joiner' --address '192.168.2.2' --datadir '/var/lib/mysql/' --defaults-file '/etc/my.cnf' --defaults-group-suffix '' --parent '18678' --mysqld-version '5.7.25-28-57' '' : 2 (No such file or directory)2023-05-31T02:58:56.692617Z 2 [ERROR] WSREP: Failed to prepare for 'xtrabackup-v2' SST. Unrecoverable.2023-05-31T02:58:56.692622Z 2 [ERROR] Aborting
解决:
yum -y install socat
3.报错3
failed to open gcomm backend connection: 110
解决:
查看各主机的主机名与ip地址的映射关系
rectory)
2023-05-31T02:58:56.692617Z 2 [ERROR] WSREP: Failed to prepare for ‘xtrabackup-v2’ SST. Unrecoverable.
2023-05-31T02:58:56.692622Z 2 [ERROR] Aborting
解决:```bash yum -y install socat
3.报错3
failed to open gcomm backend connection: 110
解决:
查看各主机的主机名与ip地址的映射关系
查看各主机之间 wsrep.cnf 配置文件中集群名称是否一致
来源地址:https://blog.csdn.net/weixin_53678904/article/details/130966660
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341