1.PXC簡介
PXC全稱是Percona XtraDB Cluster, 是著名的mysql公司Percona出品的免費的數據庫集群產品。
PXC對數據庫集群是基於Galera的面向OLTP的多主同步復制插件。
長期以來mysql都缺少官方原生的集群方案,這也給很多第三方公司提供了機會,所以Galera技術就是眾多mysql集群技術中較為著名的一種,mysql自帶的主從復制模式天生就不能保證數據同步的完整一致,所以很多大公司花了很多人力和物力去解決這個問題,但是收效甚微,幸運的是Galera帶來了多主架構,同步復制,並發復制,故障切換等一系列的功能。這才是真正意義上保證了數據的一致性,正確性和完整性。
PXC主要用於解決MySQL集群中數據同步強一致性的問題。
PXC是MySQL集群方案中公認的優先方案之一
1.1 PXC的特點
同步復制,事務在所有集群節點提交成功,要么不成功。
多主復制,可以在任意一個節點寫入
數據同步的強一致性,所有節點數據保持一致
1.2 盡可能的控制PXC集群的規模
PXC集群節點越多,數據同步的速度就越慢
1.3 所有PXC節點的硬件配置要相同
PXC集群數據同步的速度取決於配置最低的節點。因為PXC是同步復制,一個節點寫入的數據的速度慢,會拖慢整個集群的寫入速度。
1.4 PXC集群只支持InnoDB引擎
只有InnoDB的數據才會被同步
2.准備虛擬機
官網推薦至少3個節點
A cluster consists of nodes, where each node contains the same set of data synchronized accross nodes. The recommended configuration is to have at least 3 nodes, but you can have 2 nodes as well.
所以我們要實現的是三台mysql節點集群,所以准備3台虛擬機
192.168.109.101 mysql01
192.168.109.102 mysql02
192.168.109.103 mysql03
3.下載PXC資源包
官網:https://www.percona.com/downloads/Percona-XtraDB-Cluster-LATEST/
下載xtabackup,地址 https://www.percona.com/downloads/Percona-XtraBackup-2.4/LATEST/
可以直接下載tar包解壓,也可以下載下方對應的rpm包,這里直接下載兩個tar包。
還需要下載qpress-11-1.el7.x86_64.prm
wget https://repo.percona.com/release/centos/6Server/RPMS/x86_64/qpress-11-1.el6.x86_64.rpm
4.安裝PXC
4.1 PXC集群要使用4個端口,開放4個端口
直接關閉iptables
service iptables stop
chkconfig iptables off
4.2 關閉selinux
setenforce 0
vim /etc/sysconfig/selinux
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these two values:
# targeted - Targeted processes are protected,
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
4.3 解壓2個tar包 把相關rpm包放在一起
[root@mysql01 percona]# pwd
/softwares/percona
[root@mysql01 percona]# ll
total 314696
-rw-r--r--. 1 root root 74692 Dec 16 13:10 libev-4.15-1.el6.rf.x86_64.rpm
-rw-rw-r--. 1 root root 8521676 Dec 4 17:13 percona-xtrabackup-24-2.4.17-1.el6.x86_64.rpm
-rw-rw-r--. 1 root root 54076376 Dec 4 17:13 percona-xtrabackup-24-debuginfo-2.4.17-1.el6.x86_64.rpm
-rw-rw-r--. 1 root root 17098980 Dec 4 17:13 percona-xtrabackup-test-24-2.4.17-1.el6.x86_64.rpm
-rw-rw-r--. 1 root root 28384 Sep 16 14:44 Percona-XtraDB-Cluster-57-5.7.27-31.39.1.el6.x86_64.rpm
-rw-rw-r--. 1 root root 142296916 Sep 16 14:44 Percona-XtraDB-Cluster-57-debuginfo-5.7.27-31.39.1.el6.x86_64.rpm
-rw-rw-r--. 1 root root 8499424 Sep 16 14:44 Percona-XtraDB-Cluster-client-57-5.7.27-31.39.1.el6.x86_64.rpm
-rw-rw-r--. 1 root root 1222100 Sep 16 14:44 Percona-XtraDB-Cluster-devel-57-5.7.27-31.39.1.el6.x86_64.rpm
-rw-rw-r--. 1 root root 27892 Sep 16 14:44 Percona-XtraDB-Cluster-full-57-5.7.27-31.39.1.el6.x86_64.rpm
-rw-rw-r--. 1 root root 624680 Sep 16 14:44 Percona-XtraDB-Cluster-garbd-57-5.7.27-31.39.1.el6.x86_64.rpm
-rw-rw-r--. 1 root root 55747332 Sep 16 14:44 Percona-XtraDB-Cluster-server-57-5.7.27-31.39.1.el6.x86_64.rpm
-rw-rw-r--. 1 root root 902256 Sep 16 14:44 Percona-XtraDB-Cluster-shared-57-5.7.27-31.39.1.el6.x86_64.rpm
-rw-rw-r--. 1 root root 32717112 Sep 16 14:44 Percona-XtraDB-Cluster-test-57-5.7.27-31.39.1.el6.x86_64.rpm
-rw-r--r--. 1 root root 31512 Mar 19 2019 qpress-11-1.el6.x86_64.rpm
-rw-r--r--. 1 root root 348020 Dec 16 13:13 socat-1.7.2.4-1.el6.rf.x86_64.rpm
安裝rpm
yum localinstall *.rpm
centos 6安裝pxc cluster會有報錯,centos7 沒有問題,需要解決。
Error: Package: percona-xtrabackup-24-2.4.11-1.el6.x86_64 (percona-release-x86_64)
Requires: libev.so.4()(64bit)
Error: Package: Percona-XtraDB-Cluster-server-57-5.7.21-29.26.1.el6.x86_64 (percona-release-x86_64)
Requires: socat
You could try using --skip-broken to work around the problem
You could try running: rpm -Va --nofiles --nodigest
因為percona-xtrabackup依賴libev.so.4()(64bit)的包,而這個包只能從EPEL源(Extra Packages for Enterprise Linux,企業版Linux的額外軟件包 是Fedora小組維護的一個軟件倉庫項目,為RHEL/CentOS提供他們默認不提供的軟件包)安裝 ,所以只要安裝了這個包即可解決問題。
從http://rpmfind.net/linux/rpm2html/search.php上下載libev-4.15-1.el6.rf.x86_64.rpm安裝包,然后安裝即可
rpm -ivh libev-4.15-1.el6.rf.x86_64.rpm
warning: libev-4.15-1.el6.rf.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 6b8d79e6: NOKEY
Preparing... ########################################### [100%]
1:libev ########################################### [100%]
從http://rpmfind.net/linux/rpm2html/search.php上下載socat-1.7.2.3-1.el6.x86_64.rpm安裝包,這個包有其它的依賴性關系,最簡單的方法是用yun localinstall方法安裝
yum localinstall socat-1.7.2.3-1.el6.x86_64.rpm
再執行就成功安裝了。
yum localinstall *.rpm
5.配置PXC
5.1 進入PXC配置文件目錄
[root@mysql01 percona-xtradb-cluster.conf.d]# pwd
/etc/percona-xtradb-cluster.conf.d
[root@mysql01 percona-xtradb-cluster.conf.d]# ll
total 12
-rw-r--r--. 1 root root 381 Sep 16 18:41 mysqld.cnf
-rw-r--r--. 1 root root 440 Sep 16 18:41 mysqld_safe.cnf
-rw-r--r--. 1 root root 1066 Sep 16 18:41 wsrep.cnf
[root@mysql01 percona-xtradb-cluster.conf.d]#
mysql的常用信息都寫在了mysqld.cnf文件,wsrep.cnf文件配置的是pxc集群的信息
我們可以簡化一下配置文件,將mysqld.cnf文件和wsrep.cnf文件的內容復制到/etc/my.cnf文件中,把所有配置信息寫到一個文件中,並添加字符集等配置。
[root@mysql01 percona-xtradb-cluster.conf.d]# more /etc/my.cnf
# Template my.cnf for PXC
# Edit to your requirements.
[client]
socket=/data/mysql/mysql.sock
[mysqld]
server-id=1
datadir=/data/mysql
socket=/data/mysql/mysql.sock
log-error=/data/mysqllog/mysqld.log
pid-file=/data/mysql/mysqld.pid
log-bin
log_slave_updates
expire_logs_days=7
character_set_server = utf8
skip-name-resolve
sync_binlog=1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# WSREP CONF #
# Path to Galera library
wsrep_provider=/usr/lib64/galera3/libgalera_smm.so
# Cluster connection URL contains IPs of nodes
#If no IP is found, this implies that a new cluster needs to be created,
#in order to do that you need to bootstrap this node
wsrep_cluster_address=gcomm://192.168.109.101,192.168.109.102,192.168.109.103
# In order for Galera to work correctly binlog format should be ROW
binlog_format=ROW
# MyISAM storage engine has only experimental support
default_storage_engine=InnoDB
# This changes how InnoDB autoincrement locks are managed and is a requirement for Galera
innodb_autoinc_lock_mode=2
# Cluster name
wsrep_cluster_name=pxc-cluster
#If wsrep_node_name is not specified, then system hostname will be used
wsrep_node_name=pxc1
# Node IP address
wsrep_node_address=192.168.109.101
#pxc_strict_mode allowed values: DISABLED,PERMISSIVE,ENFORCING,MASTER
pxc_strict_mode=ENFORCING
# SST method
wsrep_sst_method=xtrabackup-v2
#Authentication for SST method
wsrep_sst_auth= "sstuser:s3cret"
差異化配置:
# 需要根據相應的節點配置
server-id=2
wsrep_node_name=pxc2
wsrep_node_address=192.168.109.102
配置數據目錄:
mdkir -p /data/mysql
5.2 啟動
啟動第一個節點,第一個啟動的PXC節點是主節點
/etc/init.d/mysql bootstrap-pxc
查看root密碼
# root初始化密碼被記錄到log-error中
grep 'temporary password' /data/mysqllog/mysqld.log
修改root密碼,並配置SST認證賬號
mysql> CREATE USER 'sstuser'@'localhost' IDENTIFIED BY 's3cret';
mysql> GRANT RELOAD, LOCK TABLES, PROCESS, REPLICATION CLIENT ON *.* TO 'sstuser'@'localhost';
mysql> FLUSH PRIVILEGES;
啟動其他節點,非第一個啟動的PXC節點
service mysql start
各個節點都采用第一個幾點修改的root密碼登錄。
5.3 節點的啟動和關閉
- 如果最后關閉的PXC節點是安全退出的,那么下次啟動要最先啟動這個節點,而且要以主節點啟動
- 如果最后關閉的PXC節點不是安全退出的,那么要先修改
/var/lib/mysql/grastate.dat
文件,把其中的safe_to_bootstrap
屬性值設置為1,再安照主節點啟動。
5.4節點的安全關閉操作
/etc/init.d/mysql bootstrap.service stop
service mysql stop
關閉掉主節點之后查看/var/lib/mysql/grastate.dat文件
5.5 PXC節點的不安全或意外關閉操作
宕機,掛起,關機,重啟,斷電,斷網都會讓節點意外下線。
意外下線部分節點
安全下線節點不會讓剩下的節點宕機,如果節點意外退出,集群的規模不會縮小,意外退出的節點超過半數,比如三個節點意外退出了2個節點,那么剩下的節點就不能夠讀寫了。其他節點按照普通節點啟動上線即可恢復pxc集群。
service mysql start
意外下線全部節點,不同時退出
如果三個節點都意外退出,那么查看/var/lib/mysql/grastate.dat文件,看看哪個文件的safe_to_bootstarp的值是1,那么那個節點是最后意外關閉的,再按照safe_to_bootstarp的值啟動pxc集群
意外下線全部節點,同時退出
如果三個節點同時意外退出,我們需要修改配置文件,挑選一個節點作為主節點,修改safe_to_bootstarp的值設置為1,那么這個節點可以以主節點啟動
5.6 查看節點狀態
查看節點數:
show global status like 'wsrep_cluster_size';
查看集群狀態:
show global status like 'wsrep%';
查看當前節點狀態:
show global status like 'wsrep_cluster_status';
6.一些參數說明
- wsrep_provider:指定Galera庫路徑,沒有這個庫,節點只是一個MySQL單實例,無法參與pxc集群
- Debian or Ubuntu: /usr/lib/libgalera_smm.so
- Red Hat or CentOS: /usr/lib64/galera3/libgalera_smm.so
-
wsrep_cluster_name:集群名稱,所有節點相同
-
wsrep_cluster_address:集群所有節點IP地址列表
-
wsrep_node_name:節點名稱,默認為主機名稱
-
wsrep_node_address:節點IP地址
- wsrep_sst_method:指定SST方法,推薦wsrep_sst_method=xtrabackup-v2
- wsrep_sst_auth:SST認證賬號密碼
- <sst_user>:<sst_pass>
- 當第一個節點啟動后手動創建該賬號密碼,並授予相應的權限
-
pxc_strict_mode:PXC嚴格控制模式,
一些名詞解釋
-
WS:write set寫數據集,寫/更新事務
-
IST:Incremental State Transfer 增量同步
- SST:State Snapshot Transfe 全量同步。
- SST支持的方法有:mysqldump,rsync ,xtrabackup
- mysqldump,rsync同步需要READ LOCK, (SST applies FLUSH TABLES WITH READ LOCK command)
- xtrabackup 在整個同步數據過程中不需要READ LOCK
- 配置參數:wsrep_sst_method=xtrabackup-v2
-
UUID:節點狀態改變及順序的唯一標識
-
GTID:Global Transaction ID,由UUID和sequence number偏移量組成。wsrep api中定義的集群內部全局事務id,用於記錄集群中發生狀態改變的唯一標識以及隊列中的偏移量。
-
WSRWP API:在DBMS庫和wsrep provider之間提供接口