基於rhel6 安裝Percona XtraDB Cluster(PXC) 5.7

基於rhel6 安裝Percona XtraDB Cluster(PXC) 5.7

2018年06月13日 17:12:45 zuozhiji 閱讀數 171
 

利用官方yum源安裝Percona-XtraDB-Cluster-57html

1.當前OS環境:node

 
  1.  
    [root@qht131 local]# more /etc/redhat-release
  2.  
    Red Hat Enterprise Linux Server release 6.3 (Santiago)

配置hosts文件mysql

 
  1.  
    [root@qht131 local]# cat /etc/hosts
  2.  
    127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
  3.  
    ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
  4.  
    172.17.61.131 qht131
  5.  
    172.17.61.132 qht132
  6.  
    172.17.61.133 qht133

修改selinux配置文件linux

 
  1.  
    [root@qht131 local]# cat /etc/selinux/config
  2.  
     
  3.  
    # This file controls the state of SELinux on the system.
  4.  
    # SELINUX= can take one of these three values:
  5.  
    # enforcing - SELinux security policy is enforced.
  6.  
    # permissive - SELinux prints warnings instead of enforcing.
  7.  
    # disabled - No SELinux policy is loaded.
  8.  
    SELINUX=disabled
  9.  
    # SELINUXTYPE= can take one of these two values:
  10.  
    # targeted - Targeted processes are protected,
  11.  
    # mls - Multi Level Security protection.
  12.  
    SELINUXTYPE=targeted

關閉防火牆sql

[root@qht131 local]# chkconfig iptables off

或者打開這幾個端口也能夠:數據庫

3306    數據庫對外提供服務的端口
4444    鏡像數據傳輸SST,集羣數據同步端口,全量同步,新節點加入時起做用
4567    集羣節點間相互通訊的端口
4568    增量數據同步IST,節點下線、重啓後使用該端口,增量同步數據。bootstrap

2.準備安裝centos

