Percona XtraDB Cluster(简称PXC)集群是基于Galera 2.x library,事务型应用下的通用的多主同步复制插件,主要用于解决强一致性问题,使得各个节点之间的数据保持实时同步以及实现多节点同时读写。提高了数据库的可靠性,也可以实现读写分离,是MySQL关系型数据库中大家公认的集群优选方案之一。本文简要介绍其原理并给出安装指导。
群集由节点组成。建议的配置是至少有3个节点,但你可以使它
2节点运行。
每个节点可以是常规MySQL或者Percona Server程序。支持将现有的MySQL/Percona Server为节点提升为集群节点。也可以从集群中分离节点,使它作为一个普通的服务器。
每个节点包含完整的数据副本。
PXC的优点:
服务高可用
数据同步复制(并发复制),几乎无延迟;
多个可同时读写节点,可实现写扩展,不过最好事先进行分库分表,让各个节点分别写不同的表或者库,避免让galera解决数据冲突;
新节点可以自动部署,部署操作简单;
数据严格一致性,尤其适合电商类应用;
完全兼容MySQL;
PXC局限性:
只支持InnoDB引擎;
所有表都要有主键;
不支持LOCK TABLE等显式锁操作;
锁冲突、死锁问题相对更多;
不支持XA;
集群吞吐量/性能取决于短板;
新加入节点采用SST时代价高;
存在写扩大问题;
如果并发事务量很大的话,建议采用InfiniBand网络,降低网络延迟;
在每个群集节点上进行以下配置
### OS版本
# cat /etc/issue
CentOS release 6.7 (Final)
Kernel \r on an \m
###将以下内容添加到所有节点
# vi /etc/hosts
192.168.1.132 node132 #主节点
192.168.1.133 node133
192.168.1.135 node135
mysql> select 'Leshami' Author,'http://blog.csdn.net/leshami' Blog,'645746311' QQ;
+---------+------------------------------+-----------+
| Author | Blog | QQ |
+---------+------------------------------+-----------+
| Leshami | http://blog.csdn.net/leshami | 645746311 |
+---------+------------------------------+-----------+
###关闭防火墙
# service iptables stop
# chkconfig iptables off
# vi /etc/selinux/config
SELINUX=disabled #修改该项为disabled
# setenforce 0 #立即生效,而无需重启OS
###配置epel源
# rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
###基于YUM方式安装
# yum -y install cmake gcc gcc-c++ libaio libaio-devel automake autoconf bzr ncurses5-devel
# yum -y install perl-DBD-MySQL perl-DBI perl-Time-HiRes
# yum install http://www.percona.com/downloads/percona-release/redhat/0.1-3/percona-release-0.1-3.noarch.rpm
# yum install Percona-XtraDB-Cluster-56
### 如果服务器无法直接连接到yum源,使用RPM安装:
# rpm -Uvh autoconf-2.63-5.1.el6.noarch.rpm
# rpm -Uvh bzr-2.1.1-2.el6.x86_64.rpm
# rpm -Uvh compat-readline5-5.2-17.1.el6.x86_64.rpm
# rpm -Uvh libaio-devel-0.3.107-10.el6.x86_64.rpm
# rpm -Uvh perl-DBI-1.609-4.el6.x86_64.rpm
# rpm -Uvh perl-DBD-MySQL-4.013-3.el6.x86_64.rpm
# rpm -Uvh automake-1.11.1-4.el6.noarch.rpm
# rpm -Uvh cmake-2.8.12.2-4.el6.x86_64.rpm
# rpm -Uvh libstdc++-4.4.7-18.el6.x86_64.rpm
# rpm -Uvh libstdc++-devel-4.4.7-18.el6.x86_64.rpm
# rpm -Uvh libgomp-4.4.7-18.el6.x86_64.rpm
# rpm -Uvh gcc-c++-4.4.7-18.el6.x86_64.rpm
# rpm -Uvh cpp-4.4.7-18.el6.x86_64.rpm
# rpm -Uvh libgcc-4.4.7-18.el6.x86_64.rpm
# rpm -Uvh libev-4.15-1.el6.rf.x86_64.rpm
# rpm -Uvh socat-1.7.2.3-1.el6.x86_64.rpm
# rpm -Uvh mysql-libs-5.1.73-8.el6_8.x86_64.rpm
# rpm -Uvh percona-xtrabackup-2.3.8-1.el6.x86_64.rpm
# rpm -ivh Percona-XtraDB-Cluster-shared-56-5.6.35-26.20.2.el6.x86_64.rpm
# rpm -ivh Percona-XtraDB-Cluster-galera-3-3.20-2.el6.x86_64.rpm
# rpm -ivh Percona-XtraDB-Cluster-client-56-5.6.35-26.20.2.el6.x86_64.rpm
# rpm -ivh Percona-XtraDB-Cluster-devel-56-5.6.35-26.20.2.el6.x86_64.rpm
# rpm -ivh Percona-XtraDB-Cluster-server-56-5.6.35-26.20.2.el6.x86_64.rpm
###本演示中主节点为:192.168.1.132
初始化主节点
# /usr/bin/mysql_install_db --basedir=/usr --user=mysql
启动mysql
# service mysql start
修改密码
# /usr/bin/mysqladmin -u root -h localhost password 'pass'
创建用户
mysql> grant reload,lock tables,replication client on *.* to 'sstuser'@'%' identified by 'xxx';
关闭mysql
# service mysql stop
修改/etc/my.cnf增加以下信息:
[mysql]
user=root
password=pass
[mysqld]
datadir=/var/lib/mysql
user=mysql
server_id=132
wsrep_provider=/usr/lib64/libgalera_smm.so
wsrep_cluster_address="gcomm://192.168.1.132,192.168.1.133,192.168.1.135"
wsrep_sst_auth=wsrep_sst_auth=sstuser:xxx
wsrep_cluster_name=my_pxc_cluster
wsrep_sst_method=rsync
wsrep_node_address=192.168.1.132
wsrep_slave_threads=2
innodb_locks_unsafe_for_binlog=1
innodb_autoinc_lock_mode=2
binlog_format=ROW
启动mysql及PXC服务
/etc/init.d/mysql bootstrap-pxc
其他节点上修改配置文件如下,以下以192.168.1.133为例
[mysql]
user=root
password=pass
[mysqld]
datadir=/var/lib/mysql
user=mysql
server_id=133
wsrep_provider=/usr/lib64/libgalera_smm.so
wsrep_cluster_address="gcomm://192.168.1.132,192.168.1.133,192.168.1.135"
wsrep_sst_auth=wsrep_sst_auth=sstuser:xxx
wsrep_cluster_name=my_pxc_cluster
wsrep_sst_method=rsync
wsrep_node_address=192.168.1.133
wsrep_slave_threads=2
innodb_locks_unsafe_for_binlog=1
innodb_autoinc_lock_mode=2
binlog_format=ROW
注:各群集节点上server_id和wsrep_node_address设置为不同的值
配置文件修改完毕后,启动当前节点mysql
注,集群非初始主节点,则无需初始化数据库
# service mysql start
mysql> show global status like 'wsrep%';
+------------------------------+--------------------------------------+
| Variable_name | Value |
+------------------------------+--------------------------------------+
| wsrep_local_state_uuid | 05e19a0d-379c-11e7-8547-f6f31f261bbc |
| wsrep_protocol_version | 7 |
| wsrep_last_committed | 6 |
| wsrep_replicated | 2 |
................ |
| wsrep_cert_interval | 0.000000 |
| wsrep_incoming_addresses | 192.168.1.132:3306,192.168.1.133:330 |
| 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 | baf33684-37ad-11e7-90ea-1e5d080abc7d |
| wsrep_cluster_conf_id | 9 |
| wsrep_cluster_size | 2 |
| wsrep_cluster_state_uuid | 05e19a0d-379c-11e7-8547-f6f31f261bbc |
| wsrep_cluster_status | Primary |
| wsrep_connected | ON |
| wsrep_local_bf_aborts | 0 |
| wsrep_local_index | 0 |
| wsrep_provider_name | Galera |
| wsrep_provider_vendor | Codership Oy |
| wsrep_provider_version | 3.20(r7e383f7) |
| wsrep_ready | ON |
+------------------------------+--------------------------------------+
59 rows in set (0.01 sec)
关注:wsrep_cluster_size 表示可用节点数,当前为2.wsrep_connected为ON,表示已连接。
另外一个节点如法炮制。
在任意节点建表以及更新数据,在其他节点均可以见。
mysql> show status like 'wsrep_cluster_status';
+----------------------+---------+
| Variable_name | Value |
+----------------------+---------+
| wsrep_cluster_status | Primary |
+----------------------+---------+
1 row in set (0.00 sec)
mysql> use test;
mysql> create table t(id int,val varchar(20));
Query OK, 0 rows affected (0.04 sec)
mysql> insert into t values(1,'robin');
Query OK, 1 row affected (0.01 sec)
--以下查询结果为133节点返回数据
mysql> select * from t;
+------+-------+
| id | val |
+------+-------+
| 1 | robin |
+------+-------+
--在其余2节点验证,应该可以看到数据。
--反之,在某一个节点上插入数据,其余节点也可以看到。