MySQL中PXC集群的介绍

发布时间:2020-05-27 14:37:02 作者:鸽子
来源:亿速云 阅读:450

一、PXC介绍

参考:Percona官方

PXC是一个开源的MySQL高可用解决方案,它将Percona Server和Xtrabackup与Galera库集成,以实现同步多主复制。基于Galera的高可用方案主要有MariaDB Galera Cluster(MGC)和Percona XtraDB Cluster(PXC),目前PXC架构在生产环境中用的更多而且更成熟些,PXC相比那些传统的基于主从模式的集群架构MHA和双主,PXC最突出的特点就是解决了诟病已久的复制延迟问题,基本上可以达到实时同步。而且节点与节点之间,它们互相的关系是对等的。本身Galera Cluster也是一种多主架构。PXC是在存储引擎层实现的同步复制,而非异步复制,所以其数据的一致性是相当高的。

要搭建PXC架构至少需要三台MySQL实例来组成一个集群,三个实例之间不是主从模式,而是各自为主,所以三者之间的关系是对等的,不分主从,这也叫multi-master架构,客户端读写时,连接哪个实例都是一样的,读取到的数据是相同的,写入任意一个实例后,集群会将自己新写入的数据同步到其他实例上,这种架构不共享任何数据,是一种高冗余的MySQL集群架构。

1、PXC优缺点

优点:

缺点:

2、PXC工作原理

PXC的操作流程大体是这样的,首先客户端向请求连接的写入节点提交事务之前,由该节点将需要产生的replication writeset广播出去,然后获取全局事务ID,一并传送到其它的节点上去。其它节点通过certification合并数据之后,发现没有冲突数据,便执行apply_cb和commit_cb操作,否则就discard此次事务。
而当前节点(客户端请求的写入节点)通过验证之后,执行commit_cb操作,并返回OK给客户端。如果验证没有通过,则rollback_cb。
在生产线上的PXC集群中,至少要有三台节点。如果其中一个节点没有验证通过,出现了数据冲突,那么此时采取的方式就是将出现数据不一致的节点踢出集群,而且它会自动执行shutdown命令来自动关机。

3、PXC中的重要概念

首先要规范集群中节点的数量,整个集群节点数控制在最少3个、最多8个的范围内。最少3个是为了防止脑裂现象,因为只有在两个节点的情况下才会出现脑裂。脑裂的表现就是输出任何命令,返回结果都是unkown command。
当一个新节点要加入PXC集群的时候,需要从集群中各节点里选举出一个doner节点作为全量数据的贡献者。PXC有两种节点的数据传输方式,一种叫SST全量传输,另一种叫IST增量传输。SST传输有XtraBackup、mysqldump、rsync三种方式,而增量传输只有XtraBackup。一般数据量不大的时候可以使用SST作为全量传输,但也只使用XtraBackup方式。

节点在集群中,会因新节点的加入或故障,同步失效等而发生状态的切换,下面列举出这些状态的含义:

二、部署PXC集群

1、环境准备

OS主机名IP
Cent OS 7.5pxc-01192.168.20.2
Cent OS 7.5pxc-02192.168.20.3
Cent OS 7.5pxc-03192.168.20.4

注:所有主机都是不需要提前安装MySQL服务的,在PXC的源码包中包括了MySQL服务的。

注:2-8小结的所有操作,三台节点都是需要执行的。如果有什么是不需要在三台节点都执行的,我会特别说明的。

2、安装依赖

[root@pxc-01 ~]# yum install -y libev lsof perl-Compress-Raw-Bzip2 perl-Compress-Raw-Zlib perl-DBD-MySQL perl-DBI perl-Digest perl-Digest-MD5 perl-IO-Compress perl-Net-Daemon perl-PlRPC socat openssl openssl-devel

3、卸载Mariadb

[root@pxc-01 ~]# yum -y erase mariadb && rpm -qa | grep mariadb | xargs rpm -e --nodeps

