Percona XtraDB Cluster(下文简称PXC集群)提供了MySQL高可用的一种实现方法。PXC集群以节点组成(推荐至少3节点,便于故障恢复),每个节点都是基于常规的 MySQL/Percona Server,意味着你可以从集群中分离出某节点单独使用。集群中每个节点都包含完整的数据。
PXC集群主要由两部分组成:Percona Server with XtraDB和Write Set Replication patches(使用了Galera library,一个通用的用于事务型应用的同步、多主复制插件)。
PXC的特性和优点:
1、同步复制
2、支持多主复制
3、支持并行复制
4、作为高可用方案,相比其他方案其结构和实施相对简单明了
PXC的局限和劣势:
1、 版本(5.6.20)的复制只支持InnoDB引擎,其他存储引擎的更改不复制。然而,DDL(Data Definition Language) 语句在statement级别被复制,并且,对mysql.*表的更改会基于此被复制。例如CREATE USER...语句会被复制,但是 INSERT INTO mysql.user...语句则不会。(也可以通过wsrep_replicate_myisam参数开启myisam引擎的 复制,但这是一个实验性的参数)。
2、 由于PXC集群内部一致性控制的机制,事务有可能被终止,原因如下:集群允许在两个节点上通知执行操作同一行的两个事务,但是只有一个能执行成功,另一个 会被终止,同时集群会给被终止的客户端返回死锁错误(Error: 1213 SQLSTATE: 40001 (ER_LOCK_DEADLOCK)).
3、写入效率取决于节点中最弱的一台,因为PXC集群采用的是强一致性原则,一个更改操作在所有节点都成功才算执行成功。
部署环境: CentOS7.X
文档没讲如何配IP,如有需要的朋友自己上网查,谢谢!
脚本中配置内部源的部分,请根据实际情况修改或注释掉。
共3台虚拟机,分配1cpu1g内存20硬盘即可。为逼免发生“脑裂”,集群使用奇数台数。
172.25.0.131 pxc1
172.25.0.132 pxc2
172.25.0.133 pxc3
上传安装脚本和安装包“Percona-XtraDB-Cluster-5.7.22-29.26-r422-el7-x86_64-bundle.tar”到虚拟机的root目录
ssh到虚拟主机上
分别执行pxcX.txt文件
如
sh pxc1.txt
pxc1做为主节点。等所有脚本执行完成,在pxc2、pxc3执行
systemctl start mysql
开启服务。
pxc1.txt代码
#!/bin/bash
yum remove mariadb-libs -y
echo "正在配置源为192.168.239.241……"
sed -e "s/^metalink=/#metalink=/g" \
-e "s/^mirrorlist=http/#mirrorlist=http/g" \
-e "s@^#baseurl=@baseurl=@g" \
-e "s@http://mirror.centos.org@http://192.168.239.241@g" \
-i /etc/yum.repos.d/*.repo > /dev/null 2>&1
if [ $? -eq 0 ];then
echo -n "已经配置源为192.168.239.241!"
fi
#更新系统
yum update -y
#配置selinux
sed -i '/^SELINUX=/s/=.*/=disabled/' /etc/selinux/config
setenforce 0
if [ $? -eq 0 ];then
echo "SELinux初始化完毕!"
fi
#配置防火墙
firewall-cmd --add-port={3306,4567,4568,4444}/tcp --permanent
firewall-cmd --reload
if [ $? -eq 0 ];then
echo "Iptables防火墙初始化完毕!"
fi
##配置主机名及hosts
#echo `ip add show ens33 |grep 'inet '|awk '{print $2}'|awk -F / '{print $1}'` pxc1 >>/etc/hosts
echo 172.25.0.131 pxc1 >>/etc/hosts
echo 172.25.0.132 pxc2 >>/etc/hosts
echo 172.25.0.133 pxc3 >>/etc/hosts
hostnamectl set-hostname pxc1
#安装Percona-XtraDB-Cluster
tar xvf Percona-XtraDB-Cluster-5.7.22-29.26-r422-el7-x86_64-bundle.tar
yum install http://www.percona.com/downloads/percona-release/redhat/0.1-6/percona-release-0.1-6.noarch.rpm -y
yum localinstall Percona-XtraDB-Cluster*.rpm -y
if [ $? -eq 0 ];then
echo "PXC安装包安装完成!"
fi
chkconfig mysql off
systemctl start mysql
mysql -u root -p`grep 'temporary password' /var/log/mysqld.log|awk '{print $11}'|tail -n1 ` --connect-expired-password -e "ALTER USER 'root'@'localhost' IDENTIFIED BY 'rootPass';flush privileges;"
if [ $? -eq 0 ];then
echo "设置mysql的root密码为'rootPass'!"
fi
systemctl stop mysql
##停止mysql服务,配置wsrep
##下面三行跟据实际填写
##wsrep_cluster_address=gcomm://x.x.x.x,x.x.x.x,
##wsrep_node_name=xxx ##
##wsrep_node_address=x.x.x.x##
##下面是我的配置
mv /etc/percona-xtradb-cluster.conf.d/wsrep.cnf /etc/percona-xtradb-cluster.conf.d/wsrep.cnf.bak
cat << EOF >/etc/percona-xtradb-cluster.conf.d/wsrep.cnf
[mysqld]
wsrep_provider=/usr/lib64/galera3/libgalera_smm.so
wsrep_cluster_name=pxc-cluster
wsrep_cluster_address=gcomm://172.25.0.131,172.25.0.132,172.25.0.133
wsrep_node_name=pxc1
wsrep_node_address=172.25.0.131
wsrep_sst_method=xtrabackup-v2
wsrep_sst_auth=sstuser:passw0rd
pxc_strict_mode=ENFORCING
binlog_format=ROW
default_storage_engine=InnoDB
innodb_autoinc_lock_mode=2
EOF
#only run on first node
#After you configure all PXC nodes
systemctl start [email protected]
#show status like 'wsrep%';
mysql -u root -p'rootPass' --connect-expired-password -e "CREATE USER 'sstuser'@'localhost' IDENTIFIED BY 'passw0rd';GRANT RELOAD, LOCK TABLES, PROCESS, REPLICATION CLIENT ON *.* TO 'sstuser'@'localhost';FLUSH PRIVILEGES;"
if [ $? -eq 0 ];then
echo "设置sstuser的密码为'passw0rd'!"
fi
#only run on first node
#第一节目启动完,再启动其它节点
#在随便节点创建测试数据库
mysql -u root -p'rootPass' --connect-expired-password -e "show status like 'wsrep%';"
mysql -u root -p'rootPass' --connect-expired-password -e "CREATE DATABASE percona;USE percona;CREATE TABLE example (node_id INT PRIMARY KEY, node_name VARCHAR(30));INSERT INTO percona.example VALUES (1, 'percona1');SELECT * FROM percona.example;"
##
#在其它节点执行
#
#mysql -u root -p'rootPass' --connect-expired-password -e "USE percona;SELECT * FROM percona.example;"
pxc2.txt代码
#!/bin/bash
yum remove mariadb-libs -y
echo "正在配置源为192.168.239.241……"
sed -e "s/^metalink=/#metalink=/g" \
-e "s/^mirrorlist=http/#mirrorlist=http/g" \
-e "s@^#baseurl=@baseurl=@g" \
-e "s@http://mirror.centos.org@http://192.168.239.241@g" \
-i /etc/yum.repos.d/*.repo > /dev/null 2>&1
if [ $? -eq 0 ];then
echo -n "已经配置源为192.168.239.241!"
fi
#更新系统
yum update -y
#配置selinux
sed -i '/^SELINUX=/s/=.*/=disabled/' /etc/selinux/config
setenforce 0
if [ $? -eq 0 ];then
echo "SELinux初始化完毕!"
fi
#配置防火墙
firewall-cmd --add-port={3306,4567,4568,4444}/tcp --permanent
firewall-cmd --reload
if [ $? -eq 0 ];then
echo "Iptables防火墙初始化完毕!"
fi
##配置主机名及hosts
#echo `ip add show ens33 |grep 'inet '|awk '{print $2}'|awk -F / '{print $1}'` pxc1 >>/etc/hosts
echo 172.25.0.131 pxc1 >>/etc/hosts
echo 172.25.0.132 pxc2 >>/etc/hosts
echo 172.25.0.133 pxc3 >>/etc/hosts
hostnamectl set-hostname pxc2
#安装Percona-XtraDB-Cluster
tar xvf Percona-XtraDB-Cluster-5.7.22-29.26-r422-el7-x86_64-bundle.tar
yum install http://www.percona.com/downloads/percona-release/redhat/0.1-6/percona-release-0.1-6.noarch.rpm -y
yum localinstall Percona-XtraDB-Cluster*.rpm -y
if [ $? -eq 0 ];then
echo "PXC安装包安装完成!"
fi
chkconfig mysql off
systemctl start mysql
mysql -u root -p`grep 'temporary password' /var/log/mysqld.log|awk '{print $11}'|tail -n1 ` --connect-expired-password -e "ALTER USER 'root'@'localhost' IDENTIFIED BY 'rootPass';flush privileges;"
if [ $? -eq 0 ];then
echo "设置mysql的root密码为'rootPass'!"
fi
systemctl stop mysql
##停止mysql服务,配置wsrep
##下面三行跟据实际填写
##wsrep_cluster_address=gcomm://x.x.x.x,x.x.x.x,
##wsrep_node_name=xxx ##
##wsrep_node_address=x.x.x.x##
##下面是我的配置
mv /etc/percona-xtradb-cluster.conf.d/wsrep.cnf /etc/percona-xtradb-cluster.conf.d/wsrep.cnf.bak
cat << EOF >/etc/percona-xtradb-cluster.conf.d/wsrep.cnf
[mysqld]
wsrep_provider=/usr/lib64/galera3/libgalera_smm.so
wsrep_cluster_name=pxc-cluster
wsrep_cluster_address=gcomm://172.25.0.131,172.25.0.132,172.25.0.133
wsrep_node_name=pxc2
wsrep_node_address=172.25.0.132
wsrep_sst_method=xtrabackup-v2
wsrep_sst_auth=sstuser:passw0rd
pxc_strict_mode=ENFORCING
binlog_format=ROW
default_storage_engine=InnoDB
innodb_autoinc_lock_mode=2
EOF
#After you configure all PXC nodes
#systemctl start mysql
#show status like 'wsrep%';
pxc3.txt代码
#!/bin/bash
yum remove mariadb-libs -y
echo "正在配置源为192.168.239.241……"
sed -e "s/^metalink=/#metalink=/g" \
-e "s/^mirrorlist=http/#mirrorlist=http/g" \
-e "s@^#baseurl=@baseurl=@g" \
-e "s@http://mirror.centos.org@http://192.168.239.241@g" \
-i /etc/yum.repos.d/*.repo > /dev/null 2>&1
if [ $? -eq 0 ];then
echo -n "已经配置源为192.168.239.241!"
fi
#更新系统
yum update -y
#配置selinux
sed -i '/^SELINUX=/s/=.*/=disabled/' /etc/selinux/config
setenforce 0
if [ $? -eq 0 ];then
echo "SELinux初始化完毕!"
fi
#配置防火墙
firewall-cmd --add-port={3306,4567,4568,4444}/tcp --permanent
firewall-cmd --reload
if [ $? -eq 0 ];then
echo "Iptables防火墙初始化完毕!"
fi
##配置主机名及hosts
#echo `ip add show ens33 |grep 'inet '|awk '{print $2}'|awk -F / '{print $1}'` pxc1 >>/etc/hosts
echo 172.25.0.131 pxc1 >>/etc/hosts
echo 172.25.0.132 pxc2 >>/etc/hosts
echo 172.25.0.133 pxc3 >>/etc/hosts
hostnamectl set-hostname pxc1
#安装Percona-XtraDB-Cluster
tar xvf Percona-XtraDB-Cluster-5.7.22-29.26-r422-el7-x86_64-bundle.tar
yum install http://www.percona.com/downloads/percona-release/redhat/0.1-6/percona-release-0.1-6.noarch.rpm -y
yum localinstall Percona-XtraDB-Cluster*.rpm -y
if [ $? -eq 0 ];then
echo "PXC安装包安装完成!"
fi
chkconfig mysql off
systemctl start mysql
mysql -u root -p`grep 'temporary password' /var/log/mysqld.log|awk '{print $11}'|tail -n1 ` --connect-expired-password -e "ALTER USER 'root'@'localhost' IDENTIFIED BY 'rootPass';flush privileges;"
if [ $? -eq 0 ];then
echo "设置mysql的root密码为'rootPass'!"
fi
systemctl stop mysql
##停止mysql服务,配置wsrep
##下面三行跟据实际填写
##wsrep_cluster_address=gcomm://x.x.x.x,x.x.x.x,
##wsrep_node_name=xxx ##
##wsrep_node_address=x.x.x.x##
##下面是我的配置
mv /etc/percona-xtradb-cluster.conf.d/wsrep.cnf /etc/percona-xtradb-cluster.conf.d/wsrep.cnf.bak
cat << EOF >/etc/percona-xtradb-cluster.conf.d/wsrep.cnf
[mysqld]
wsrep_provider=/usr/lib64/galera3/libgalera_smm.so
wsrep_cluster_name=pxc-cluster
wsrep_cluster_address=gcomm://172.25.0.131,172.25.0.132,172.25.0.133
wsrep_node_name=pxc3
wsrep_node_address=172.25.0.133
wsrep_sst_method=xtrabackup-v2
wsrep_sst_auth=sstuser:passw0rd
pxc_strict_mode=ENFORCING
binlog_format=ROW
default_storage_engine=InnoDB
innodb_autoinc_lock_mode=2
EOF
#After you configure all PXC nodes
#systemctl start mysql
#show status like 'wsrep%';