MySQL之PXC

Posted 丶旋律

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL之PXC相关的知识,希望对你有一定的参考价值。

Galera Cluster特点

多主架构:真正的多点读写的集群,在任何时候读写数据,都是最新的
同步复制:集群不同节点之间数据同步,没有延迟,在数据库挂掉之后,数据不会丢失
并发复制:从节点APPLY数据时,支持并行执行,更好的性能
故障切换:在出现数据库故障时,因支持多点写入,切换容易
热插拔:在服务期间,如果数据库挂了,只要监控程序发现的够快,不可服务时间就会非常少。在
节点故障期间,节点本身对集群的影响非常小
自动节点克隆:在新增节点,或者停机维护时,增量数据或者基础数据不需要人工手动备份提供,
Galera Cluster会自动拉取在线节点数据,最终集群会变为一致
对应用透明:集群的维护,对应用程序是透明的

Galera Cluster 缺点

由于DDL 需全局验证通过,则集群性能由集群中最差性能节点决定(一般集群节点配置都是一样
的)
新节点加入或延后较大的节点重新加入需全量拷贝数据(SST,State Snapshot Transfer),作为
donor( 贡献者,如: 同步数据时的提供者)的节点在同步过程中无法提供读写
只支持innodb存储引擎的表

PXC最常使用如下4个端口号:

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

下面是MyCat结合PXC的架构图

PXC:Percona XtraDB Cluster,是Percona对Galera的实现
参考仓库:

https://mirrors.tuna.tsinghua.edu.cn/percona/release/$releasever/RPMS/$basearch

案例:Percona XtraDB Cluster(PXC 5.7)


1 环境准备

四台主机

pxc1:172.31.0.7
pxc2:172.31.0.17
pxc3:172.31.0.27
pxc4:172.31.0.37   # 这台是作为测试添加新机器到集群

OS 版本目前不支持CentOS 8

关闭防火墙和SELinux,保证时间同步
注意:如果已经安装mysql,必须卸载

此处使用清华大学yum源,官方源太慢了

[root@localhost ~]# vim /etc/yum.repos.d/pxc.repo
[percona]
name=percona-repo
baseurl=https://mirrors.tuna.tsinghua.edu.cn/percona/release/$releasever/RPMS/$basearch
enabled=1
gpgcheck=0

# 把镜像源拷贝到远端服务器
[root@localhost ~]# scp /etc/yum.repos.d/pxc.repo 172.31.0.17:/etc/yum.repos.d/
[root@localhost ~]# scp /etc/yum.repos.d/pxc.repo 172.31.0.27:/etc/yum.repos.d/

# 在三个节点都安装好PXC 5.7
[root@localhost ~]# yum install Percona-XtraDB-Cluster-57 -y
[root@localhost ~]# yum install Percona-XtraDB-Cluster-57 -y
[root@localhost ~]# yum install Percona-XtraDB-Cluster-57 -y

在各个节点上分别配置mysql及集群配置文件

/etc/my.cnf为主配置文件,当前版本中,其余的配置文件都放在/etc/percona-xtradb-cluster.conf.d目
录里,包括mysqld.cnf,mysqld_safe.cnf,wsrep.cnf 三个文件

# 主配置文件不需要修改
[root@localhost ~]# vim /etc/my.cnf

# 各个节点改id号即可
[root@localhost ~]# vim /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=17
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
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

# PXC的配置文件必须修改
# 172.31.0.17
[root@localhost ~]# grep -Ev "^#|^$" /etc/percona-xtradb-cluster.conf.d/wsrep.cnf 
[mysqld]
wsrep_provider=/usr/lib64/galera3/libgalera_smm.so
wsrep_cluster_address=gcomm://172.31.0.17,172.31.0.27,172.31.0.37   # 各个节点IP
binlog_format=ROW
default_storage_engine=InnoDB
wsrep_slave_threads= 8
wsrep_log_conflicts
innodb_autoinc_lock_mode=2
wsrep_node_address=172.31.0.17  # 本机ip
wsrep_cluster_name=pxc-cluster-m520
wsrep_node_name=pxc-cluster-node-1-m520 # 各个节点指定的ID
pxc_strict_mode=ENFORCING
wsrep_sst_method=xtrabackup-v2
wsrep_sst_auth="sstuser:s3cretPass"