4、下载所需软件包(xtraBackup、Percona-XtraDB-Cluster及qpress)

[root@pxc-01 bin]# wget https://www.percona.com/downloads/Percona-XtraBackup-2.4/Percona-XtraBackup-2.4.18/binary/redhat/7/x86_64/percona-xtrabackup-24-2.4.18-1.el7.x86_64.rpm
[root@pxc-01 bin]# wget http://www.quicklz.com/qpress-11-linux-x64.tar
#上面这个包使用wget可能会报错406,用浏览器打开链接下载再上传到服务器即可。
[root@pxc-01 bin]# wget https://www.percona.com/downloads/Percona-XtraDB-Cluster-LATEST/Percona-XtraDB-Cluster-5.7.28-31.41/binary/tarball/Percona-XtraDB-Cluster-5.7.28-rel31-31.41.1.Linux.x86_64.ssl101.tar.gz
#这个包下载的话,时间会稍微长一些,耐心等待。

5、配置qpress命令并安装xtrabackup

[root@pxc-01 bin]# tar xf qpress-11-linux-x64.tar -C /usr/bin
[root@pxc-01 bin]# rpm -ivh percona-xtrabackup-24-2.4.18-1.el7.x86_64.rpm

6、部署PXC服务

#创建MySQL系统用户
[root@pxc-01 bin]# useradd -M -r -s /bin/false mysql 
#解包
[root@pxc-01 bin]# tar zxf Percona-XtraDB-Cluster-5.7.28-rel31-31.41.1.Linux.x86_64.ssl101.tar.gz
#移动到指定目录
[root@pxc-01 bin]# mv Percona-XtraDB-Cluster-5.7.28-rel31-31.41.1.Linux.x86_64.ssl101 /usr/local/mysql
#创建data目录并更改目录属主属组为MySQL
[root@pxc-01 bin]# mkdir -p /usr/local/mysql/data
[root@pxc-01 bin]# chown -R mysql.mysql /usr/local/mysql/
#为MySQL命令配置全局变量
[root@pxc-01 bin]# echo "export PATH=/usr/local/mysql/bin:$PATH" >> /etc/profile
[root@pxc-01 bin]# source /etc/profile

7、定义MySQL服务的配置文件

