Percona XtraDB Cluster 初探

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 #1
    hostname: percona1
    IP: 192.168.1.35
node #2
    hostname: percona2
    IP: 192.168.1.36
node #3
    hostname: percona3
    IP: 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

  1. 安装Precona XtraDB Cluster所需要的扩展包,以防在后期配置时候报错
    yum install -y cmake gcc gcc-c++ libaio libaio-devel automake autoconf bzr bisonlibtool ncurses5-devel boost

  2. 安装配置Precona 官方yum源
    yum install http://www.percona.com/downloads/percona-release/percona-release-0.0-1.x86_64.rpm
  3. 安装socat
    socat is a relay for bidirectional data transfer between two independentdata channels. (Socat是一个在两个独立数据的双向传输之间起到中继作用的软件)

    配置好epel源之后,可直接执行 yum install socat*
    若无法yum安装socat,则按照以下步骤编译安装

    wget  http://www.dest-unreach.org/socat/download/socat-1.7.2.4.tar.gz
    tar zxvf  socat-1.7.2.4.tar.gz
    ./configure
    Make && make install
  4. 安装perl组件(xtrabackup需要的组件)
    yum install perl-DBD-MySQL perl-DBI perl-Time-HiRes

  5. 安装Percona-XtraDB-Cluster及其相关组件
    yum install Percona-XtraDB-Cluster-56 (如下图)
    image

初始化Percona-XtraDB-Cluster集群

在任意节点(一般为node1)上执行集群的初始化操作
创建/etc/my.cnf,内容如下

[mysqld]
datadir=/var/lib/mysql
user=mysql
# Path to Galeralibrary
wsrep_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 ROW
binlog_format=ROW
# MyISAM storageengine has only experimental support
default_storage_engine=InnoDB
# This changes howInnoDB autoincrement locks are managed and is a requirement for Galera
innodb_autoinc_lock_mode=2
# Node #1 address----本机ip
wsrep_node_address=192.168.1.35
# SST method----节点间同步的方式
wsrep_sst_method=xtrabackup-v2
# Cluster name
wsrep_cluster_name=my__cluster
# Authentication forSST method----来做节点间数据同步的账号密码
wsrep_sst_auth="sstuser:s3cret"

(其他mysql相关参数绝大多数也可以直接在配置文件里添加)

注意:第一次启动node1(初始化集群)时,配置文件里wsrep_cluster_address=gcomm:// 不需加上各节点IP,否则其他节点会无法启动;当初始化完成后,需将此处修改为加上各节点IP

在node1执行/etc/init.d/mysql bootstrap-pxc 来初始化集群,如图
image

然后再修改配置文件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
平均时间约为6m20s

2. 开启完整集群,在三个节点分别执行一遍导入操作,取三次执行时间平均值

平均时间约为7m50s

经过初步测试写性能PXC集群相比单server下降约12%。

部分参数调优

  1. Variable wsrep_slave_threads :复制线程的数量,galera支持真正的并行复制,适当增大此值可以在复制时获得更好的吞吐量,如果更改此值后遇到复制的某些问题,尝试将此值改回1,查看是否解决。(默认1)

  2. wsrep_provider_options=”gcache.size=512M”,调节galera的缓存大小,或许对减少IO有帮助

  3. variable gcache.mem_size

  4. variable gcache.size 此参数定义作为‘IST增量同步’的内容源的galera.cache文件的大小。此文件设置的大些以便节点重新加入集群式更有可能采用IST而非SST。(默认128M)

  5. variable gcs.fc_master_slave 此变量指定集群中是否只有一个master节点

  6. variable pc.weight This variable specifies thenode weight that’sgoing to be used for Weighted Quorumcalculations. Default Value 1

  7. 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.1      local0
   log         127.0.0.1      local1 notice
   chroot      /var/lib/haproxy
   pidfile     /var/run/haproxy.pid
   maxconn     4000
   user        haproxy
   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
   mode                    http
   log                     global
   option                  tcplog
   option                 dontlognull
#  option http-server-close
#  option forwardfor       except127.0.0.0/8
   option                  redispatch
   retries                 3
   maxconn                 2000
   timeout connect         5s
   timeout client          50s
   timeout server          50s
#  timeout http-keep-alive 10s
   timeout check           10s
listen mysql-cluster 0.0.0.0:3306
    mode tcp
    balance roundrobin
    server node1 192.168.1.35:3306 check
    server node2 192.168.1.36:3306 check
    server node3 192.168.1.37:3306 check
# 建议首先设置haproxy的监控界面,便于直观的观察后端节点是否在线
listen status 192.168.1.34:8080
     stats enable 
     stats uri /status
     stats auth admin:xxxx
     stats realm (haproxy\ statistic)

Percona XtraDB Cluster 的详细介绍请点这里
Percona XtraDB Cluster 的下载地址请点这里

猜你喜欢

转载自www.linuxidc.com/Linux/2016-10/136217.htm