MySQL部署PXC集群
一,PXC了解
1.PXC介绍
- Percona XtraDB Cluster(简称PXC)
- 是基于Galera的MySQL高可用集群解决方案
- Galera Cluster是Codership公司开发的一套免费开源的高可用方案
- PXC集群主要由两部分组成:Percona Server with XtraDB(数据存储插件)和 Write Set Replication patches(同步、多主复制插件)
2.PXC特点
- 数据强一致性,无同步延迟(写入主服务器的数据,所有从服务器必须马上也得有)
- 没有主从切换操作,无需使用虚拟IP(无需一主多从的结构,无需vip地址)
- 支持InnoDB存储引擎
- 多线程复制(多线程同步工作)
- 部署使用简单
- 支持节点自动加入,无需手动拷贝数据(服务器会自动同步宕机期间的数据,无需手动配置)
二,开始部署PXC
1.实施环境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.安装软件
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 /usr/local/src/ 192.168.2.2:/usr/local/ [root@my01 src]# scp /usr/local/src/ 192.168.2.3:/usr/local/
2.安装percoa-xtrabackup(三台相同)
[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 /usr/local/src/ 192.168.2.2:/usr/local/ [root@my01 src]# scp /usr/local/src/ 192.168.2.3:/usr/local/
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"
5.服务启动
1.在my01上去执行
[root@my01 per-cluster]# systemctl start mysql@bootstrap.service • 1
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@"%" identified by "1234.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.查看集群信息
show status like "%wsrep%";
2.测试集群功能
- 在任意一台服务器上添加访问数据的授权用户
- 在客户端使用授权用户连接任意数据库服务器都可以存储数据,且可以查看到同样的数据
- 建表时,必须有主键字段
3.测试集群高可用
- 测试故障自动恢复
- 任何一台数据库服务器宕机都不影响用户存取数据
- 服务器运行后自动同步宕机期间的数据