1:PXC安装环境

OS:CentOS 6.5

集群名称:pxc-zhang
mysq1:192.168.1.21
mysq1:192.168.1.22
mysq1:192.168.1.23


2:关闭防火墙和 Selinux
chkconfig iptables off
vi /etc/selinux/config
SELINUX=disabled
reboot

3:创建mysql用户和组
3个节点都要做
groupadd MySQL
useradd –g mysql mysql


4、主要软件的安装
3个节点都要做
软件的存放位置在/usr/local/mysql/下
首先先配置好yum源
rpm -Uvh epel-release-6-8.noarch.rpm
 
4.1:解压安装Percona-XtraDB-Cluster-5.6.26-rel74.0-25.12.1.Linux.x86_64.tar.gz
tar -zxvf Percona-XtraDB-Cluster-5.6.26-rel74.0-25.12.1.Linux.x86_64.tar.gz
mv Percona-XtraDB-Cluster-5.6.26-rel74.0-25.12.1.Linux.x86_64 /usr/local
然后软连接到/usr/local/mysql上
cd /usr/local/
ln -s Percona-XtraDB-Cluster-5.6.26-rel74.0-25.12.1.Linux.x86_64 mysql
授予mysql用户组的权限
chown –R mysql:mysql /usr/local/mysql


mkdir -p /mysql/data
mkdir -p /mysql/logs
mkdir -p /mysql/temp
chown -R mysql:mysql /mysql

yum -y install libev
yum install http://www.percona.com/downloads/percona-release/redhat/0.1-3/percona-release-0.1-3.noarch.rpm
rpm -Uvh percona-xtrabackup-2.3.4-1.el6.x86_64.rpm 
yum -y install socat
yum -y install gcc


5:配置mysql参数文件
vi /etc/my.cnf

[client]
port            = 3306
socket          = /tmp/mysql3306.sock 
[mysql]
prompt="\\u@\\h:\\p  [\\d]>
#pager="less -i -n -S"
#tee=/home/mysql/query.log
no-auto-rehash

[mysqld]
#misc
user = mysql
basedir = /usr/local/mysql
datadir = /data/mysql/mysql3306/data
port = 3306
socket = /tmp/mysql3306.sock
event_scheduler = 0
 
tmpdir=/mysql/tmp
#timeout
interactive_timeout = 300
wait_timeout = 300
 
#character set
character-set-server = utf8 
open_files_limit = 65535
max_connections = 100
max_connect_errors = 100000
#
explicit_defaults_for_timestamp
#logs
log-output=file
slow_query_log = 1
slow_query_log_file = /mysql/logs/slow.log
log-error = /mysql/logs/error.log
log_warnings = 2
pid-file = /mysql/temp/mysql.pid
long_query_time = 1
#log-slow-admin-statements = 1
#log-queries-not-using-indexes = 1
log-slow-slave-statements = 1
 
#binlog
binlog_format = row
server-id = 213306     ###ip最后一位+端口号
log-bin =/mysql/logs/mysql-bin
binlog_cache_size = 1M
max_binlog_size = 200M
max_binlog_cache_size = 2G
sync_binlog = 1
expire_logs_days = 5
 
#relay log
skip_slave_start = 1
max_relay_log_size = 500M
relay_log_purge = 1
relay_log_recovery = 1
log_slave_updates
#slave-skip-errors=1032,1053,1062
 
#buffers & cache
table_open_cache = 2048
table_definition_cache = 2048
table_open_cache = 2048
max_heap_table_size = 96M
sort_buffer_size = 2M
join_buffer_size = 2M
thread_cache_size = 256
query_cache_size = 0
query_cache_type = 0
query_cache_limit = 256K
query_cache_min_res_unit = 512
thread_stack = 192K
tmp_table_size = 96M
key_buffer_size = 8M
read_buffer_size = 2M
read_rnd_buffer_size = 16M
bulk_insert_buffer_size = 32M
 
#myisam
myisam_sort_buffer_size = 32M
myisam_max_sort_file_size = 2G
myisam_repair_threads = 1
 
#innodb
innodb_buffer_pool_size = 1024M
innodb_buffer_pool_instances = 1
innodb_data_file_path =ibdata1:1024M:autoextend
innodb_flush_log_at_trx_commit = 1
innodb_log_buffer_size = 64M
innodb_log_file_size = 256M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_file_per_table = 1
innodb_rollback_on_timeout
innodb_status_file = 1
innodb_io_capacity = 2000
transaction_isolation = READ-COMMITTED
innodb_flush_method = O_DIRECT
 
#pxc
default_storage_engine=Innodb
innodb_locks_unsafe_for_binlog=1
innodb_autoinc_lock_mode=2
 