# 172.31.0.27
[root@localhost ~]# grep -Ev "^#|^$" /etc/percona-xtradb-cluster.conf.d/wsrep.cnf 
[mysqld]
wsrep_provider=/usr/lib64/galera3/libgalera_smm.so
wsrep_cluster_address=gcomm://172.31.0.17,172.31.0.27,172.31.0.37
binlog_format=ROW
default_storage_engine=InnoDB
wsrep_slave_threads= 8
wsrep_log_conflicts
innodb_autoinc_lock_mode=2
wsrep_node_address=172.31.0.27
wsrep_cluster_name=pxc-cluster-m520
wsrep_node_name=pxc-cluster-node-2-m520   # 各个节点指定的ID
pxc_strict_mode=ENFORCING
wsrep_sst_method=xtrabackup-v2
wsrep_sst_auth="sstuser:s3cretPass" # 取消注释

# 172.31.0.37
[root@localhost ~]# grep -Ev "^#|^$" /etc/percona-xtradb-cluster.conf.d/wsrep.cnf 
[mysqld]
wsrep_provider=/usr/lib64/galera3/libgalera_smm.so
wsrep_cluster_address=gcomm://172.31.0.17,172.31.0.27,172.31.0.27
binlog_format=ROW
default_storage_engine=InnoDB
wsrep_slave_threads= 8
wsrep_log_conflicts
innodb_autoinc_lock_mode=2
wsrep_node_address=172.31.0.37
wsrep_cluster_name=pxc-cluster-m520
wsrep_node_name=pxc-cluster-node-3-m520
pxc_strict_mode=ENFORCING
wsrep_sst_method=xtrabackup-v2
wsrep_sst_auth="sstuser:s3cretPass"

注意:尽管Galera Cluster不再需要通过binlog的形式进行同步,但还是建议在配置文件中开启二进制日
志功能,原因是后期如果有新节点需要加入,老节点通过SST全量传输的方式向新节点传输数据,很可
能会拖垮集群性能,所以让新节点先通过binlog方式完成同步后再加入集群会是一种更好的选择

配置文件各项配置意义

配置说明
wsrep_provider指定Galera库的路径
wsrep_cluster_nameGalera集群的名称
wsrep_cluster_addressGalera集群中各节点地址。地址使用组通信协议gcomm://(groupcommunication)
wsrep_node_name本节点在Galera集群中的名称
wsrep_node_address本节点在Galera集群中的通信地址
wsrep_sst_methodstate_snapshot_transfer(SST)使用的传输方法,可用方法有mysqldump、rsync和xtrabackup,前两者在传输时都需要对Donor加全局只读锁(FLUSH TABLES WITH READLOCK),xtrabackup则不需要(它使用percona自己提供的backup lock)。强烈建议采用xtrabackup
wsrep_sst_auth在SST传输时需要用到的认证凭据,格式为:“用户:密码”
pxc_strict_mode是否限制PXC启用正在试用阶段的功能,ENFORCING是默认值,表示不启用
binlog_format二进制日志的格式。Galera只支持row格式的二进制日志
default_storage_engine指定默认存储引擎。Galera的复制功能只支持InnoDB
innodb_autoinc_lock_mode只能设置为2,设置为0或1时会无法正确处理死锁问题

启动PXC集群中任意的节点可以作为第一节点

[root@localhost ~]# systemctl start mysql@bootstrap.service

# 查看端口有4567和3306
[root@localhost ~]# ss -tunl
Netid  State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
tcp    LISTEN     0      128               *:22                            *:*                  
tcp    LISTEN     0      128               *:4567                          *:*                  
tcp    LISTEN     0      100       127.0.0.1:25                            *:*                  
tcp    LISTEN     0      80               :::3306                         :::*                  
tcp    LISTEN     0      128              :::22                           :::*                  
tcp    LISTEN     0      100             ::1:25                           :::* 

# 查看root密码
[root@localhost ~]# grep "temporary password" /var/log/mysqld.log 
2021-05-22T13:50:17.837284Z 1 [Note] A temporary password is generated for root@localhost: +Egpg69fijV%

