Percona XtraDB Cluster 是percona 提供的mysql 集群的高可用方案,该方案基于galera 协议,可以保证集群的高可用和数据的强一致性。
优点:
缺点:
pxc集群可以部署在两台服务器上,但是容易出现脑裂问题,因此建议部署至少3台服务器,最多8台、
IP:
192.168.56.111 centos1
192.168.56.222 centos2
192.168.56.123 centos3
系统版本:CentOS release 6.5 (Final)
xtrabackup版本:xtrabackup version 2.4.12 based on MySQL server 5.7.19 Linux (x86_64) (revision id: 170eb8c)
pxc 版本:Percona-XtraDB-Cluster-5.7.23-rel23-31.31.2.Linux.x86_64.ssl101.tar.gz
防火墙关闭:
# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destinationChain FORWARD (policy ACCEPT)
target prot opt source destinationChain OUTPUT (policy ACCEPT)
target prot opt source destination# chkconfig iptables off
关闭 selinux
sed -i 's/=enforcing/=disabled/' /etc/selinux/config
创建MySQL用户
# groupadd mysql
# useradd mysql -g mysql
解压软件
tar -xzvf Percona-XtraDB-Cluster-5.7.14-rel8-26.17.1.Linux.x86_64.ssl101.tar.gz -C /usr/local/
ln -s Percona-XtraDB-Cluster-5.7.14-rel8-26.17.1.Linux.x86_64.ssl101/ mysql
chown mysql:mysql -R /usr/local/mysql
创建数据目录
# mkdir -p /data/mysql
# chown -R mysql:mysql /data/mysql
安装系统包
检查需要的软件包: rpm -q --qf '%{NAME}-%{VERSION}-%{RELEASE} (%{ARCH})\n' git scons gcc gcc-c++ openssl check cmake bison \ boost-devel asio-devel libaio-devel ncurses-devel readline-devel pam-devel \ socat libcurl-devel 使用yum 安装 yum install -y git scons gcc gcc-c++ openssl check cmake bison \ boost-devel asio-devel libaio-devel ncurses-devel readline-devel pam-devel \ socat libcurl-devel
部分包在centos 6.5 镜像中不能存在,下载安装
- yum install asio-devel-1.10.8-1.el6.x86_64.rpm 连接:http://rpmfind.net/linux/rpm2html/search.php?query=asio-devel&submit=Search+...&system=&arch=
- 编译安装socat-1.7.3.2.tar.gz
- 编译的时候依赖yum install -y gcc tcp_wrappers-deve
- 安装scons scons-2.0.1-1.el6.noarch.rpm
编辑my.cnf
vi /etc/my.cnf
[...]
wsrep_provider=/usr/lib/libgalera_smm.so
wsrep_cluster_name=pxc-cluster
wsrep_cluster_address=gcomm://192.168.56.111,192.168.56.222,192.168.56.123
wsrep_node_name=pxc1
wsrep_node_address=192.168.56.111
wsrep_sst_method=xtrabackup-v2
wsrep_sst_auth=sst:sz
pxc_strict_mode=ENFORCING
binlog_format=ROW
default_storage_engine=InnoDB
innodb_autoinc_lock_mode=2
初始化数据库
# /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/data/mysql --user=mysql --initialize
安装xtrabackup
yum install -y percona-xtrabackup-24-2.4.12-1.el6.x86_64.rpm
需要配置yum源,会自动安装需要的DBD软件包
拷贝启动脚本,创建同步用户
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
启动集群第一个节点
/etc/init.d/mysql bootstrap-pxc
修改密码,创建同步用户,注意和my.cnf中配置相同
CREATE USER 'sst'@'localhost' IDENTIFIED BY 'zs'; GRANT RELOAD, LOCK TABLES, PROCESS, REPLICATION CLIENT ON *.* TO 'sst'@'localhost'; FLUSH PRIVILEGES;
分别配置第二三节点
启动方式区别于第一节点:/etc/init.d/mysql start
centos 1:
create database db ;
use db;
create table t(id int primary key );
insert into t values(1);
其他节点检查:
select * from db.t;
+----+
| id |
+----+
| 1 |
+----+
2018-12-11T07:08:37.579990Z 0 [ERROR] WSREP: Failed to read 'ready
' from: wsrep_sst_xtrabackup-v2 --role 'joiner' --address '192.168.56.2
2' --datadir '/data/mysql/' --defaults-file '/etc/my.cnf' --defaults-group-suffix '' --parent '4380' --binlog '/data/mysql/mybinlog'
Read: '(null)'
2018-12-11T07:08:37.580028Z 0 [ERROR] WSREP: Process completed with error: wsrep_sst_xtrabackup-v2 --role 'joiner' --address '192.168.56.22' --d
atadir '/data/mysql/' --defaults-file '/etc/my.cnf' --defaults-group-suffix '' --parent '4380' --binlog '/data/mysql/mybinlog' : 2 (No such file
or directory)
2018-12-11T07:08:37.580075Z 1 [ERROR] WSREP: Failed to prepare for 'xtrabackup-v2' SST. Unrecoverable.
2018-12-11T07:08:37.580082Z 1 [ERROR] Aborting单独执行
# wsrep_sst_xtrabackup-v2 --role 'joiner' --address '192.168.56.22' --datadir '/data/mysql/' --defaults-file '/etc/my.cnf' --defaults-group-suffix '' --parent '13252' --binlog '/data/mysql/mybinlog'
报错如下:
WSREP_SST: [INFO] The xtrabackup version is 2.4.12 (20181211 18:21:58.156)
WSREP_SST: [INFO] Streaming with xbstream (20181211 18:21:58.303)
WSREP_SST: [INFO] Using socat as streamer (20181211 18:21:58.305)
which: no socat in (/usr/sbin:/sbin:/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql/bin)
WSREP_SST: [ERROR] socat not found in path: /usr/sbin:/sbin:/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql/bin (20181211 18:21:58.308)
/usr/bin/mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/data/mysql --user=mysql --initialize
Error: Package: percona-xtrabackup-24-2.4.12-1.el6.x86_64 (/percona-xtrabackup-24-2.4.12-1.el6.x86_64)
Requires: libev.so.4()(64bit)
系统中缺少libev-4 的包,下载安装即可:http://rpmfind.net/linux/rpm2html/search.php
安装xtrabackup的时候可以使用yum 安装,此方式会同时安装需要的 perl-DBD-MySQL
602 WSREP_SST: [ERROR] Error while getting data from donor node: exit codes: 137 0 (20181218 10:57:54.289)
603 WSREP_SST: [ERROR] Cleanup after exit with status:32 (20181218 10:57:54.291)
先使用 wsrep_sst_method=rsync 启动,节点成功加入集群之后,再改成wsrep_sst_method=xtrabackup-v2,重启节点服务