利用官方yum源安裝Percona-XtraDB-Cluster-57html
1.當前OS環境:node
[root@qht131 local]# more /etc/redhat-release
Red Hat Enterprise Linux Server release 6.3 (Santiago)
配置hosts文件mysql
[root@qht131 local]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.17.61.131 qht131
172.17.61.132 qht132
172.17.61.133 qht133
修改selinux配置文件linux
[root@qht131 local]# cat /etc/selinux/config
# 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
關閉防火牆sql
[root@qht131 local]# chkconfig iptables off
或者打開這幾個端口也能夠:數據庫
3306 數據庫對外提供服務的端口
4444 鏡像數據傳輸SST,集羣數據同步端口,全量同步,新節點加入時起做用
4567 集羣節點間相互通訊的端口
4568 增量數據同步IST,節點下線、重啓後使用該端口,增量同步數據。bootstrap
2.準備安裝centos
配置percona的yum文件緩存
[root@qht131 ~]# cd /etc/yum.repos.d/
[root@qht131 yum.repos.d]# sudo yum install http://www.percona.com/downloads/percona-release/redhat/0.1-4/percona-release-0.1-4.noarch.rpm
Installed:
percona-release.noarch 0:0.1-4
Complete!
測試一下yum,dom
[root@qht131 yum.repos.d]# sudo yum list | grep percona
Unable to read consumer identity
percona-release.noarch 0.1-4 @/percona-release-0.1-4.noarch
Percona-SQL-50-debuginfo.x86_64 5.0.92-b23.89.rhel6 percona-release-x86_64
Percona-SQL-client-50.x86_64 5.0.92-b23.89.rhel6 percona-release-x86_64
Percona-SQL-devel-50.x86_64 5.0.92-b23.89.rhel6 percona-release-x86_64
Percona-SQL-server-50.x86_64 5.0.92-b23.89.rhel6 percona-release-x86_64
Percona-SQL-shared-50.x86_64 5.0.92-b23.89.rhel6 percona-release-x86_64
Percona-SQL-shared-compat.x86_64 5.0.92-b23.89.rhel6 percona-release-x86_64
Percona-SQL-test-50.x86_64 5.0.92-b23.89.rhel6 percona-release-x86_64
。。。
3.安裝Percona XtraDB Cluster5.7
[root@qht131 ~]# sudo yum install Percona-XtraDB-Cluster-57
正常安裝完成後應該是這樣:
Installed:
Percona-XtraDB-Cluster-57.x86_64 0:5.7.21-29.26.1.el6
Dependency Installed:
Percona-XtraDB-Cluster-client-57.x86_64 0:5.7.21-29.26.1.el6 Percona-XtraDB-Cluster-server-57.x86_64 0:5.7.21-29.26.1.el6
Percona-XtraDB-Cluster-shared-57.x86_64 0:5.7.21-29.26.1.el6 percona-xtrabackup-24.x86_64 0:2.4.11-1.el6
perl-DBD-MySQL.x86_64 0:4.013-3.el6 qpress.x86_64 0:11-1.el6
Dependency Updated:
zlib.x86_64 0:1.2.3-29.el6
Complete!
更改root密碼:
mysql沒有啓動的話須要手工啓動一下,我是reboot系統後纔可能正常啓動的,還不知道是爲何
[root@qht131 ~]# service mysql status
MySQL (Percona XtraDB Cluster) running (1974) [ OK ]
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.04 sec)
mysql> exit
Bye
[root@qht131 ~]# service mysql stop
Shutting down MySQL (Percona XtraDB Cluster)...............[ OK ]
因爲我是虛擬機測試的,這時我把qht131複製成qht132,qht133,更改IP後啓動,特別注意須要更改mysql的server-uuid.
位置在:
[root@qht131 mysql]# cat /var/lib/mysql/auto.cnf
[auto]
server-uuid=b6315eab-6f16-11e8-84e3-000c29b55066
先要確保三臺mysql是關閉的狀態
[root@qht131 ~]# service mysql stop
Shutting down MySQL (Percona XtraDB Cluster)...............[ OK ]
接着修改/etc/my.cnf
[root@qht131 ~]# grep "^[^#]" /etc/my.cnf
[mysqld] --這個必須加上,默認是沒有的
!includedir /etc/my.cnf.d/
!includedir /etc/percona-xtradb-cluster.conf.d/
wsrep_provider=/usr/lib64/galera3/libgalera_smm.so
wsrep_cluster_name=pxc-cluster
wsrep_cluster_address=gcomm://172.17.61.131,172.17.61.132,172.17.61.133
wsrep_node_name=pxc1
wsrep_node_address=172.17.61.131
wsrep_sst_method=xtrabackup-v2
wsrep_sst_auth=sstuser:sstuser
pxc_strict_mode=ENFORCING
binlog_format=ROW
default_storage_engine=InnoDB
innodb_autoinc_lock_mode=2
qht132和qht133只須要改動wsrep_node_address以及wsrep_node_nam,其它的參數和qht132一致。
接着開啓第一個節點:(centos7的啓動方法是[root@pxc1 ~]# systemctl start mysql@bootstrap.service)
[root@qht131 ~]# /etc/init.d/mysql bootstrap-pxc
查看一下關於Write-Set Replication的參數 :
mysql> show status like 'wsrep%';
+----------------------------------+-----------------------------------------------+
| Variable_name | Value |
+----------------------------------+-----------------------------------------------+
| wsrep_local_state_uuid | a084e15e-6f18-11e8-9dc4-efc0d77a254c |
|。。。
| wsrep_local_state | 4 |
| wsrep_local_state_comment | Synced |
。。。
| wsrep_incoming_addresses | 172.17.61.131:3306 |
。。。
| wsrep_cluster_size | 1 |
| wsrep_cluster_state_uuid | a084e15e-6f18-11e8-9dc4-efc0d77a254c |
| wsrep_cluster_status | Primary |
| wsrep_connected | ON |
。。。
| wsrep_ready | ON |
+----------------------------------+-----------------------------------------------+
68 rows in set (0.00 sec)
表示當前的cluster_size爲1,是Primary的組件,當前的狀態是synced,wsrep_ready是開啓的狀態
當前的監聽端口3306和4567都已打開
[root@qht131 ~]# ss -ntl
Recv-Q Send-Q Local Address:Port Peer Address:Port
0 128 :::43753 :::*
0 80 :::3306 :::*
0 128 :::111 :::*
0 128 *:111 *:*
0 64 :::9200 :::*
0 128 :::22 :::*
0 128 *:22 *:*
0 128 *:4567 *:*
0 128 127.0.0.1:631 *:*
0 128 ::1:631 :::*
0 100 ::1:25 :::*
0 100 127.0.0.1:25 *:*
0 128 *:52412 *:*
在初始化啓動節點1的mysql後,須要建立用於節點直接同步數據的帳戶。
在新節加入到集羣以前,在第一個節點須要創建sst用戶,賬戶名和密碼是wsrep_sst_auth裏面設置的一致。
默認狀況下percona xtrDB Cluster是用Xtrabackup來作SST(State Snapshot Transfer ),因此須要創建一個用戶並給予相應的權限。
mysql> CREATE USER 'sstuser'@'localhost' IDENTIFIED BY 'sstuser';
Query OK, 0 rows affected (0.03 sec)
mysql> GRANT RELOAD, LOCK TABLES, PROCESS, REPLICATION CLIENT ON *.* TO 'sstuser'@'localhost';
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
下一步就是開始其它的2個別點,加入到cluster中。
[root@qht132 ~]# /etc/init.d/mysql start
Starting MySQL (Percona XtraDB Cluster)....State transfer in progress, setting sleep higher
. [ OK ]
[root@qht133 ~]# /etc/init.d/mysql start
Starting MySQL (Percona XtraDB Cluster)...State transfer in progress, setting sleep higher
. [ OK ]
三個節都起來後,查看一下wsrep的相關參數 :
mysql> show status like '%wsrep_cluster%';
+--------------------------+--------------------------------------+
| Variable_name | Value |
+--------------------------+--------------------------------------+
| wsrep_cluster_conf_id | 3 |
| wsrep_cluster_size | 3 |
| wsrep_cluster_state_uuid | a084e15e-6f18-11e8-9dc4-efc0d77a254c |
| wsrep_cluster_status | Primary |
+--------------------------+--------------------------------------+
4 rows in set (0.00 sec)
mysql> show status like 'wsrep_connected';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| wsrep_connected | ON |
+-----------------+-------+
1 row in set (0.00 sec)
5.驗證集羣:
在qht132中創建些測試數據:
mysql> CREATE DATABASE percona;
Query OK, 1 row affected (0.03 sec)
mysql> USE percona;
Database changed
mysql> CREATE TABLE example (node_id INT PRIMARY KEY, node_name VARCHAR(30));
Query OK, 0 rows affected (0.04 sec)
mysql> INSERT INTO percona.example VALUES (1, 'percona1');
Query OK, 1 row affected (0.07 sec)
mysql> SELECT * FROM percona.example;
+---------+-----------+
| node_id | node_name |
+---------+-----------+
| 1 | percona1 |
+---------+-----------+
1 row in set (0.00 sec)
qht133中查看新數據:
mysql> SELECT * FROM percona.example;
+---------+-----------+
| node_id | node_name |
+---------+-----------+
| 1 | percona1 |
+---------+-----------+
1 row in set (0.00 sec)
mysql> INSERT INTO percona.example VALUES (2, 'percona1');
Query OK, 1 row affected (0.04 sec)
qht131中查看數據:
mysql> SELECT * FROM percona.example;
+---------+-----------+
| node_id | node_name |
+---------+-----------+
| 1 | percona1 |
| 2 | percona1 |
+---------+-----------+
2 rows in set (0.00 sec)
PXC集羣內的全部節點都可寫入並同步複製到其餘節點。
接着測試若是一個節點掛掉後再從新加入到cluster會不會同步數據。
將qht132的mysql停掉
qht132:
[root@qht132 ~]# /etc/init.d/mysql stop
Shutting down MySQL (Percona XtraDB Cluster).............. [ OK ]
qht131:
mysql> show status like '%wsrep_clust%';
+--------------------------+--------------------------------------+
| Variable_name | Value |
+--------------------------+--------------------------------------+
| wsrep_cluster_conf_id | 4 |
| wsrep_cluster_size | 2 |
| wsrep_cluster_state_uuid | a084e15e-6f18-11e8-9dc4-efc0d77a254c |
| wsrep_cluster_status | Primary |
+--------------------------+--------------------------------------+
4 rows in set (0.00 sec)
當前的cluster_size爲2,說明只有2個節點是存活的。
mysql> INSERT INTO percona.example VALUES (3, 'percona1');
Query OK, 1 row affected (0.00 sec)
qht132:
[root@qht132 ~]# /etc/init.d/mysql start
Starting MySQL (Percona XtraDB Cluster).... [ OK ]
mysql> SELECT * FROM percona.example;
+---------+-----------+
| node_id | node_name |
+---------+-----------+
| 1 | percona1 |
| 2 | percona1 |
| 3 | percona1 |
+---------+-----------+
3 rows in set (0.00 sec)
沒有問題,qht132從新加入到cluster後,數據也同步了過來。
通過測試,3個節點中任何一個節點掛掉後從新再加入cluster都能正常的同步數據,包括qht131。
因而可知,當集羣內的某個節點掉線後,其餘節點仍能夠正常的工做,新寫入的數據會在該節點從新上線後完成同步,以實現PXC的高可用。
針對從新上線的節點,PXC有兩種方式完成數據傳輸以保證數據同步:State Snapshot Transfer (SST)和Incremental State Transfer (IST)。
SST一般用在當有新的節點加入PXC集羣同時從已存在節點複製所有數據時採用,在PXC中有三種可用的方式完成SST過程:
– mysqldump
– rsync
– xtrabackup
mysqldump和rsync的缺點是在數據傳輸過程當中,PXC集羣將會變成只讀模式,SST將會對數據庫施加只讀鎖(FLUSH TABLES WITH READ LOCK)。而使用xtrabackup則不須要再數據同步過程當中施加讀鎖,僅僅是同步.frm文件,相似於常規的備份。
IST用於只將數據的增量變化從一個節點複製到另外一個節點。
參考:
https://www.percona.com/doc/percona-xtradb-cluster/5.7/install/index.html
https://blog.csdn.net/geoffreychan/article/details/80499956