[root@localhost ~]# mysql -uroot -p'+Egpg69fijV%'

# 修改root密码
mysql> alter user 'root'@'localhost' identified by 'centos';
Query OK, 0 rows affected (0.00 sec)

# 创建相关用户并授权
mysql> create user 'sstuser'@'localhost' identified by 's3cretPass';
Query OK, 0 rows affected (0.01 sec)

# 授权
mysql> grant reload,lock tables,process,replication client on *.* to 'sstuser'@'localhost';
Query OK, 0 rows affected (0.00 sec)

# 查看相关变量
mysql> show variables like 'wsrep%'\\G
*************************** 1. row ***************************
Variable_name: wsrep_OSU_method
        Value: TOI
*************************** 2. row ***************************
Variable_name: wsrep_RSU_commit_timeout
        Value: 5000
*************************** 3. row ***************************
Variable_name: wsrep_auto_increment_control
        Value: ON
*************************** 4. row ***************************
Variable_name: wsrep_causal_reads
        Value: OFF
*************************** 5. row ***************************
Variable_name: wsrep_certification_rules
        Value: strict
*************************** 6. row ***************************
Variable_name: wsrep_certify_nonPK
        Value: ON
*************************** 7. row ***************************
Variable_name: wsrep_cluster_address
        Value: gcomm://172.31.0.17,172.31.0.27,172.31.0.37
*************************** 8. row ***************************
Variable_name: wsrep_cluster_name
        Value: pxc-cluster
*************************** 9. row ***************************
Variable_name: wsrep_convert_LOCK_to_trx
        Value: OFF
*************************** 10. row ***************************
Variable_name: wsrep_data_home_dir
        Value: /var/lib/mysql/
*************************** 11. row ***************************
Variable_name: wsrep_dbug_option
        Value: 
*************************** 12. row ***************************
Variable_name: wsrep_debug
        Value: OFF
*************************** 13. row ***************************
Variable_name: wsrep_desync
        Value: OFF
*************************** 14. row ***************************
Variable_name: wsrep_dirty_reads
        Value: OFF
*************************** 15. row ***************************
Variable_name: wsrep_drupal_282555_workaround
        Value: OFF
*************************** 16. row ***************************
Variable_name: wsrep_forced_binlog_format
        Value: NONE
*************************** 17. row ***************************
Variable_name: wsrep_load_data_splitting
        Value: ON
*************************** 18. row ***************************
Variable_name: wsrep_log_conflicts
        Value: ON
*************************** 19. row ***************************
Variable_name: wsrep_max_ws_rows
        Value: 0
*************************** 20. row ***************************
Variable_name: wsrep_max_ws_size
        Value: 2147483647
*************************** 21. row ***************************
Variable_name: wsrep_mode
        Value: 
*************************** 22. row ***************************
Variable_name: wsrep_node_address
        Value: 172.31.0.17
*************************** 23. row ***************************
Variable_name: wsrep_node_incoming_address
        Value: AUTO
*************************** 24. row ***************************
Variable_name: wsrep_node_name
        Value: pxc-cluster-node-1
*************************** 25. row ***************************
Variable_name: wsrep_notify_cmd
        Value: 
*************************** 26. row ***************************
Variable_name: wsrep_on
        Value: ON
*************************** 27. row ***************************
Variable_name: wsrep_preordered
        Value: OFF
*************************** 28. row ***************************
Variable_name: wsrep_provider
        Value: /usr/lib64/galera3/libgalera_smm.so