wsrep_cluster_name=pxc-zhang 
wsrep_cluster_address=gcomm://192.168.1.21,192.168.1.22,192.168.1.23    ##3个节点的ip
wsrep_node_address=192.168.1.21                                                                         ###本机的ip
wsrep_provider=/usr/local/mysql/lib/libgalera_smm.so
 
# wsrep_sst_method=rsync                         ##sst的模式
wsrep_sst_method=xtrabackup-v2             ##sst的模式
wsrep_sst_auth=zhang:mysql123              ##sst的用户名和密码

6:初始化mysql,3个节点都要做
[root@mysql1 mysql]# ./scripts/mysql_install_db 
./bin/my_print_defaults: error while loading shared libraries: libssl.so.6: cannot open shared object file: No such file or directory
FATAL ERROR: Neither host 'mysql1' nor 'localhost' could be looked up with
./bin/resolveip
Please configure the 'hostname' command to return a correct
hostname.
If you want to solve this at a later stage, restart this script
with the --force option

在进行安装初始化的时候报错,提示找不到libss.so.6这个包,然后locate了一下,发现系统内部是有这个包的,
只是版本不同,然后我将libssl.so.10的这个包软连接为libssl.so.6
[root@mysql1 mysql]# locate libssl
/usr/lib64/.libssl.so.1.0.1e.hmac
/usr/lib64/.libssl.so.10.hmac
/usr/lib64/libssl.so
/usr/lib64/libssl.so.1.0.1e
/usr/lib64/libssl.so.10
/usr/lib64/libssl3.so
/usr/lib64/pkgconfig/libssl.pc

[root@mysql1 mysql]# ln -s /usr/lib64/libssl.so.10 /usr/lib64/libssl.so.6
[root@mysql1 mysql]# ./scripts/mysql_install_db 
./bin/my_print_defaults: error while loading shared libraries: libcrypto.so.6: cannot open shared object file: No such file or directory
FATAL ERROR: Neither host 'mysql1' nor 'localhost' could be looked up with
./bin/resolveip
Please configure the 'hostname' command to return a correct
hostname.
If you want to solve this at a later stage, restart this script
with the --force option
[root@mysql1 mysql]# locate libcrypto
/usr/lib64/.libcrypto.so.1.0.1e.hmac
/usr/lib64/.libcrypto.so.10.hmac
/usr/lib64/libcrypto.so
/usr/lib64/libcrypto.so.1.0.1e
/usr/lib64/libcrypto.so.10
/usr/lib64/pkgconfig/libcrypto.pc

[root@mysql1 mysql]# ln -s /usr/lib64/libcrypto.so.10 /usr/lib64/libcrypto.so.6

[root@mysql1 mysql]# ./scripts/mysql_install_db 
Installing MySQL system tables...2016-05-22 03:07:56 0 [Note] ./bin/mysqld (mysqld 5.6.26-74.0-25.12-log) starting as process 2850 ...
OK
Filling help tables...2016-05-22 03:08:08 0 [Note] ./bin/mysqld (mysqld 5.6.26-74.0-25.12-log) starting as process 2875 ...
OK

出现两个OK,初始化已经成功了

[root@mysql1 mysql]# 

7:PXC的启动
[root@mysql1 mysql]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
[root@mysql1 mysql]# /etc/init.d/mysql bootstrap-pxc
Bootstrapping PXC (Percona XtraDB Cluster)Starting MySQL (Percona XtraDB Cluster).......[  OK  ]


然后就是启动第一个节点,第一个节点在启动的时候要使用bootstrap-pxc的方式启动
也可以使用/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf--wsrep_new_cluster &
进行第一个节点的启动
其余的节点直接使用 /etc/init.d/mysql start启动

[root@mysql1 mysql]# /usr/local/mysql/bin/mysql -S /tmp/mysql3306.sock
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.26-74.0-25.12-log Percona XtraDB Cluster binary (GPL) 5.6.26-25.12, Revision 624ef81, wsrep_25.12
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.

"root@localhost:mysql3306.sock  [(none)]>

连接到mysql进行安全初始化
delete from mysql.user where user!=’root’or host!=’localhost’;
truncate table mysql.db;
drop database test;
flush privileges;
用户授权
grant all privileges on *.* to zhang@'%' identified by 'mysql123';

grant reload,create tablespace,lock tables ,replication client,super on *.* to 'zhang'@localhost;

flush privileges;



8:PXC的关闭
/etc/init.d/mysql stop 关闭
Ps:最后一个停止的节点,需要第一个启动,启动方式为bootstrap-pxc启动


9、遇到的错误及解决方法
9.1
第二个节点启动报错
解决方法:
这是机器异常停了,lock文件没删除就样,重启机器后就可以正常启动了
如果重新启动后还是出现这个错误,那么更新下percona-xtrabackup版本,更新到最新版本