1)pxc-01完整配置文件如下:
[root@pxc-01 bin]# cat /etc/my.cnf
# Example: Percona XtraDB Cluster 5.7
[client]
port = 3306
socket = /tmp/mysql.sock
[mysql]
prompt="\u@\h \R:\m:\s[\d]> "
no-auto-rehash
[mysqld]
user = mysql
port = 3306
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
socket = /tmp/mysql.sock
pid-file = db.pid
character-set-server = utf8mb4
skip_name_resolve = 1
open_files_limit = 65535
back_log = 1024
max_connections = 512
max_connect_errors = 1000000
table_open_cache = 1024
table_definition_cache = 1024
table_open_cache_instances = 64
thread_stack = 512K
external-locking = FALSE
max_allowed_packet = 32M
sort_buffer_size = 4M
join_buffer_size = 4M
thread_cache_size = 768
#query_cache_size = 0
#query_cache_type = 0
interactive_timeout = 600
wait_timeout = 600
tmp_table_size = 32M
max_heap_table_size = 32M
slow_query_log = 1
slow_query_log_file = /usr/local/mysql/data/slow.log
log-error = /usr/local/mysql/data/error.log
long_query_time = 0.1
server-id = 1813306
log-bin = /usr/local/mysql/data/mysql-bin
sync_binlog = 1
binlog_cache_size = 4M
max_binlog_cache_size = 1G
max_binlog_size = 1G
expire_logs_days = 7
master_info_repository = TABLE
relay_log_info_repository = TABLE
gtid_mode = on
enforce_gtid_consistency = 1
log_slave_updates
binlog_format = row    #为了让Galera正确工作,binlog格式应该是ROW
relay_log_recovery = 1
relay-log-purge = 1
key_buffer_size = 32M
read_buffer_size = 8M
read_rnd_buffer_size = 4M
bulk_insert_buffer_size = 64M
lock_wait_timeout = 3600
explicit_defaults_for_timestamp = 1
innodb_thread_concurrency = 0
innodb_sync_spin_loops = 100
innodb_spin_wait_delay = 30
transaction_isolation = REPEATABLE-READ
innodb_buffer_pool_size = 1024M
innodb_buffer_pool_instances = 8
innodb_buffer_pool_load_at_startup = 1
innodb_buffer_pool_dump_at_shutdown = 1
innodb_data_file_path = ibdata1:1G:autoextend
innodb_flush_log_at_trx_commit = 1
innodb_log_buffer_size = 32M
innodb_log_file_size = 2G
innodb_log_files_in_group = 2
#innodb_max_undo_log_size = 4G
innodb_io_capacity = 2000
innodb_io_capacity_max = 4000
innodb_flush_neighbors = 0
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_purge_threads = 4
innodb_page_cleaners = 4
innodb_open_files = 65535
innodb_max_dirty_pages_pct = 50
innodb_flush_method = O_DIRECT
innodb_lru_scan_depth = 4000
innodb_checksum_algorithm = crc32
#innodb_file_format = Barracuda
#innodb_file_format_max = Barracuda
innodb_lock_wait_timeout = 10
innodb_rollback_on_timeout = 1
innodb_print_all_deadlocks = 1
innodb_file_per_table = 1
innodb_online_alter_log_max_size = 4G
internal_tmp_disk_storage_engine = InnoDB
innodb_stats_on_metadata = 0
# PXC  以上都是关于数据库优化的一些信息,大多数为可选配置,与pxc有关的都是下面的参数
wsrep_provider=/usr/local/mysql/lib/libgalera_smm.so
wsrep_provider_options="gcache.size=2G"
wsrep_cluster_name=my_pxc_cluster
wsrep_cluster_address=gcomm://192.168.20.2,192.168.20.3,192.168.20.4
wsrep_node_name=pxc01
wsrep_node_address=192.168.20.2
wsrep_sst_method=xtrabackup-v2
wsrep_sst_auth=sst:pwd@123
pxc_strict_mode=ENFORCING  
default_storage_engine=InnoDB    # myisam存储引擎只能在测试环境中使用
innodb_autoinc_lock_mode=2    # 此 InnoDB 自动增量锁定模式是 Galera 强制的
[mysqldump]
quick
max_allowed_packet = 32M

PXC相关的部分参数解释如下:

更多更详细的PXC参数解释请移步:MySQL wsrep选项,会有惊奇的发现,同时,若要修改上面的配置项,还是建议去看详细的选项解释。

2)pxc-02节点完整配置文件如下:

与pxc-01节点相比,只有三处不能一样,下面都标注了出来

