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架构图

MySQL部署PXC集群-全网最详细_第1张图片

二,开始部署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/hosts
192.168.2.1 my01
192.168.2.2 my02
192.168.2.3 my03

3.安装软件

percona-xtrabackup和Percona-XtraDB-Cluster登录下面地址找到相关版本来进行下载软件包
下载地址:https://www.percona.com/downloads

qpress-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/src/
[root@my01 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 src]# ls
percona-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 src]# scp -r /usr/local/src/ 192.168.2.2:/usr/local/
[root@my01 src]# scp -r /usr/local/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 src]# yum -y localinstall percona-xtrabackup-24-2.4.28-1.el7.x86_64.rpm

3.安装qpress(三台相同)

[root@my01 src]# yum -y localinstall qpress-1.1-14.11.x86_64.rpm

4.安装Percona-xtradb-cluster(三台相同)

[root@my01 src]# mkdir per-cluster
[root@my01 src]# tar -xf Percona-XtraDB-Cluster-5.7.41-31.65-r654-el7-x86_64-bundle.tar -C per-cluster/
[root@my01 src]# cd per-cluster/
[root@my01 per-cluster]# ls
Percona-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.rpm
Percona-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.rpm
Percona-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.rpm
Percona-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.rpm
Percona-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/mysql
socket=/var/lib/mysql/mysql.soc
pid-file=/var/run/mysqld/mysqld.pid
log-bin
log_slave_updates
expire_logs_days=7

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-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 library
wsrep_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 node
wsrep_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 ROW
binlog_format=ROW

# MyISAM storage engine has only experimental support
default_storage_engine=InnoDB

# 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=192.168.2.1
# Cluster name
wsrep_cluster_name=pxc-cluster

#If wsrep_node_name is not specified,  then system hostname will be used
wsrep_node_name=my01

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

# SST method
wsrep_sst_method=xtrabackup-v2

#Authentication for SST method
wsrep_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/
[email protected]'s password:
wsrep.cnf                                                                                                                100% 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/
[email protected]'s password:
wsrep.cnf                    
2.my02和my03的配置
[root@my02 per-cluster]# vi /etc/percona-xtradb-cluster.conf.d/wsrep.cnf
wsrep_node_address=192.168.2.2
wsrep_node_name=my02


[root@my03 per-cluster]# vi /etc/percona-xtradb-cluster.conf.d/wsrep.cnf
wsrep_node_address=192.168.2.3
wsrep_node_name=my03

5.服务启动

1.在my01上去执行

[root@my01 per-cluster]# systemctl start [email protected]

2.查询MySQL的初始化密码

[root@my01 per-cluster]#  grep pass /var/log/mysqld.log
2023-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/src/gcs_group.cpp:gcs_group_handle_join_msg():766: Will never receive state.

​ 解决:

  1. 检查SST用户的用户名和密码是否正确,并且有权限(wsrep_sst_auth)
  2. 检查防火墙(selinux, firewalld)
  3. 检查节点xtrabackup是否安装并且能正常备份
  4. 参数文件中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 配置文件中集群名称是否一致

你可能感兴趣的:(从精通到忘记学习数据库,mysql,linux,服务器)