Percona XtraDB Cluster 初探(2)

注意:第一次启动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 平均时间约为6m20s

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

平均时间约为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. # #--------------------------------------------------------------------- # 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集群 

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/db5fcbc9a470b4d40aeadf7df23324ce.html