[root@pxc-02 src]# cat /etc/my.cnf 
# Example: Percona XtraDB Cluster 5.7
[client]
port = 3306
socket = /tmp/mysql.sock
[mysql]
prompt="\u@\h \R:\m:\s[\d]> "
no-auto-rehash
[mysqld]
user = mysql
port = 3306
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
socket = /tmp/mysql.sock
pid-file = db.pid
character-set-server = utf8mb4
skip_name_resolve = 1
open_files_limit = 65535
back_log = 1024
max_connections = 512
max_connect_errors = 1000000
table_open_cache = 1024
table_definition_cache = 1024
table_open_cache_instances = 64
thread_stack = 512K
external-locking = FALSE
max_allowed_packet = 32M
sort_buffer_size = 4M
join_buffer_size = 4M
thread_cache_size = 768
#query_cache_size = 0
#query_cache_type = 0
interactive_timeout = 600
wait_timeout = 600
tmp_table_size = 32M
max_heap_table_size = 32M
slow_query_log = 1
slow_query_log_file = /usr/local/mysql/data/slow.log
log-error = /usr/local/mysql/data/error.log
long_query_time = 0.1
server-id = 1813307                   # server-id不可以一样
log-bin = /usr/local/mysql/data/mysql-bin
sync_binlog = 1
binlog_cache_size = 4M
max_binlog_cache_size = 1G
max_binlog_size = 1G
expire_logs_days = 7
master_info_repository = TABLE
relay_log_info_repository = TABLE
gtid_mode = on
enforce_gtid_consistency = 1
log_slave_updates
binlog_format = row   
relay_log_recovery = 1
relay-log-purge = 1
key_buffer_size = 32M
read_buffer_size = 8M
read_rnd_buffer_size = 4M
bulk_insert_buffer_size = 64M
lock_wait_timeout = 3600
explicit_defaults_for_timestamp = 1
innodb_thread_concurrency = 0
innodb_sync_spin_loops = 100
innodb_spin_wait_delay = 30
transaction_isolation = REPEATABLE-READ
innodb_buffer_pool_size = 1024M
innodb_buffer_pool_instances = 8
innodb_buffer_pool_load_at_startup = 1
innodb_buffer_pool_dump_at_shutdown = 1
innodb_data_file_path = ibdata1:1G:autoextend
innodb_flush_log_at_trx_commit = 1
innodb_log_buffer_size = 32M
innodb_log_file_size = 2G
innodb_log_files_in_group = 2
#innodb_max_undo_log_size = 4G
innodb_io_capacity = 2000
innodb_io_capacity_max = 4000
innodb_flush_neighbors = 0
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_purge_threads = 4
innodb_page_cleaners = 4
innodb_open_files = 65535
innodb_max_dirty_pages_pct = 50
innodb_flush_method = O_DIRECT
innodb_lru_scan_depth = 4000
innodb_checksum_algorithm = crc32
#innodb_file_format = Barracuda
#innodb_file_format_max = Barracuda
innodb_lock_wait_timeout = 10
innodb_rollback_on_timeout = 1
innodb_print_all_deadlocks = 1
innodb_file_per_table = 1
innodb_online_alter_log_max_size = 4G
internal_tmp_disk_storage_engine = InnoDB
innodb_stats_on_metadata = 0
# PXC  
wsrep_provider=/usr/local/mysql/lib/libgalera_smm.so
wsrep_provider_options="gcache.size=2G"
wsrep_cluster_name=my_pxc_cluster
wsrep_cluster_address=gcomm://192.168.20.2,192.168.20.3,192.168.20.4
wsrep_node_name=pxc02             # 节点的逻辑名称不可以一样
wsrep_node_address=192.168.20.3     #这里的IP地址是本机的,当然也不一样
wsrep_sst_method=xtrabackup-v2
wsrep_sst_auth=sst:pwd@123
pxc_strict_mode=ENFORCING
default_storage_engine=InnoDB 
innodb_autoinc_lock_mode=2    
[mysqldump]
quick
max_allowed_packet = 32M
3)pxc-03完整配置文件如下:

同样,不能一样的还是那三处配置,分别是server-id、节点逻辑名称,以及节点IP地址。