*************************** 29. row ***************************
Variable_name: wsrep_provider_options
        Value: base_dir = /var/lib/mysql/; base_host = 172.31.0.17; base_port = 4567; cert.log_conflicts = no; cert.optimistic_pa = yes; debug = no; evs.auto_evict = 0; evs.causal_keepalive_period = PT1S; evs.debug_log_mask = 0x1; evs.delay_margin = PT1S; evs.delayed_keep_period = PT30S; evs.inactive_check_period = PT0.5S; evs.inactive_timeout = PT15S; evs.info_log_mask = 0; evs.install_timeout = PT7.5S; evs.join_retrans_period = PT1S; evs.keepalive_period = PT1S; evs.max_install_timeouts = 3; evs.send_window = 10; evs.stats_report_period = PT1M; evs.suspect_timeout = PT5S; evs.use_aggregate = true; evs.user_send_window = 4; evs.version = 0; evs.view_forget_timeout = P1D; gcache.dir = /var/lib/mysql/; gcache.freeze_purge_at_seqno = -1; gcache.keep_pages_count = 0; gcache.keep_pages_size = 0; gcache.mem_size = 0; gcache.name = /var/lib/mysql//galera.cache; gcache.page_size = 128M; gcache.recover = no; gcache.size = 128M; gcomm.thread_prio = ; gcs.fc_debug = 0; gcs.fc_factor = 1; gcs.fc_limit = 100; gcs.fc_master_slave = no; 
*************************** 30. row ***************************
Variable_name: wsrep_recover
        Value: OFF
*************************** 31. row ***************************
Variable_name: wsrep_reject_queries
        Value: NONE
*************************** 32. row ***************************
Variable_name: wsrep_replicate_myisam
        Value: OFF
*************************** 33. row ***************************
Variable_name: wsrep_restart_slave
        Value: OFF
*************************** 34. row ***************************
Variable_name: wsrep_retry_autocommit
        Value: 1
*************************** 35. row ***************************
Variable_name: wsrep_slave_FK_checks
        Value: ON
*************************** 36. row ***************************
Variable_name: wsrep_slave_UK_checks
        Value: OFF
*************************** 37. row ***************************
Variable_name: wsrep_slave_threads
        Value: 8
*************************** 38. row ***************************
Variable_name: wsrep_sst_allowed_methods
        Value: mysqldump,rsync,skip,xtrabackup,xtrabackup-v2
*************************** 39. row ***************************
Variable_name: wsrep_sst_auth
        Value: ********
*************************** 40. row ***************************
Variable_name: wsrep_sst_donor
        Value: 
*************************** 41. row ***************************
Variable_name: wsrep_sst_donor_rejects_queries
        Value: OFF
*************************** 42. row ***************************
Variable_name: wsrep_sst_method
        Value: xtrabackup-v2
*************************** 43. row ***************************
Variable_name: wsrep_sst_receive_address
        Value: AUTO
*************************** 44. row ***************************
Variable_name: wsrep_start_position
        Value: 00000000-0000-0000-0000-000000000000:-1
*************************** 45. row ***************************
Variable_name: wsrep_sync_wait
        Value: 0
45 rows in set (0.01 sec)


# 查看相关状态变量
mysql> show status like 'wsrep%'\\G
*************************** 1. row ***************************
Variable_name: wsrep_local_state_uuid
        Value: a9af3ceb-bb04-11eb-8cd1-d26e25aee8cd
*************************** 2. row ***************************
Variable_name: wsrep_protocol_version
        Value: 9
*************************** 3. row ***************************
Variable_name: wsrep_last_applied
        Value: 3
*************************** 4. row ***************************
Variable_name: wsrep_last_committed
        Value: 3
*************************** 5. row ***************************
Variable_name: wsrep_replicated
        Value: 3
*************************** 6. row ***************************
Variable_name: wsrep_replicated_bytes
        Value: 752
*************************** 7. row ***************************
Variable_name: wsrep_repl_keys
        Value: 3
*************************** 8. row ***************************
Variable_name: wsrep_repl_keys_bytes
        Value: 96
*************************** 9. row ***************************
Variable_name: wsrep_repl_data_bytes
        Value: 460
*************************** 10. row ***************************
Variable_name: wsrep_repl_other_bytes
        Value: 0
*************************** 11. row ***************************
Variable_name: wsrep_received
        Value: 2
*************************** 12. row ***************************
Variable_name: wsrep_received_bytes
        Value: 153
*************************** 13. row ***************************
Variable_name: wsrep_local_commits
        Value: 0
*************************** 14. row ***************************
Variable_name: wsrep_local_cert_failures
        Value: 0
*************************** 15. row ***************************
Variable_name: wsrep_local_replays
        Value: 0
*************************** 16. row ***************************
Variable_name: wsrep_local_send_queue
        Value: 0