配置percona的yum文件緩存

 
  1.  
    [root@qht131 ~]# cd /etc/yum.repos.d/
  2.  
    [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
 
  1.  
    Installed:
  2.  
    percona-release.noarch 0:0.1-4
  3.  
     
  4.  
    Complete!

測試一下yum,dom

 
  1.  
    [root@qht131 yum.repos.d]# sudo yum list | grep percona
  2.  
    Unable to read consumer identity
  3.  
    percona-release.noarch 0.1-4 @/percona-release-0.1-4.noarch
  4.  
    Percona-SQL-50-debuginfo.x86_64 5.0.92-b23.89.rhel6 percona-release-x86_64
  5.  
    Percona-SQL-client-50.x86_64 5.0.92-b23.89.rhel6 percona-release-x86_64
  6.  
    Percona-SQL-devel-50.x86_64 5.0.92-b23.89.rhel6 percona-release-x86_64
  7.  
    Percona-SQL-server-50.x86_64 5.0.92-b23.89.rhel6 percona-release-x86_64
  8.  
    Percona-SQL-shared-50.x86_64 5.0.92-b23.89.rhel6 percona-release-x86_64
  9.  
    Percona-SQL-shared-compat.x86_64 5.0.92-b23.89.rhel6 percona-release-x86_64
  10.  
    Percona-SQL-test-50.x86_64 5.0.92-b23.89.rhel6 percona-release-x86_64
  11.  
    。。。

3.安裝Percona XtraDB Cluster5.7

[root@qht131 ~]# sudo yum install Percona-XtraDB-Cluster-57

安裝時出現依賴包的錯誤的解決方法

 
  1.  
    正常安裝完成後應該是這樣:
  2.  
    Installed:
  3.  
    Percona-XtraDB-Cluster-57.x86_64 0:5.7.21-29.26.1.el6
  4.  
     
  5.  
    Dependency Installed:
  6.  
    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
  7.  
    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
  8.  
    perl-DBD-MySQL.x86_64 0:4.013-3.el6 qpress.x86_64 0:11-1.el6
  9.  
     
  10.  
    Dependency Updated:
  11.  
    zlib.x86_64 0:1.2.3-29.el6
  12.  
     
  13.  
    Complete!

更改root密碼:

mysql沒有啓動的話須要手工啓動一下,我是reboot系統後纔可能正常啓動的,還不知道是爲何

 
  1.  
    [root@qht131 ~]# service mysql status
  2.  
    MySQL (Percona XtraDB Cluster) running (1974) [ OK ]
 
  1.  
    mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
  2.  
    Query OK, 0 rows affected (0.04 sec)
  3.  
     
  4.  
    mysql> exit
  5.  
    Bye
 
  1.  
    [root@qht131 ~]# service mysql stop
  2.  
    Shutting down MySQL (Percona XtraDB Cluster)...............[ OK ]

因爲我是虛擬機測試的,這時我把qht131複製成qht132,qht133,更改IP後啓動,特別注意須要更改mysql的server-uuid.

位置在:

 
  1.  
    [root@qht131 mysql]# cat /var/lib/mysql/auto.cnf
  2.  
    [auto]
  3.  
    server-uuid=b6315eab-6f16-11e8-84e3-000c29b55066
4.修改Write-Set Replication
 

先要確保三臺mysql是關閉的狀態

 
  1.  
    [root@qht131 ~]# service mysql stop
  2.  
    Shutting down MySQL (Percona XtraDB Cluster)...............[ OK ]

接着修改/etc/my.cnf

 
  1.  
    [root@qht131 ~]# grep "^[^#]" /etc/my.cnf
  2.  
    [mysqld] --這個必須加上,默認是沒有的
  3.  
    !includedir /etc/my.cnf.d/
  4.  
    !includedir /etc/percona-xtradb-cluster.conf.d/
  5.  
    wsrep_provider=/usr/lib64/galera3/libgalera_smm.so
  6.  
    wsrep_cluster_name=pxc-cluster
  7.  
    wsrep_cluster_address=gcomm://172.17.61.131,172.17.61.132,172.17.61.133
  8.  
    wsrep_node_name=pxc1
  9.  
    wsrep_node_address=172.17.61.131
  10.  
    wsrep_sst_method=xtrabackup-v2
  11.  
    wsrep_sst_auth=sstuser:sstuser
  12.  
    pxc_strict_mode=ENFORCING
  13.  
    binlog_format=ROW
  14.  
    default_storage_engine=InnoDB
  15.  
    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的參數 :

 
  1.  
    mysql> show status like 'wsrep%';
  2.  
    +----------------------------------+-----------------------------------------------+
  3.  
    | Variable_name | Value |
  4.  
    +----------------------------------+-----------------------------------------------+
  5.  
    | wsrep_local_state_uuid | a084e15e-6f18-11e8-9dc4-efc0d77a254c |
  6.  
    |。。。
  7.  
    | wsrep_local_state | 4 |
  8.  
    | wsrep_local_state_comment | Synced |
  9.  
    。。。
  10.  
    | wsrep_incoming_addresses | 172.17.61.131:3306 |
  11.  
    。。。
  12.  
    | wsrep_cluster_size | 1 |
  13.  
    | wsrep_cluster_state_uuid | a084e15e-6f18-11e8-9dc4-efc0d77a254c |
  14.  
    | wsrep_cluster_status | Primary |
  15.  
    | wsrep_connected | ON |
  16.  
    。。。
  17.  
    | wsrep_ready | ON |
  18.  
    +----------------------------------+-----------------------------------------------+
  19.  
    68 rows in set (0.00 sec)

表示當前的cluster_size爲1,是Primary的組件,當前的狀態是synced,wsrep_ready是開啓的狀態

當前的監聽端口3306和4567都已打開

 
  1.  
    [root@qht131 ~]# ss -ntl
  2.  
    Recv-Q Send-Q Local Address:Port Peer Address:Port
  3.  
    0 128 :::43753 :::*
  4.  
    0 80 :::3306 :::*
  5.  
    0 128 :::111 :::*
  6.  
    0 128 *:111 *:*
  7.  
    0 64 :::9200 :::*
  8.  
    0 128 :::22 :::*
  9.  
    0 128 *:22 *:*
  10.  
    0 128 *:4567 *:*
  11.  
    0 128 127.0.0.1:631 *:*
  12.  
    0 128 ::1:631 :::*
  13.  
    0 100 ::1:25 :::*
  14.  
    0 100 127.0.0.1:25 *:*
  15.  
    0 128 *:52412 *:*

在初始化啓動節點1的mysql後,須要建立用於節點直接同步數據的帳戶。

在新節加入到集羣以前,在第一個節點須要創建sst用戶,賬戶名和密碼是wsrep_sst_auth裏面設置的一致。

默認狀況下percona xtrDB Cluster是用Xtrabackup來作SST(State Snapshot Transfer ),因此須要創建一個用戶並給予相應的權限。

 
  1.  
    mysql> CREATE USER 'sstuser'@'localhost' IDENTIFIED BY 'sstuser';
  2.  
    Query OK, 0 rows affected (0.03 sec)
  3.  
     
  4.  
    mysql> GRANT RELOAD, LOCK TABLES, PROCESS, REPLICATION CLIENT ON *.* TO 'sstuser'@'localhost';
  5.  
    Query OK, 0 rows affected (0.00 sec)
  6.  
     
  7.  
    mysql> FLUSH PRIVILEGES;
  8.  
    Query OK, 0 rows affected (0.00 sec)

下一步就是開始其它的2個別點,加入到cluster中。

 
  1.  
    [root@qht132 ~]# /etc/init.d/mysql start
  2.  
    Starting MySQL (Percona XtraDB Cluster)....State transfer in progress, setting sleep higher
  3.  
    . [ OK ]
  4.  
     
  5.  
    [root@qht133 ~]# /etc/init.d/mysql start
  6.  
    Starting MySQL (Percona XtraDB Cluster)...State transfer in progress, setting sleep higher
  7.  
    . [ OK ]

三個節都起來後,查看一下wsrep的相關參數 :

 
  1.  
    mysql> show status like '%wsrep_cluster%';
  2.  
    +--------------------------+--------------------------------------+
  3.  
    | Variable_name | Value |
  4.  
    +--------------------------+--------------------------------------+
  5.  
    | wsrep_cluster_conf_id | 3 |
  6.  
    | wsrep_cluster_size | 3 |
  7.  
    | wsrep_cluster_state_uuid | a084e15e-6f18-11e8-9dc4-efc0d77a254c |
  8.  
    | wsrep_cluster_status | Primary |
  9.  
    +--------------------------+--------------------------------------+
  10.  
    4 rows in set (0.00 sec)
 
  1.  
    mysql> show status like 'wsrep_connected';
  2.  
    +-----------------+-------+
  3.  
    | Variable_name | Value |
  4.  
    +-----------------+-------+
  5.  
    | wsrep_connected | ON |
  6.  
    +-----------------+-------+
  7.  
    1 row in set (0.00 sec)
  8.  
     

5.驗證集羣:

在qht132中創建些測試數據:

 
  1.  
     
  2.  
    mysql> CREATE DATABASE percona;
  3.  
    Query OK, 1 row affected (0.03 sec)
  4.  
     
  5.  
    mysql> USE percona;
  6.  
    Database changed
  7.  
    mysql> CREATE TABLE example (node_id INT PRIMARY KEY, node_name VARCHAR(30));
  8.  
    Query OK, 0 rows affected (0.04 sec)
  9.  
     
  10.  
    mysql> INSERT INTO percona.example VALUES (1, 'percona1');
  11.  
    Query OK, 1 row affected (0.07 sec)
  12.  
     
  13.  
    mysql> SELECT * FROM percona.example;
  14.  
    +---------+-----------+
  15.  
    | node_id | node_name |
  16.  
    +---------+-----------+
  17.  
    | 1 | percona1 |
  18.  
    +---------+-----------+
  19.  
    1 row in set (0.00 sec)

qht133中查看新數據:

 
  1.  
    mysql> SELECT * FROM percona.example;
  2.  
    +---------+-----------+
  3.  
    | node_id | node_name |
  4.  
    +---------+-----------+
  5.  
    | 1 | percona1 |
  6.  
    +---------+-----------+
  7.  
    1 row in set (0.00 sec)
 
  1.  
    mysql>  INSERT INTO percona.example VALUES (2, 'percona1');
  2.  
    Query OK, 1 row affected (0.04 sec)
  3.  
     

qht131中查看數據:

 
  1.  
    mysql> SELECT * FROM percona.example;
  2.  
    +---------+-----------+
  3.  
    | node_id | node_name |
  4.  
    +---------+-----------+
  5.  
    | 1 | percona1 |
  6.  
    | 2 | percona1 |
  7.  
    +---------+-----------+
  8.  
    2 rows in set (0.00 sec)

PXC集羣內的全部節點都可寫入並同步複製到其餘節點。 

接着測試若是一個節點掛掉後再從新加入到cluster會不會同步數據。

將qht132的mysql停掉

qht132:

 
  1.  
    [root@qht132 ~]# /etc/init.d/mysql stop
  2.  
    Shutting down MySQL (Percona XtraDB Cluster).............. [ OK ]

qht131:

 
  1.  
    mysql>  show status like '%wsrep_clust%';
  2.  
    +--------------------------+--------------------------------------+
  3.  
    | Variable_name            | Value                                |
  4.  
    +--------------------------+--------------------------------------+
  5.  
    | wsrep_cluster_conf_id    | 4                                    |
  6.  
    | wsrep_cluster_size       | 2                                    |
  7.  
    | wsrep_cluster_state_uuid | a084e15e-6f18-11e8-9dc4-efc0d77a254c |
  8.  
    | wsrep_cluster_status     | Primary                              |
  9.  
    +--------------------------+--------------------------------------+
  10.  
    4 rows in set (0.00 sec)

當前的cluster_size爲2,說明只有2個節點是存活的。

 
  1.  
    mysql> INSERT INTO percona.example VALUES (3, 'percona1');
  2.  
    Query OK, 1 row affected (0.00 sec)

qht132:

 
  1.  
    [root@qht132 ~]# /etc/init.d/mysql start
  2.  
    Starting MySQL (Percona XtraDB Cluster).... [ OK ]
 
  1.  
    mysql> SELECT * FROM percona.example;
  2.  
    +---------+-----------+
  3.  
    | node_id | node_name |
  4.  
    +---------+-----------+
  5.  
    | 1 | percona1 |
  6.  
    | 2 | percona1 |
  7.  
    | 3 | percona1 |
  8.  
    +---------+-----------+
  9.  
    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過程:

 
  1.  
    – mysqldump
  2.  
    – rsync
  3.  
    – xtrabackup

mysqldump和rsync的缺點是在數據傳輸過程當中,PXC集羣將會變成只讀模式,SST將會對數據庫施加只讀鎖(FLUSH TABLES WITH READ LOCK)。而使用xtrabackup則不須要再數據同步過程當中施加讀鎖,僅僅是同步.frm文件,相似於常規的備份。 

 

IST用於只將數據的增量變化從一個節點複製到另外一個節點。 
儘管,若是SST使用xtrabackup不須要施加讀鎖,SST仍可能擾亂了服務的正常運行。而IST則不會。若是一個節點掉線時間較短,當再次上線,它將只會從其餘節點獲取掉線期間的數據變化部分。IST是在節點上使用緩存機制實現的。每一個節點包含一個緩存,且環形緩衝區(大小是可配置的)存儲最後N個變化,而且節點可以傳輸該緩存的一部分。顯然,只有當傳輸所需的更改量小於N時,才能夠執行IST,若是超過N,則加入的節點必須執行SST。

 

參考:

https://www.percona.com/doc/percona-xtradb-cluster/5.7/install/index.html

https://blog.csdn.net/geoffreychan/article/details/80499956

相關文章
相關標籤/搜索