[root@pxc-03 src]# cat /etc/my.cnf 
# Example: Percona XtraDB Cluster 5.7
[client]
port = 3306
socket = /tmp/mysql.sock
[mysql]
prompt="\u@\h \R:\m:\s[\d]> "
no-auto-rehash
[mysqld]
user = mysql
port = 3306
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
socket = /tmp/mysql.sock
pid-file = db.pid
character-set-server = utf8mb4
skip_name_resolve = 1
open_files_limit = 65535
back_log = 1024
max_connections = 512
max_connect_errors = 1000000
table_open_cache = 1024
table_definition_cache = 1024
table_open_cache_instances = 64
thread_stack = 512K
external-locking = FALSE
max_allowed_packet = 32M
sort_buffer_size = 4M
join_buffer_size = 4M
thread_cache_size = 768
#query_cache_size = 0
#query_cache_type = 0
interactive_timeout = 600
wait_timeout = 600
tmp_table_size = 32M
max_heap_table_size = 32M
slow_query_log = 1
slow_query_log_file = /usr/local/mysql/data/slow.log
log-error = /usr/local/mysql/data/error.log
long_query_time = 0.1
server-id = 1813308
log-bin = /usr/local/mysql/data/mysql-bin
sync_binlog = 1
binlog_cache_size = 4M
max_binlog_cache_size = 1G
max_binlog_size = 1G
expire_logs_days = 7
master_info_repository = TABLE
relay_log_info_repository = TABLE
gtid_mode = on
enforce_gtid_consistency = 1
log_slave_updates
binlog_format = row   
relay_log_recovery = 1
relay-log-purge = 1
key_buffer_size = 32M
read_buffer_size = 8M
read_rnd_buffer_size = 4M
bulk_insert_buffer_size = 64M
lock_wait_timeout = 3600
explicit_defaults_for_timestamp = 1
innodb_thread_concurrency = 0
innodb_sync_spin_loops = 100
innodb_spin_wait_delay = 30
transaction_isolation = REPEATABLE-READ
innodb_buffer_pool_size = 1024M
innodb_buffer_pool_instances = 8
innodb_buffer_pool_load_at_startup = 1
innodb_buffer_pool_dump_at_shutdown = 1
innodb_data_file_path = ibdata1:1G:autoextend
innodb_flush_log_at_trx_commit = 1
innodb_log_buffer_size = 32M
innodb_log_file_size = 2G
innodb_log_files_in_group = 2
#innodb_max_undo_log_size = 4G
innodb_io_capacity = 2000
innodb_io_capacity_max = 4000
innodb_flush_neighbors = 0
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_purge_threads = 4
innodb_page_cleaners = 4
innodb_open_files = 65535
innodb_max_dirty_pages_pct = 50
innodb_flush_method = O_DIRECT
innodb_lru_scan_depth = 4000
innodb_checksum_algorithm = crc32
#innodb_file_format = Barracuda
#innodb_file_format_max = Barracuda
innodb_lock_wait_timeout = 10
innodb_rollback_on_timeout = 1
innodb_print_all_deadlocks = 1
innodb_file_per_table = 1
innodb_online_alter_log_max_size = 4G
internal_tmp_disk_storage_engine = InnoDB
innodb_stats_on_metadata = 0
# PXC  
wsrep_provider=/usr/local/mysql/lib/libgalera_smm.so
wsrep_provider_options="gcache.size=2G"
wsrep_cluster_name=my_pxc_cluster
wsrep_cluster_address=gcomm://192.168.20.2,192.168.20.3,192.168.20.4
wsrep_node_name=pxc03
wsrep_node_address=192.168.20.4
wsrep_sst_method=xtrabackup-v2
wsrep_sst_auth=sst:pwd@123
pxc_strict_mode=ENFORCING
default_storage_engine=InnoDB 
innodb_autoinc_lock_mode=2    
[mysqldump]
quick
max_allowed_packet = 32M

8、各个节点初始化MySQL服务并开启防火墙相关端口

