Percona XtraDB Cluster(下文简称PXC集群)提供了MySQL高可用的一种实现方法。PXC集群以节点组成(推荐至少3节点,后面会讨论两节点的情况),每个节点都是基于常规的MySQL/Percona Server,意味着你可以从集群中分离出某节点单独使用。集群中每个节点都包含完整的数据。PXC集群主要由两部分组成:Percona Server with XtraDB和Write Set Replication patches(使用了Galera library,一个通用的用于事务型应用的同步、多主复制插件)。PXC的特性和优点:
- 同步复制
- 支持多主复制
- 支持并行复制
- 相比其他高可用方案更像真正意义上的高可用
PXC的局限和劣势:
- 当前版本(5.6.20)的复制只支持InnoDB引擎,其他存储引擎的更改不复制。然而,DDL(Data Definition Language)语句在statement级别被复制,并且,对mysql.*表的更改会基于此被复制。例如CREATE USER…语句会被复制,但是INSERT INTO mysql.user…语句则不会。(也可以通过wsrep_replicate_myisam参数开启myisam引擎的复制,但这是一个实验性的参数)
- 由于PXC集群内部一致性控制的机制,事务有可能被终止,原因如下:集群允许在两个节点上通知执行操作同一行的两个事务,但是只有一个能执行成功,另一个会被终止,同时集群会给被终止的客户端返回死锁错误(Error: 1213 SQLSTATE: 40001 (ER_LOCK_DEADLOCK))
- 写入效率取决于节点中最弱的一台,因为PXC集群采用的是强一致性原则,一个更改操作在所有节点都成功才算执行成功
下面从安装部署和功能和性能测试三个方面开始PXC之旅。
安装部署
实验环境:三台server(硬件配置相同),信息如下:
node #1hostname: percona1IP: 192.168.1.35node #2hostname: percona2IP: 192.168.1.36node #3hostname: percona3IP: 192.168.1.37注意:Firewall has been set up to allow connecting to ports 3306, 4444,4567 and 4568 SELinux isdisabled如果不关闭SELinux,启动其他(node1之外)节点时,错误日志里会记录“[ERROR] WSREP:Permission denied”在三台server安装Percona-XtraDB-Cluster-56
- 安装epel源
yum install http://dl.Fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
(包含比较全比较新的软件源。最关键的是包含PreconaXtraDB Cluster所依赖的socat软件)
vi /etc/yum.repos.d/epel.repo (修改epel原配置以可用epel源)
将其中baseurl行的注释去掉,同时将mirror行注释 - 安装Precona XtraDB Cluster所需要的扩展包,以防在后期配置时候报错
yum install -y cmake gcc gcc-c++ libaio libaio-devel automake autoconf bzr bisonlibtool ncurses5-devel boost - 安装配置Precona 官方yum源
yum install http://www.percona.com/downloads/percona-release/percona-release-0.0-1.x86_64.rpm - 安装socat
socat is a relay for bidirectional data transfer between two independentdata channels. (Socat是一个在两个独立数据的双向传输之间起到中继作用的软件)配置好epel源之后,可直接执行 yum install socat*
若无法yum安装socat,则按照以下步骤编译安装wgethttp://www.dest-unreach.org/socat/download/socat-1.7.2.4.tar.gztar zxvfsocat-1.7.2.4.tar.gz./configureMake && make install - 安装perl组件(xtrabackup需要的组件)
yum install perl-DBD-MySQL perl-DBI perl-Time-HiRes - 安装Percona-XtraDB-Cluster及其相关组件
yum install Percona-XtraDB-Cluster-56 (如下图)
初始化Percona-XtraDB-Cluster集群
在任意节点(一般为node1)上执行集群的初始化操作
创建/etc/my.cnf,内容如下
[mysqld]datadir=/var/lib/mysqluser=mysql# Path to Galeralibrarywsrep_provider=/usr/lib64/libgalera_smm.so# Cluster connectionURL contains the IPs of node#1, node#2 and node#3----所有节点的ip#第一次启动node1节点时,此处不写各节点IP,需写成下面一行配置wsrep_cluster_address=gcomm://#第一次启动node1(初始化集群)完成后,此处需要改成下面一行配置#wsrep_cluster_address=gcomm://192.168.1.35,192.168.1.36,192.168.1.37# In order for Galerato work correctly binlog format should be ROWbinlog_format=ROW# MyISAM storageengine has only experimental supportdefault_storage_engine=InnoDB# This changes howInnoDB autoincrement locks are managed and is a requirement for Galerainnodb_autoinc_lock_mode=2# Node #1 address----本机ipwsrep_node_address=192.168.1.35# SST method----节点间同步的方式wsrep_sst_method=xtrabackup-v2# Cluster namewsrep_cluster_name=my_CentOS_cluster# Authentication forSST method----来做节点间数据同步的账号密码wsrep_sst_auth="sstuser:s3cret"(其他mysql相关参数绝大多数也可以直接在配置文件里添加)
注意:第一次启动node1(初始化集群)时,配置文件里wsrep_cluster_address=gcomm:// 不需加上各节点IP,否则其他节点会无法启动;当初始化完成后,需将此处修改为加上各节点IP在node1执行/etc/init.d/mysql bootstrap-pxc 来初始化集群,如图
然后再修改配置文件my.cnf将
wsrep_cluster_address=gcomm:// 行改为
wsrep_cluster_address=gcomm://192.168.1.35,192.168.1.36,192.168.1.37然后,执行
service mysql restart 至此,集群初始化完成,并且node1启动置mysql root密码
mysql@percona1>UPDATE mysql.user SET password=PASSWORD("Passw0rd") whereuser=’root’; mysql@percona1>FLUSH PRIVILEGES; 设置用于复制的账户密码
mysql@percona1>GRANT RELOAD, LOCK TABLES, REPLICATION CLIENT ON *.* TO ’sstuser’@’localhost’identified by ‘s3cret’;配置node2、node3并启动
将node1的/etc/my.cnf内容拷贝至node2和node3,并修改其中
wsrep_node_address=192.168.1.35改为本机ip。然后启动node2、node3 。执行
service mysql start 注意:node2、node3会从node1同步账号设置的更改配置成功以后如果集群内所有节点实例均crash后,再启动时都需要选择一个存有最新正 确数据的节点作为主节点,执 行
/etc/init.d/mysql bootstrap-pxc 启动; 然后再启动他节点。
写入效率初测
测试方法为
1. 将三个节点分别作为单独的server导入一个sql文件,执行三次取时间平均值
time mysql -uroot -pxxx system < /system.sql平均时间约为6m20s2. 开启完整集群,在三个节点分别执行一遍导入操作,取三次执行时间平均值
平均时间约为7m50s经过初步测试写性能PXC集群相比单server下降约12%。
部分参数调优
- Variable wsrep_slave_threads :复制线程的数量,galera支持真正的并行复制,适当增大此值可以在复制时获得更好的吞吐量,如果更改此值后遇到复制的某些问题,尝试将此值改回1,查看是否解决。(默认1)
- wsrep_provider_options=”gcache.size=512M”,调节galera的缓存大小,或许对减少IO有帮助
- variable gcache.mem_size
- variable gcache.size 此参数定义作为‘IST增量同步’的内容源的galera.cache文件的大小。此文件设置的大些以便节点重新加入集群式更有可能采用IST而非SST。(默认128M)
- variable gcs.fc_master_slave 此变量指定集群中是否只有一个master节点
- variable pc.weight This variable specifies thenode weight that’sgoing to be used for Weighted Quorumcalculations. Default Value 1
- innodb_flush_log_at_trx_commit
为0时:log buffer 每秒向logfile 刷新一次,同时log file 每秒执行一次所记录语句,以使数据持久化。
为1时:默认值。每次事务提交后,log buffer向log file 刷新一次,同时log file 执行一次数据持久化操作。
为2时:每次事务提交后,log buffer向log file 刷新一次,但是从log file 向磁盘做数据持久化的操作是每秒执行一次
将此值改为0后,插入全量测试数据的时间有了质的飞越
Hapeoxy代理
上文主要介绍了PXC集群本身的内容,PXC作为一个集群,需要前端配以代理服务器,做负载均衡,才能真正实现其高可用的价值。Percona官方推荐用haproxy作为前端代理,下面介绍一下haproxy的简要配置:个人感觉haproxy配置上比lvs相对简单,但功能并不逊色,我试验环境的haproxy配置文件如下
#---------------------------------------------------------------------# Example configuration for a possibleweb application.See the# full configuration options online.#http://haproxy.1wt.eu/download/1.4/doc/configuration.txt#---------------------------------------------------------------------# Global settings#---------------------------------------------------------------------global # to have these messages end up in /var/log/haproxy.log you will # need to: # 1) configure syslog to accept network log events.This is done #by adding the "-r" option tothe SYSLOGD_OPTIONS in #/etc/sysconfig/syslog # 2) configure local2 events to go to the /var/log/haproxy.log # file. A line like thefollowing can be added to # /etc/sysconfig/syslog # #local2.* /var/log/haproxy.log # log 127.0.0.1local0 log 127.0.0.1local1 notice chroot/var/lib/haproxy pidfile /var/run/haproxy.pid maxconn 4000 userhaproxy group haproxy daemon # turn on stats unix socket stats socket /var/lib/haproxy/stats#---------------------------------------------------------------------# common defaults that all the"listen" and "backend" sections will# use if not designated in their block#---------------------------------------------------------------------defaults modehttp log global optiontcplog option dontlognull#option http-server-close#option forwardfor except127.0.0.0/8 optionredispatch retries 3 maxconn 2000 timeout connect 5s timeout client50s timeout server50s#timeout http-keep-alive 10s timeout check 10slisten mysql-cluster 0.0.0.0:3306mode tcpbalance roundrobinserver node1 192.168.1.35:3306 checkserver node2 192.168.1.36:3306 checkserver node3 192.168.1.37:3306 check# 建议首先设置haproxy的监控界面,便于直观的观察后端节点是否在线listen status 192.168.1.34:8080 stats enablestats uri /status stats auth admin:xxxx stats realm (haproxy statistic)如何搭建Percona XtraDB Cluster集群 http://www.linuxidc.com/Linux/2016-06/132700.htm
Percona XtraDB Cluster 的详细介绍:请点这里
Percona XtraDB Cluster 的下载地址:请点这里
本文永久更新链接地址