千家信息网

MySQL5.7之PXC集群搭建

发表于:2024-11-27 作者:千家信息网编辑
千家信息网最后更新 2024年11月27日,新建pxc001服务器:系统:centos7内存:2G磁盘:128GIP地址:192.168.0.201(第一节点)192.168.0.202192.168.0.203#vi /etc/selinux
千家信息网最后更新 2024年11月27日MySQL5.7之PXC集群搭建

新建pxc001服务器:
系统:centos7
内存:2G
磁盘:128G
IP地址:192.168.0.201(第一节点)
192.168.0.202
192.168.0.203

#vi /etc/selinux/config
SELINUX=disabled #修改该项为disabled
#执行命令 setenforce 0
#systemctl restart firewalld
开放防火墙这4个端口
firewall-cmd --add-port=4444/tcp --permanent
firewall-cmd --add-port=4567/tcp --permanent
firewall-cmd --add-port=4568/tcp --permanent
firewall-cmd --add-port=3306/tcp --permanent

安装集群
#yum -y install http://www.percona.com/downloads/percona-release/redhat/0.1-4/percona-release-0.1-4.noarch.rpm
#yum -y update percona-release
#yum -y install Percona-XtraDB-Cluster-57

启动mysql,并修改密码

service mysql start

grep 'temporary password' /var/log/mysqld.log

[Note] A temporary password is generated for root@localhost: u8E;M:,p_T&H
#mysql -uroot -pu8E;M:,p_T&H
mysql> update mysql.user set authentication_string=password('BingoData@1024.com') where user='root' and Host = 'localhost';

使用下面方式启动第一台机器(201服务器),这台叫引导节点。
只有这一台是这样启动。别的服务器用service mysql start启动
#ssh 192.168.0.201
#systemctl start mysql@bootstrap.service
进入mysql
#mysql -uroot -pBingoData@1024.com
#set wsrep_causal_reads =1; 避免各个节点的数据不一致,这种情况需要等待全同步复制 将此文件复制到master1、master2,注意要把 wsrep_node_name 和 wsrep_node_address 改成相应节点的 hostname 和 ip。

创建配置文件中对应的用户 所有节点的IP都要创建
#mysql -uroot -pBingoData@1024.com
mysql> grant all privileges on . to 'pxcuser'@'%' identified by 'BingoData@1024.com';
报错如下:
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
解决办法:
mysql> set password = password('BingoData@1024.com');
mysql> grant all privileges on . to 'pxcuser'@'%' identified by 'BingoData@1024.com';
mysql> grant all privileges on . to 'pxcuser'@'localhost' identified by 'BingoData@1024.com';
mysql> flush privileges;

进入202和203服务器配置
#ssh 192.168.0.203
注意203和202的启动方式
#service mysql start
创建配置文件中对应的用户 所有节点的IP都要创建
mysql> grant all privileges on . to 'pxcuser'@'%' identified by 'BingoData@1024.com';
mysql> grant all privileges on . to 'pxcuser'@'localhost' identified by 'BingoData@1024.com';
mysql> flush privileges;

注意:服务的启动和停止要对应
service mysql stop ------> 启动时用service mysql start
systemctl stop mysql@bootstrap.service -----> 启用是用 systemctl start mysql@bootstrap.service

注意避坑:/var/lib/mysql/grastate.dat文件中safe_to_bootstrap为1的服务器是主库,用主库的方式启动!!我们想让谁做主库也可以手动设置该服务器,而且三台集群的uuid也相同才可以启动。

seqno为-1表示已加入到集群,数据库关闭后seqno会变。
下面这台服务器201做主库启动后,seqno从4变成了-1,表示已经加入集群。

203服务器做从库一直无法启动。
启动方式是:service mysql start
报错信息如下#tail -fn 100 /var/log/mysqld.log
2020-01-10T09:25:56.982598Z 0 [Note] WSREP: (32d9017e, 'tcp://0.0.0.0:4567') turning message relay requesting off
2020-01-10T09:26:23.489499Z 0 [Note] WSREP: (32d9017e, 'tcp://0.0.0.0:4567') turning message relay requesting on, nonlive peers: tcp://192.168.0.202:4567
2020-01-10T09:26:24.489792Z 0 [Note] WSREP: (32d9017e, 'tcp://0.0.0.0:4567') reconnecting to 30d8eb06 (tcp://192.168.0.202:4567), attempt 0
2020-01-10T09:26:24.494967Z 0 [ERROR] WSREP: failed to open gcomm backend connection: 110: failed to reach primary view (pc.wait_prim_timeout): 110 (Connection timed out)
at gcomm/src/pc.cpp:connect():159
2020-01-10T09:26:24.495007Z 0 [ERROR] WSREP: gcs/src/gcs_core.cpp:gcs_core_open():209: Failed to open backend connection: -110 (Connection timed out)
2020-01-10T09:26:24.495376Z 0 [ERROR] WSREP: gcs/src/gcs.cpp:gcs_open():1514: Failed to open channel 'pxc-cluster-test' at 'gcomm://192.168.0.202,192.168.0.203,192.168.0.201': -110 (Connection timed out)
2020-01-10T09:26:24.495402Z 0 [ERROR] WSREP: gcs connect failed: Connection timed out
2020-01-10T09:26:24.495415Z 0 [ERROR] WSREP: Provider/Node (gcomm://192.168.0.202,192.168.0.203,192.168.0.201) failed to establish connection with cluster (reason: 7)
2020-01-10T09:26:24.495423Z 0 [ERROR] Aborting

解决办法:把203服务器的grastate.dat文件的uuid修改的和202,201一模一样就可以启动了。
safe_to_bootstrap: 设置为0
那么UUID是什么呢?
为什么同一个pxc集群的UUID是相同的呢?

验证集群:show status like 'wsrep%';

| wsrep_incoming_addresses | 192.168.0.202:3306,192.168.0.203:3306,192.168.0.201: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 | e9e76881-338f-11ea-90d6-3b07eeeaf3a4 |
| wsrep_cluster_conf_id | 3 |
| wsrep_cluster_size | 3 |
| wsrep_cluster_state_uuid | 9d5bc8a5-3374-11ea-9aac-b3599f4e37e4 |
| wsrep_cluster_status | Primary |
| wsrep_connected | ON |
| wsrep_local_bf_aborts | 0 |
| wsrep_local_index | 2 |
| wsrep_provider_name | Galera |
| wsrep_provider_vendor | Codership Oy |
| wsrep_provider_version | 3.41(rb3295e6) |
| wsrep_ready | ON

0