#初始化MySQL服务
[root@pxc-01 ~]# mysqld --defaults-file=/etc/my.cnf --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ --initialize
#开启防火墙相关端口
[root@pxc-01 ~]# firewall-cmd --permanent --add-port={3306,4567,4444,4568}/tcp
success
[root@pxc-01 ~]# firewall-cmd --reload
[root@pxc-01 ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33
  sources: 
  services: ssh dhcpv6-client
  ports: 3306/tcp 4567/tcp 4444/tcp 4568/tcp    #确认端口已添加
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 

相关端口信息如下:

注:接下来的操作,如没有特殊声明,都是在pxc-01节点上进行。

9、引导pxc-01节点初始化集群

[root@pxc-01 ~]# mysqld --defaults-file=/etc/my.cnf --wsrep_new_cluster &
[root@pxc-01 ~]# ss -anptl | grep mysql
LISTEN     0      128          *:4567                     *:*                   users:(("mysqld",pid=61739,fd=11))
LISTEN     0      128         :::3306                    :::*                   users:(("mysqld",pid=61739,fd=31))

10、在pxc01主机查看初始密码并登录到MySQL

# 查看初始密码
[root@pxc-01 ~]# grep password /usr/local/mysql/data/error.log 
2020-02-26T13:11:29.770228Z 1 [Note] A temporary password is generated for root@localhost: j?wh;jn=7uB.
#登录数据库,由于有特殊字符,所以要给密码用单引号引起来
[root@pxc-01 ~]# mysql -uroot -p'j?wh;jn=7uB.'    
#修改root用户的初始密码,才能进行其他操作。
root@localhost 21:47: [(none)]> alter user root@localhost identified by '123.com';
# 创建配置文件中指定的sst账号,密码一定要和配置文件中指定的密码一致。
root@localhost 21:47: [(none)]> grant all privileges on *.* to sst@localhost identified by 'pwd@123';

11、将其他节点添加到集群

注:以下操作需要在每个要加入到pxc-01集群中的节点上执行,我这里是分别在pxc-02和pxc-03节点上执行的。

[root@pxc-02 src]# mysqld --defaults-file=/etc/my.cnf &
[1] 61731
[root@pxc-02 src]# ss -anltp | grep mysql    #此刻新加入集群的节点,正在从群集内的节点中同步数据
LISTEN     0      128          *:4567                     *:*                   users:(("mysqld",pid=61731,fd=11))
[root@pxc-02 src]# ss -anltp | grep mysql   
#耐心等待一会,当发现3306端口在监听,则表示数据已同步,该节点已经加入到集群中
LISTEN     0      128          *:4567                     *:*                   users:(("mysqld",pid=61731,fd=11))
LISTEN     0      128         :::3306                    :::*                   users:(("mysqld",pid=61731,fd=36))
#启动成功后的节点,直接可以使用pxc-01节点设置的root密码进行数据库登录操作
#因为pxc-01节点的数据库信息已经同步到了本地数据库节点。
[root@pxc-02 src]# mysql -uroot -p123.com

12 、验证复制功能

在pxc-01节点创建库及表
[root@pxc-01 ~]# mysql -uroot -p123.com
root@localhost 22:23: [(none)]> create database test01;
root@localhost 22:24: [(none)]> use test01
root@localhost 22:31: [test01]> create table t1(id int primary key,name varchar(10));

# 分别在三个节点上插入一条数据
# pxc-01:
root@localhost 22:31: [test01]> insert into t1(id,name) values(1,'张三');
# pxc-02:
root@localhost 22:33: [(none)]> insert into test01.t1(id,name) values(2,'李四');
# pxc-03:
root@localhost 22:33: [(none)]> insert into test01.t1(id,name) values(3,'王五');


13、添加MySQL为系统服务

添加为系统服务后,以后再启停服务就不需要想第一次启动服务一样使用mysqld命令来启动了,而是直接使用systemctl start mysqld命令即可。

#拷贝脚本文件
[root@pxc-01 ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@pxc-01 ~]# chkconfig --add mysqld    #添加为系统服务
[root@pxc-01 ~]# chkconfig mysqld on    #加入开机自启

至此,这三个节点都加入到了这个集群当中,以后若要添加其他的节点到该集群中,只要进行和pxc-02或pxc-03节点一样的配置即可(pxc-01节点的配置有些许不一样,因为它是群集中的第一个节点)。

14、查看集群状态

上述参数解释如下:

wsrep_local_state:显示当前节点状态,值为4表示正常。该状态有四个值:


推荐阅读:
  1. ProxySQL辅助的PXC集群维护模式
  2. MySQL5.7之PXC集群搭建

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

pxc集群 mysql

上一篇:准备在树莓派4上运行windows 10 arm版系统

下一篇:java常用数据结构

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》