*************************** 17. row ***************************
Variable_name: wsrep_local_send_queue_max
        Value: 1
*************************** 18. row ***************************
Variable_name: wsrep_local_send_queue_min
        Value: 0
*************************** 19. row ***************************
Variable_name: wsrep_local_send_queue_avg
        Value: 0.000000
*************************** 20. row ***************************
Variable_name: wsrep_local_recv_queue
        Value: 0
*************************** 21. row ***************************
Variable_name: wsrep_local_recv_queue_max
        Value: 2
*************************** 22. row ***************************
Variable_name: wsrep_local_recv_queue_min
        Value: 0
*************************** 23. row ***************************
Variable_name: wsrep_local_recv_queue_avg
        Value: 0.500000
*************************** 24. row ***************************
Variable_name: wsrep_local_cached_downto
        Value: 1
*************************** 25. row ***************************
Variable_name: wsrep_flow_control_paused_ns
        Value: 0
*************************** 26. row ***************************
Variable_name: wsrep_flow_control_paused
        Value: 0.000000
*************************** 27. row ***************************
Variable_name: wsrep_flow_control_sent
        Value: 0
*************************** 28. row ***************************
Variable_name: wsrep_flow_control_recv
        Value: 0
*************************** 29. row ***************************
Variable_name: wsrep_flow_control_interval
        Value: [ 100, 100 ]
*************************** 30. row ***************************
Variable_name: wsrep_flow_control_interval_low
        Value: 100
*************************** 31. row ***************************
Variable_name: wsrep_flow_control_interval_high
        Value: 100
*************************** 32. row ***************************
Variable_name: wsrep_flow_control_status
        Value: OFF
*************************** 33. row ***************************
Variable_name: wsrep_flow_control_active
        Value: false
*************************** 34. row ***************************
Variable_name: wsrep_flow_control_requested
        Value: false
*************************** 35. row ***************************
Variable_name: wsrep_cert_deps_distance
        Value: 1.000000
*************************** 36. row ***************************
Variable_name: wsrep_apply_oooe
        Value: 0.000000
*************************** 37. row ***************************
Variable_name: wsrep_apply_oool
        Value: 0.000000
*************************** 38. row ***************************
Variable_name: wsrep_apply_window
        Value: 1.000000
*************************** 39. row ***************************
Variable_name: wsrep_commit_oooe
        Value: 0.000000
*************************** 40. row ***************************
Variable_name: wsrep_commit_oool
        Value: 0.000000
*************************** 41. row ***************************
Variable_name: wsrep_commit_window
        Value: 1.000000
*************************** 42. row ***************************
Variable_name: wsrep_local_state
        Value: 4
*************************** 43. row ***************************
Variable_name: wsrep_local_state_comment
        Value: Synced
*************************** 44. row ***************************
Variable_name: wsrep_cert_index_size
        Value: 1
*************************** 45. row ***************************
Variable_name: wsrep_cert_bucket_count
        Value: 22
*************************** 46. row ***************************
Variable_name: wsrep_gcache_pool_size
        Value: 2192
*************************** 47. row ***************************
Variable_name: wsrep_causal_reads
        Value: 0
*************************** 48. row ***************************
Variable_name: wsrep_cert_interval
        Value: 0.000000
*************************** 49. row ***************************
Variable_name: wsrep_open_transactions
        Value: 0
*************************** 50. row ***************************
Variable_name: wsrep_open_connections
        Value: 0
*************************** 51. row ***************************
Variable_name: wsrep_ist_receive_status
        Value: 
*************************** 52. row ***************************
Variable_name: wsrep_ist_receive_seqno_start
        Value: 0
*************************** 53. row ***************************
Variable_name: wsrep_ist_receive_seqno_current
        Value: 0
*************************** 54. row ***************************
Variable_name: wsrep_ist_receive_seqno_end
        Value: 0
*************************** 55. row ***************************
Variable_name: wsrep_incoming_addresses
        Value: 172.31.0.17:3306
*************************** 56. row ***************************
Variable_name: wsrep_cluster_weight
        Value: 1
*************************** 57. row ***************************
Variable_name: wsrep_desync_count
        Value: 0