查看节点2的error.log,如果是和xtrabackup相关的错误,先查看节点1的xtrabackup日志,
在启动过程中,可以看到是通过xtrabackup来同步节点1的DB,达到一个一致。
9.2启动第三个个节点,并查看状态。


"root@localhost:mysql3306.sock  [testdb]> show status like 'wsrep%';
+------------------------------+-------------------------------------------------------+
| Variable_name                | Value                                                 |
+------------------------------+-------------------------------------------------------+
| wsrep_local_state_uuid       | aa5d7609-1f88-11e6-b293-0e080815500d                  |
| wsrep_protocol_version       | 7                                                     |
| wsrep_last_committed         | 13                                                    |
| 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               | 4                                                     |
| wsrep_received_bytes         | 489                                                   |
| wsrep_local_commits          | 0                                                     |
| wsrep_local_cert_failures    | 0                                                     |
| wsrep_local_replays          | 0                                                     |
| wsrep_local_send_queue       | 0                                                     |
| wsrep_local_send_queue_max   | 2                                                     |
| wsrep_local_send_queue_min   | 0                                                     |
| wsrep_local_send_queue_avg   | 0.333333                                              |
| 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    | 13                                                    |
| wsrep_flow_control_paused_ns | 0                                                     |
| wsrep_flow_control_paused    | 0.000000                                              |
| wsrep_flow_control_sent      | 0                                                     |
| wsrep_flow_control_recv      | 0                                                     |
| wsrep_cert_deps_distance     | 1.000000                                              |
| wsrep_apply_oooe             | 0.000000                                              |
| wsrep_apply_oool             | 0.000000                                              |
| wsrep_apply_window           | 1.000000                                              |
| wsrep_commit_oooe            | 0.000000                                              |
| wsrep_commit_oool            | 0.000000                                              |
| wsrep_commit_window          | 1.000000                                              |
| wsrep_local_state            | 4                                                     |
| wsrep_local_state_comment    | Synced                                                |
| wsrep_cert_index_size        | 1                                                     |
| wsrep_cert_bucket_count      | 22                                                    |
| wsrep_gcache_pool_size       | 1690                                                  |
| wsrep_causal_reads           | 0                                                     |
| wsrep_cert_interval          | 0.000000                                              |
| wsrep_incoming_addresses     | 192.168.1.22:3306,192.168.1.23:3306,192.168.1.21:3306 |
| wsrep_evs_delayed            |                                                       |
| wsrep_evs_evict_list         |                                                       |
| wsrep_evs_repl_latency       | 0/0/0/0/0                                             |
| wsrep_evs_state              | OPERATIONAL                                           |
| wsrep_gcomm_uuid             | 97a22f26-1f8f-11e6-bd48-2af508fa31a4                  |
| wsrep_cluster_conf_id        | 7                                                     |
| wsrep_cluster_size           | 3                                                     |
| wsrep_cluster_state_uuid     | aa5d7609-1f88-11e6-b293-0e080815500d                  |
| wsrep_cluster_status         | Primary                                               |
| 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.12.2(rf3e626d)                                      |
| wsrep_ready                  | ON                                                    |
+------------------------------+-------------------------------------------------------+
58 rows in set (0.02 sec)

Galera是Codership提供的多主数据同步复制机制,可以实现多个节点间的数据同步复制以及读写,并且可保障数据库的服务高可用及数据一致性。

基于Galera的高可用方案主要有MariaDB Galera Cluster和Percona XtraDB Cluster(简称PXC),目前PXC用的会比较多一些。

PXC的优点

  • 服务高可用;
  • 数据同步复制(并发复制),几乎无延迟;
  • 多个可同时读写节点,可实现写扩展,不过最好事先进行分库分表,让各个节点分别写不同的表或者库,避免让galera解决数据冲突;
  • 新节点可以自动部署,部署操作简单;
  • 数据严格一致性,尤其适合电商类应用;
  • 完全兼容MySQL;

虽然有这么多好处,但也有些局限性:

  • 只支持InnoDB引擎;
  • 所有表都要有主键;
  • 不支持LOCK TABLE等显式锁操作;
  • 锁冲突、死锁问题相对更多;
  • 不支持XA;
  • 集群吞吐量/性能取决于短板;
  • 新加入节点采用SST时代价高;
  • 存在写扩大问题;
  • 如果并发事务量很大的话,建议采用InfiniBand网络,降低网络延迟;

事实上,采用PXC的主要目的是解决数据的一致性问题,高可用是顺带实现的。因为PXC存在写扩大以及短板效应,并发效率会有较大损失,类似semi sync replication机制。

参考文档:

http://blog.csdn.net/sky__liang/article/details/50262871

http://imysql.cn/tag/pxc

Logo

更多推荐