Percona-XtraDB-Cluster 介绍
1.1 Percona XtraDB Cluster是MySQL高可用性和可扩展性的解决方案,Percona XtraDB Cluster提供的特性如下:
- 同步复制,事务要么在所有节点提交或不提交。
- 多主复制,可以在任意节点进行写操作。
- 在从服务器上并行应用事件,真正意义上的并行复制。
- 节点自动配置。
- 数据一致性,不再是异步复制。
Percona XtraDB Cluster完全兼容MySQL和Percona Server,表现在:
- 数据的兼容性
- 应用程序的兼容性:无需更改应用程序
- 集群是有节点组成的,推荐配置至少3个节点,但是也可以运行在2个节点上。
- 每个节点都是普通的mysql/percona服务器,可以将现有的数据库服务器组成集群,反之,也可以将集群拆分成单独的服务器。
- 每个节点都包含完整的数据副本。
优点如下:
- 当执行一个查询时,在本地节点上执行。因为所有数据都在本地,无需远程访问。
- 无需集中管理。可以在任何时间点失去任何节点,但是集群将照常工作,不受影响。
- 良好的读负载扩展,任意节点都可以查询。
缺点如下:
- 加入新节点,开销大。需要复制完整的数据。
- 不能有效的解决写缩放问题,所有的写操作都将发生在所有节点上。
- 有多少个节点就有多少重复的数据。
架构图如下:
搭建环境:
centos7.2 版本:Percona-XtraDB-Cluster-5.6.24-rel72.2-25.11..Linux.x86_64.tar
- 环境无任何mysql版本,pxc本身就是数据库
- 10.0.0.51 db01
- 10.0.0.52 db02
- 10.0.0.53 db03
1.2 在节点1安装pxc
#下载方式 [root@db01 ~]# wget https://www.percona.com/downloads/Percona-XtraDB-Cluster-LATEST/Percona-XtraDB-Cluster-5.7.19-29.22-3/binary/tarball/Percona-XtraDB-Cluster-5.7.19-rel17-29.22.3.Linux.x86_64.ssl101.tar.gz -P /usr/local/src [root@db01 ~]# cd /usr/local/src/ [root@db01 src]# ll total 145880 -rw-r--r-- 1 root root 149380057 Jun 3 2015 Percona-XtraDB-Cluster-5.6.24-rel72.2-25.11..Linux.x86_64.tar.gz [root@db01 src]# tar xf Percona-XtraDB-Cluster-5.6.24-rel72.2-25.11..Linux.x86_64.tar.gz [root@db01 src]# mv Percona-XtraDB-Cluster-5.6.24-rel72.2-25.11..Linux.x86_64 ../mysql [root@db01 src]# cd ../mysql/ [root@db01 mysql]# useradd mysql [root@db01 mysql]# chown -R mysql.mysql . [root@db01 mysql]# cp support-files/my-default.cnf /etc/my.cnf [root@db01 mysql]# mkdir -p /data && chown -R mysql.mysql /data/ [root@db01 mysql]# ./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/ [root@db01 mysql]# cp support-files/mysql.server /etc/init.d/mysqld [root@db01 mysql]# chmod +x /etc/init.d/mysqld
修改/etc/my.cnf
[root@db01 mysql]# egrep -v '^#|^$' /etc/my.cnf [mysqld] basedir = /usr/local/mysql datadir = /data wsrep_provider=/usr/local/mysql/lib/libgalera_smm.so #库文件 wsrep_cluster_address=gcomm://10.0.0.51,10.0.0.52,10.0.0.53 #节点中所有ip wsrep_node_address=10.0.0.51 #节点的ip binlog_format=ROW #binlog格式必须为row default_storage_engine=InnoDB #暂时不支持其他存储引擎,只支持innodb,当然可以支持myisam,需要另外参数打开 innodb_autoinc_lock_mode=2 #自增锁的优化 wsrep_cluster_name=my-pxc #集群名字 wsrep_sst_auth=sst:123456 #sst模式需要的用户名和密码 wsrep_sst_method=xtrabackup-v2 #采用什么方式复制数据。还支持mysqldump,rsync
在节点1启动pxc
[root@db01 mysql]# /etc/init.d/mysqld bootstrap-pxc Bootstrapping PXC (Percona XtraDB Cluster)Starting MySQL (Percona XtraDB Cluster).. SUCCESS! [root@db01 mysql]# ss -lntup|egrep 'mysqld' tcp LISTEN 0 128 *:4567 *:* users:(("mysqld",pid=3630,fd=11)) tcp LISTEN 0 80 :::3306 :::* users:(("mysqld",pid=3630,fd=19)) [root@db01 mysql]# ln -s /usr/local/mysql/bin/mysql /usr/bin [root@db01 mysql]# mysql mysql: error while loading shared libraries: libreadline.so.5: cannot open shared object file: No such file or directory [root@db01 mysql]# yum install readline-devel readline -y [root@db01 mysql]# cd /usr/lib64 [root@db01 lib64]# ln -s ../../lib64/libreadline.so.6 libreadline.so.5 [root@db01 lib64]# mysql Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 3 Server version: 5.6.24-72.2-25.11 Percona XtraDB Cluster binary (GPL) 5.6.24-25.11, Revision, wsrep_25.11 Copyright (c) 2009-2015 Percona LLC and/or its affiliates Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved. 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 global status like "wsrep%"; #查看
mysql> GRANT RELOAD, LOCK TABLES, REPLICATION CLIENT ON * . * TO 'sst'@'localhost' IDENTIFIED BY '123456';
mysql> FLUSH PRIVILEGES;
1.3 在节点2安装pxc
[root@db02 ~]# cd /usr/local/src/ [root@db02 src]# ll total 145880 -rw-r--r-- 1 root root 149380057 Jun 3 2015 Percona-XtraDB-Cluster-5.6.24-rel72.2-25.11..Linux.x86_64.tar.gz [root@db02 src]# tar xf Percona-XtraDB-Cluster-5.6.24-rel72.2-25.11..Linux.x86_64.tar.gz [root@db02 src]# mv Percona-XtraDB-Cluster-5.6.24-rel72.2-25.11..Linux.x86_64 ../mysql [root@db02 src]# cd ../mysql/ [root@db02 mysql]# useradd mysql [root@db02 mysql]# chown -R mysql.mysql . [root@db02 mysql]# cp support-files/my-default.cnf /etc/my.cnf [root@db02 mysql]# mkdir -p /data && chown -R mysql.mysql /data/ [root@db02 mysql]# ./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/ [root@db02 mysql]# cp support-files/mysql.server /etc/init.d/mysqld [root@db02 mysql]# chmod +x /etc/init.d/mysqld
修改节点二的配置文件
[root@db02 mysql]# egrep -v "^#|^$" /etc/my.cnf [mysqld] basedir = /usr/local/mysql datadir = /data wsrep_provider=/usr/local/mysql/lib/libgalera_smm.so wsrep_cluster_address=gcomm://10.0.0.51,10.0.0.52,10.0.0.53 wsrep_node_address=10.0.0.52 wsrep_slave_threads=2 binlog_format=ROW default_storage_engine=InnoDB innodb_autoinc_lock_mode=2 wsrep_cluster_name=my-pxc wsrep_sst_auth=sst:123456 wsrep_sst_method=rsync #不知道为什么xtrabackup刚开始一直报错 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES [root@db02 mysql]# yum install -y lsof net-tools #刚开始有报错,后来安装了就好啦 [root@db02 mysql]# ln -s /usr/local/mysql/bin/mysql /usr/bin [root@db02 mysql]# /etc/init.d/mysqld restart
1.3 在节点三安装pxc
[root@db03 src]# tar xf Percona-XtraDB-Cluster-5.6.24-rel72.2-25.11..Linux.x86_64.tar.gz [root@db03 src]# mv Percona-XtraDB-Cluster-5.6.24-rel72.2-25.11..Linux.x86_64 ../mysql [root@db03 src]# cd ../mysql/ [root@db03 mysql]# useradd mysql [root@db03 mysql]# chown -R mysql.mysql . [root@db03 mysql]# cp support-files/my-default.cnf /etc/my.cnf [root@db03 mysql]# mkdir -p /data && chown -R mysql.mysql /data/ [root@db03 mysql]# ./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/ [root@db03 mysql]# cp support-files/mysql.server /etc/init.d/mysqld [root@db03 mysql]# chmod +x /etc/init.d/mysqld [root@db03 mysql]# ln -s /usr/local/mysql/bin/mysql /usr/bin
节点三的配置文件
[root@db03 mysql]# egrep -v '^#|^$' /etc/my.cnf [mysqld] basedir = /usr/local/mysql datadir = /data wsrep_provider=/usr/local/mysql/lib/libgalera_smm.so wsrep_cluster_address=gcomm://10.0.0.51,10.0.0.52,10.0.0.53 wsrep_node_address=10.0.0.53 wsrep_slave_threads=2 binlog_format=ROW default_storage_engine=InnoDB innodb_autoinc_lock_mode=2 wsrep_cluster_name=my-pxc wsrep_sst_auth=sst:123456 wsrep_sst_method=rsync sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
查看集群状态
mysql> show global status like "wsrep%"; +------------------------------+----------------------------------------------+ | Variable_name | Value | +------------------------------+----------------------------------------------+ | wsrep_local_state_uuid | 6cb81602-f146-11e7-bc2c-07065346193e |在集群所有节点的值应该是相同的,有不同值的节点,说明其没有连接入集群. | wsrep_protocol_version | 7 | | wsrep_last_committed | 3 | | wsrep_replicated | 0 | | wsrep_replicated_bytes | 0 | | wsrep_repl_keys | 0 | | wsrep_repl_keys_bytes | 0 | | wsrep_repl_data_bytes | 0 | | wsrep_repl_other_bytes | 0 | | wsrep_received | 3 | | wsrep_received_bytes | 275 | | wsrep_local_commits | 0 | | wsrep_local_cert_failures | 0 | | wsrep_local_replays | 0 | | wsrep_local_send_queue | 0 | | wsrep_local_send_queue_max | 1 | | wsrep_local_send_queue_min | 0 | | wsrep_local_send_queue_avg | 0.000000 |网络瓶颈的预兆.如果这个值比较高的话,可能存在网络瓶冲突或死锁的数目:越小越好
| wsrep_local_recv_queue | 0 | | wsrep_local_recv_queue_max | 1 | | wsrep_local_recv_queue_min | 0 | | wsrep_local_recv_queue_avg | 0.000000 | | wsrep_local_cached_downto | 0 | | wsrep_flow_control_paused_ns | 0 | | wsrep_flow_control_paused | 0.000000 |表示复制停止了多长时间.即表明集群因为Slave延迟而慢的程度.值为0~1,越靠近0越好,值为1表示复制完全停止.可优化wsrep_slave_threads的值来改善. | wsrep_flow_control_sent | 0 |表示该节点已经停止复制了多少次. 越小越好 | wsrep_flow_control_recv | 0 | | wsrep_cert_deps_distance | 0.000000 | | wsrep_apply_oooe | 0.000000 | | wsrep_apply_oool | 0.000000 | | wsrep_apply_window | 0.000000 | | wsrep_commit_oooe | 0.000000 | | wsrep_commit_oool | 0.000000 | | wsrep_commit_window | 0.000000 | | wsrep_local_state | 4 | | wsrep_local_state_comment | Synced | | wsrep_cert_index_size | 0 | | wsrep_cert_bucket_count | 22 | | wsrep_gcache_pool_size | 4096 | | wsrep_causal_reads | 0 | | wsrep_cert_interval | 0.000000 | | wsrep_incoming_addresses | 10.0.0.51:3306,10.0.0.53:3306,10.0.0.52:3306 | | wsrep_evs_delayed | | | wsrep_evs_evict_list | | | wsrep_evs_repl_latency | 0/0/0/0/0 | | wsrep_evs_state | OPERATIONAL | | wsrep_gcomm_uuid | 8c2913c8-f15c-11e7-a2f5-e2ba6980b512 | | wsrep_cluster_conf_id | 35 | | wsrep_cluster_size | 3 |如果这个值跟预期的节点数一致,则所有的集群节点已经连接.
| wsrep_cluster_state_uuid | 6cb81602-f146-11e7-bc2c-07065346193e | | wsrep_cluster_status | Primary |集群组成的状态.如果不为”Primary”,说明出现”分区”或是”split-brain”状况. | wsrep_connected | ON | | wsrep_local_bf_aborts | 0 | | wsrep_local_index | 1 | | wsrep_provider_name | Galera | | wsrep_provider_vendor | Codership Oy <info@codership.com> | | wsrep_provider_version | 3.11(r93aca2d) | | wsrep_ready | ON | #wsrep_ready 为ON,则说明MariaDB Galera集群已经正确运行了 +------------------------------+----------------------------------------------+ 58 rows in set (0.02 sec)
故障解决:
2018-01-20 00:57:21 3798 [ERROR] WSREP: failed to open gcomm backend connection: 131: invalid UUID: 00000000 (FATAL) at gcomm/src/pc.cpp:PC():271 2018-01-20 00:57:21 3798 [ERROR] WSREP: gcs/src/gcs_core.cpp:long int gcs_core_open(gcs_core_t*, const char*, const char*, bool)():206: Failed to open backend connection: -131 (State not recoverable) 2018-01-20 00:57:21 3798 [ERROR] WSREP: gcs/src/gcs.cpp:long int gcs_open(gcs_conn_t*, const char*, const char*, bool)():1379: Failed to open channel 'my-pxc' at 'gcomm://10.0.0.51,10.0.0.52,10.0.0.53': -131 (State not recoverable) 2018-01-20 00:57:21 3798 [ERROR] WSREP: gcs connect failed: State not recoverable 2018-01-20 00:57:21 3798 [ERROR] WSREP: wsrep::connect(gcomm://10.0.0.51,10.0.0.52,10.0.0.53) failed: 7 2018-01-20 00:57:21 3798 [ERROR] Aborting
解决方法
进入该数据库节点/data目录,将文件gvwstate.dat移除(mv)掉。重启