*************************** 58. row ***************************
Variable_name: wsrep_evs_delayed
        Value: 
*************************** 59. row ***************************
Variable_name: wsrep_evs_evict_list
        Value: 
*************************** 60. row ***************************
Variable_name: wsrep_evs_repl_latency
        Value: 0/0/0/0/0
*************************** 61. row ***************************
Variable_name: wsrep_evs_state
        Value: OPERATIONAL
*************************** 62. row ***************************
Variable_name: wsrep_gcomm_uuid
        Value: a9ae769b-bb04-11eb-83d4-c3d7162c47bc
*************************** 63. row ***************************
Variable_name: wsrep_gmcast_segment
        Value: 0
*************************** 64. row ***************************
Variable_name: wsrep_cluster_conf_id
        Value: 1
*************************** 65. row ***************************
Variable_name: wsrep_cluster_size
        Value: 1
*************************** 66. row ***************************
Variable_name: wsrep_cluster_state_uuid
        Value: a9af3ceb-bb04-11eb-8cd1-d26e25aee8cd
*************************** 67. row ***************************
Variable_name: wsrep_cluster_status
        Value: Primary
*************************** 68. row ***************************
Variable_name: wsrep_connected
        Value: ON
*************************** 69. row ***************************
Variable_name: wsrep_local_bf_aborts
        Value: 0
*************************** 70. row ***************************
Variable_name: wsrep_local_index
        Value: 0
*************************** 71. row ***************************
Variable_name: wsrep_provider_name
        Value: Galera
*************************** 72. row ***************************
Variable_name: wsrep_provider_vendor
        Value: Codership Oy <info@codership.com>
*************************** 73. row ***************************
Variable_name: wsrep_provider_version
        Value: 3.49(r0ef0d79)
*************************** 74. row ***************************
Variable_name: wsrep_ready
        Value: ON
74 rows in set (0.00 sec)

## 重点关注下面内容
mysql> show status like 'wsrep%';
+----------------------------------+--------------------------------------+
| Variable_name                    | Value                                |
+----------------------------------+--------------------------------------+
| wsrep_local_state_uuid           | a9af3ceb-bb04-11eb-8cd1-d26e25aee8cd |
...                                |...
| wsrep_local_state                | 4                                    |
| wsrep_local_state_comment        | Synced                               |
...                                |...
| wsrep_cluster_size               | 1                                    |
| wsrep_cluster_status             | Primary                              |
| wsrep_connected                  | ON                                   |
...                                |...
| wsrep_ready                      | ON                                   |
+----------------------------------+--------------------------------------+
74 rows in set (0.00 sec)

# 说明:
wsrep_cluster_size表示,该Galera集群中只有一个节点
wsrep_local_state_comment 状态为Synced(4),表示数据已同步完成(因为是第一个引导节点,无数据
需要同步)。 如果状态是Joiner, 意味着 SST 没有完成. 只有所有节点状态是Synced,才可以加新节点
wsrep_cluster_status为Primary,且已经完全连接并准备好

启动PXC集群中其它所有节点

# 除了名义上的master之外,其它的node节点只需要启动mysql即可
[root@localhost ~]# systemctl start mysql

查看集群状态,验证集群是否成功

#在任意节点,查看集群状态
mysql> show variables like 'wsrep_node_name';
+-----------------+-------------------------+
| Variable_name   | Value                   |
+-----------------+-------------------------+
| wsrep_node_name | pxc-cluster-node-1-m520 |
+-----------------+-------------------------+
1 row in set (0.00 sec)


mysql> show variables like 'wsrep_node_address';
+--------------------+-------------+
| Variable_name      | Value       |
+--------------------+-------------+
| wsrep_node_address | 172.31.0.17 |
+--------------------+-------------+
1 row in set (0.00 sec)


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

# 查看集群成员个数
mysql> show status like 'wsrep_cluster_size';
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| wsrep_cluster_size | 1     |
+--------------------+-------+
1 row in set (0.00 sec)

# 在任意节点查看数据库
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

# 在任意节点创建数据库
[root@localhost mysql]# mysql -uroot -pcentos
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 11
Server version: 5.7.33-36-57-log Percona XtraDB Cluster (GPL), Release rel36, Revision a1ed9c3, WSREP version 31.49, wsrep_31.49

Copyright (c) 2009-2021 Percona LLC and/or its affiliates
Copyright (c) 2000, 2021, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)

mysql> create database db1;
Query OK, 1 row affected (0.00 sec)

# 其他任意节点查询可以看到创建的db1库
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

# 上面的节点添加完,在任意节点执行可以查看集群的个数
mysql> show status like 'wsrep_cluster_size';
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| wsrep_cluster_size | 3     |
+--------------------+-------+
1 row in set (0.00 sec)

扩展

在PXC集群中加入节点

一个节点加入到Galera集群有两种情况:新节点加入集群、暂时离组的成员再次加入集群

1)新节点加入Galera集群
新节点加入集群时,需要从当前集群中选择一个Donor节点来同步数据,也就是所谓的
state_snapshot_tranfer(SST)过程。SST同步数据的方式由选项wsrep_sst_method决定,一般选择的是
xtrabackup。
必须注意,新节点加入Galera时,会删除新节点上所有已有数据,再通过xtrabackup(假设使用的是该方
式)从Donor处完整备份所有数据进行恢复。所以,如果数据量很大,新节点加入过程会很慢。而且,在
一个新节点成为Synced状态之前,不要同时加入其它新节点,否则很容易将集群压垮。
如果是这种情况,可以考虑使用wsrep_sst_method=rsync来做增量同步,既然是增量同步,最好保证
新节点上已经有一部分数据基础,否则和全量同步没什么区别,且这样会对Donor节点加上全局read
only锁。
2)旧节点加入Galera集群
如果旧节点加入Galera集群,说明这个节点在之前已经在Galera集群中呆过,有一部分数据基础,缺少
的只是它离开集群时的数据。这时加入集群时,会采用IST(incremental snapshot transfer)传输机制,
即使用增量传输。
但注意,这部分增量传输的数据源是Donor上缓存在GCache文件中的,这个文件有大小限制,如果缺失
的数据范围超过已缓存的内容,则自动转为SST传输。如果旧节点上的数据和Donor上的数据不匹配(例
如这个节点离组后人为修改了一点数据),则自动转为SST传输。

在PXC集群中再加一台新的主机PXC4:172.31.0.47

[root@localhost ~]# yum install Percona-XtraDB-Cluster-57 -y

# 改配置文件 
[root@localhost ~]# vim /etc/percona-xtradb-cluster.conf.d/mysqld.cnf
[client]
socket=/var/lib/mysql/mysql.sock

[mysqld]
server-id=47
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
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

# 改配置文件(添加新的节点需要每台都把ip加上)
[root@localhost ~]# grep -Ev "^#|^$" /etc/percona-xtradb-cluster.conf.d/wsrep.cnf 
[mysqld]
wsrep_provider=/usr/lib64/galera3/libgalera_smm.so
wsrep_cluster_address=gcomm://172.31.0.17:4567,172.31.0.27:4567,172.31.0.37:4567,172.31.0.47
binlog_format=ROW
default_storage_engine=InnoDB
wsrep_slave_threads= 8
wsrep_log_conflicts
innodb_autoinc_lock_mode=2
wsrep_node_address=172.31.0.47
wsrep_cluster_name=pxc-cluster-m520
wsrep_node_name=pxc-cluster-node-4-m520
pxc_strict_mode=ENFORCING
wsrep_sst_method=xtrabackup-v2
wsrep_sst_auth="sstuser:s3cretPass"

# 启动
[root@localhost ~]# systemctl start mysql

# 查看端口4567和3306
[root@localhost ~]# ss -tnul
Netid  State      Recv-Q Send-Q                                              Local Address:Port                                                             Peer Address:Port              
tcp    LISTEN     0      128                                                             *:22                                                                          *:*                  
tcp    LISTEN     0      128                                                             *:4567                                                                        *:*                  
tcp    LISTEN     0      100                                                     127.0.0.1:25                                                                          *:*                  
tcp    LISTEN     0      80                                                             :::3306                                                                       :::*                  
tcp    LISTEN     0      128                                                            :::22                                                                         :::*                  
tcp    LISTEN     0      100                                                           ::1:25                            :::*

任意节点查询会发现从3变成了4,说明已经添加成功

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

在除第一个启动节点外的任意节点停止服务

[root@localhost ~]# systemctl stop mysql

# 在其它任意节点查看wsrep_cluster_size变量少了一个节点
mysql> show status like 'wsrep_cluster_size';
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| wsrep_cluster_size | 3     |
+--------------------+-------+
1 row in set (0.00 sec)

报错

启动从服务报错

[root@localhost ~]# vim /var/log/mysqld.log 
2021-05-22T15:42:13.975755Z 0 [Warning] WSREP: last inactive check more than PT1.5S (3*evs.inactive_check_period) ago (PT3.50712S), skipping check
2021-05-22T15:42:43.527512Z 0 [Note] WSREP: Current view of cluster as seen by this node
view ((empty))
2021-05-22T15:42:43.527657Z 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():161
2021-05-22T15:42:43.527671Z 0 [ERROR] WSREP: gcs/src/gcs_core.cpp:gcs_core_open():209: Failed to open backend connection: -110 (Connection timed out)
2021-05-22T15:42:43.527949Z 0 [ERROR] WSREP: gcs/src/gcs.cpp:gcs_open():1527: Failed to open channel 'pxc-cluster' at 'gcomm://172.31.0.17,172.31.0.27,172.31.0.37': -110 (Connection timed out)
2021-05-22T15:42:43.527962Z 0 [ERROR] WSREP: gcs connect failed: Connection timed out
2021-05-22T15:42:43.527968Z 0 [ERROR] WSREP: Provider/Node (gcomm://172.31.0.17,172.31.0.27,172.31.0.37) failed to establish connection with cluster (reason: 7)
2021-05-22T15:42:43.527973Z 0 [ERROR] Aborting

2021-05-22T15:42:43.527981Z 0 [Note] Giving 0 client threads a chance to die gracefully
2021-05-22T15:42:43.527988Z 0 [Note] WSREP: Waiting for active wsrep applier to exit
2021-05-22T15:42:43.528011Z 0 [Note] WSREP: Service disconnected.
2021-05-22T15:42:43.528015Z 0 [Note] WSREP: Waiting to close threads......
2021-05-22T15:42:48.528984Z 0 [Note] WSREP: Some threads may fail to exit.
2021-05-22T15:42:48.535147Z 0 [Note] WSREP: Service thread queue flushed.
2021-05-22T15:42:48.535255Z 0 [Note] WSREP: MemPool(SlaveTrxHandle): hit ratio: 0, misses: 0, in use: 0, in pool: 0
2021-05-22T15:42:48.535398Z 0 [Note] WSREP: Shifting CLOSED -> DESTROYED (TO: 0)
2021-05-22T15:42:48.536037Z 0 [Note] Binlog end
2021-05-22T15:42:48.536169Z 0 [Note] /usr/sbin/mysqld: Shutdown complete

上面其他节点启动报错问题解决方法:

其他节点配置文件加上4567端口(组成员之间进行沟通的端口号),启动mysql服务即可
[root@localhost ~]# vim /etc/percona-xtradb-cluster.conf.d/wsrep.cnf 
wsrep_cluster_address=gcomm://172.31.0.17:4567,172.31.0.27:4567,172.31.0.37:4567

安装软件Percona-XtraDB-Cluster-57报错

Error: percona-xtrabackup-24 conflicts with percona-xtrabackup-2.3.10-1.el7.x86_64
 You could try using --skip-broken to work around the problem
 You could try running: rpm -Va --nofiles --nodigest

解决思路:因为曾经做实验安装过相关的软件造成冲突了,卸载然后再重新安装即可

[root@localhost ~]# yum remove percona-xtrabackup* -y

以上是关于MySQL之PXC的主要内容,如果未能解决你的问题,请参考以下文章

数据库之MySQL部署PXC方案

MySQL5.7之PXC集群搭建

mysql高可用之PXC(Percona XtraDB Cluster)

MySQL高可用架构之MySQL5.7.19 PXC

企业主流MySQL高可用集群架构三部曲之PXC

MySQL之PXC