1. 程式人生 > 實用技巧 >CentOS 7 安裝 Percona XtraDB Cluster 5.7

CentOS 7 安裝 Percona XtraDB Cluster 5.7

前言

CentOS 7 安裝 Percona XtraDB Cluster(PXC) 5.7,記錄一下安裝過程。

主要安裝過程 Percona 的官方文件Percona XtraDB Cluster 5.7 Documentation已經有描述,本次記錄一下步驟。

本文使用 MySQL 5.7 版本進行叢集為例,暫時不使用 MySQL 8。

環境說明

CentOS 7(Minimal Install)

$ cat /etc/centos-release
CentOS Linux release 7.6.1810 (Core)

配置如下

NodeHostIP AddrDescprition
Node1 pxc1 10.11.0.81/24 叢集節點1
Node2 pxc2 10.11.0.82/24 叢集節點2
Node3 pxc3 10.11.0.83/24 叢集節點3

說明

  1. Percona XtrDB Cluster 屬於 Multi-master Replication(多主複製)。
  2. 多主複製意味著支援對任意節點的讀寫,資料會同步複製到其他節點。
  3. 建議不要同時對兩個節點上相同的表寫入(即寫入最好分表或分庫)。
  4. 對寫入的分表或分庫,顧名思義,程式連線一個數據庫例項進行寫入,而不是連線多個數據庫例項。

安裝

先決條件

先確認每個叢集節點的機器名,本文以pxc1為例,其他pxc2

,pxc3相同

$ hostname
pxc1

開啟防火牆埠

$ sudo firewall-cmd --permanent --add-port={3306/tcp,4444/tcp,4567/tcp,4568/tcp}
success
$ sudo firewall-cmd --reload
success

過載之後,檢視一下防火牆狀態

$ sudo firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: enp0s3
  sources: 
  services: dhcpv6-client ssh
  ports: 3306/tcp 4444/tcp 4567/tcp 4568/tcp
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 

將每個叢集節點的 SELinux 設定為permissive模式

$ sudo vi /etc/selinux/config
SELINUX=permissive

:wq退出後,重啟系統。

$ sudo shutdown -r now

重啟完作業系統後,確認 SELinux 設定

$ sestatus
SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   permissive
Mode from config file:          permissive
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Max kernel policy version:      31

從 Percona 倉庫安裝

配置 yum 源如下,本文以pxc1為例,其他pxc2,pxc3相同

$ sudo yum install https://repo.percona.com/yum/percona-release-latest.noarch.rpm

注意本文使用時,yum 安裝的版本為percona-release-1.0-11.noarch,預設啟用了Percona Original repository,為 MySQL 5.7 的版本。

安裝 Percona XtraDB Cluster 的包

$ sudo yum install Percona-XtraDB-Cluster-57
...
...
 -------------
   *  The suggested mysql options and settings are in /etc/percona-xtradb-cluster.conf.d/mysqld.cnf
   *  If you want to use mysqld.cnf as default configuration file please make backup of /etc/my.cnf
   *  Once it is done please execute the following commands:
 rm -rf /etc/my.cnf
 update-alternatives --install /etc/my.cnf my.cnf "/etc/percona-xtradb-cluster.cnf" 200
 -------------
Percona XtraDB Cluster is distributed with several useful UDFs from Percona Toolkit.
Run the following commands to create these functions:
mysql -e "CREATE FUNCTION fnv1a_64 RETURNS INTEGER SONAME 'libfnv1a_udf.so'"
mysql -e "CREATE FUNCTION fnv_64 RETURNS INTEGER SONAME 'libfnv_udf.so'"
mysql -e "CREATE FUNCTION murmur_hash RETURNS INTEGER SONAME 'libmurmur_udf.so'"
See  http://www.percona.com/doc/percona-server/5.7/management/udf_percona_toolkit.html for more details
...
...
Replaced:
  mariadb-libs.x86_64 1:5.5.60-1.el7_5

Complete!

安裝過程中會出現如下提示,可以根據需要進行配置。

修改密碼

3 個節點都安裝完軟體包之後,接下來只對第一個節點pxc1進行修改密碼操作。

先啟動 Percona XtraDB Cluster 服務。

[admin@pxc1 ~] $ sudo systemctl start mysqld

拷貝 MySQL 安裝時自動生成的 root 的臨時密碼

[admin@pxc1 ~] $ sudo grep 'temporary password' /var/log/mysqld.log

使用臨時密碼登入 MySQL

[admin@pxc1 ~] $ mysql -u root -p

更改 root 使用者的密碼,之後退出。

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Passw0rd';
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye

關閉 MySQL 服務。

[admin@pxc1 ~] $ sudo systemctl stop mysqld

注意

  1. 以上修改密碼操作只在第一個節點進行即可。
  2. 配置好第二個和第三個節點,啟動服務操作後會複製到其他節點中。

配置節點的 Write-set Replication

配置 my.cnf

配置第一個節點pxc1/etc/my.cnf

[admin@pxc1 ~] sudo vi /etc/my.cnf

新增如下內容

# Add below configuration to override the default wsrep.conf

[mysqld]
wsrep_provider=/usr/lib64/galera3/libgalera_smm.so

wsrep_cluster_name=pxc-cluster
wsrep_cluster_address=gcomm://10.11.0.81,10.11.0.82,10.11.0.83

wsrep_node_name=pxc1
wsrep_node_address=10.11.0.81

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

這些內容也同樣對pxc2,pxc3進行配置,只有兩個引數wsrep_node_name,wsrep_node_address配置的值需要修改一下

對於pxc2內容為

wsrep_node_name=pxc2
wsrep_node_address=10.11.0.82

對於pxc3內容為

wsrep_node_name=pxc3
wsrep_node_address=10.11.0.83

配置引數說明

wsrep_provider
指定 Galera 庫的路徑。

wsrep_cluster_name
指定叢集的邏輯名稱,叢集內的所有節點,這個名稱必須一致。

wsrep_cluster_address
指定叢集內節點的 IP 地址,建議將叢集節點都配上。

wsrep_node_name
指定單個節點的邏輯名稱,如果沒有指定,將使用 hostname 作為邏輯名稱。

wsrep_node_address
指定此特定節點的 IP 地址。

wsrep_sst_method
預設的使用 Percona Xtrabackup 進行 State Snapshot Transfer (SST),強烈建議使用wsrep_sst_method=xtrabackup-v2

wsrep_sst_auth
指定 sst 的身份驗證憑據,使用<sst_user>:<sst_pass>這種格式,啟動第一個節點時必須建立,並且提供相應的許可權。

pxc_strict_mode
關閉實驗性的或者不支援的特性

binlog_format
Galera 只支援 row-level replication,所以設定為binlog_format=ROW

default_storage_engine
Galera 只支援 InnoDB 引擎,所以設定為default_storage_engine=InnoDB

innodb_autoinc_lock_mode
Galera 只支援 lock mode 為2的 InnoDB 引擎,所以設定為innodb_autoinc_lock_mode=2

啟動第一個節點

在第一個節點pxc1使用如下命令啟動

[admin@pxc1 ~]$ sudo systemctl start mysql@bootstrap

使用 bootstrap 模式啟動,預設會設定wsrep_cluster_address=gcomm://,後面沒有 IP,表示初始化叢集。

為確保初始化完成,可以使用如下命令檢視

登入 MySQL

[admin@pxc1 ~]$ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.7.25-28-57-log Percona XtraDB Cluster (GPL), Release rel28, Revision a2ef85f, WSREP version 31.35, wsrep_31.35

Copyright (c) 2009-2019 Percona LLC and/or its affiliates
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
myslq>

之後輸入

mysql> show status like 'wsrep%';
+----------------------------------+-----------------------------------------------+
| Variable_name                    | Value                                         |
+----------------------------------+-----------------------------------------------+
| wsrep_local_state_uuid           | 77007d05-777b-11e9-9836-3a33a4649f12          |
| ...                              | ...                                           |
| ...                              | ...                                           |
| wsrep_local_state                | 4                                             |
| wsrep_local_state_comment        | Synced                                        |
| ...                              | ...                                           |
| ...                              | ...                                           |
| wsrep_cluster_size               | 1                                             |
| ...                              | ...                                           |
| wsrep_cluster_status             | Primary                                       |
| wsrep_connected                  | ON                                            |
| ...                              | ...                                           |
| ...                              | ...                                           |
| wsrep_ready                      | ON                                            |
+----------------------------------+-----------------------------------------------+
71 rows in set (0.01 sec)

可以看到叢集數量時1,本節點為Synced狀態,表示連線成功,並且準備好進行write-set replication

在新增其他節點之前,我們要新增SST使用者並分配相應的許可權。

mysql> CREATE USER 'sstuser'@'localhost' IDENTIFIED BY 'Passw0rd';
Query OK, 0 rows affected (0.12 sec)

mysql> GRANT RELOAD, LOCK TABLES, PROCESS, REPLICATION CLIENT ON *.* TO 'sstuser'@'localhost';
Query OK, 0 rows affected (0.09 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.10 sec)

注意使用者名稱和密碼必須跟配置檔案匹配。

新增其他節點

注意

  1. 所有其他節點的資料和配置都會被第一個節點的資料覆蓋
  2. 不用同時加入多個節點,避免資料或網路開銷過大

啟動第二個節點

[admin@pxc2 ~]$ sudo systemctl start mysqld

啟動完成後,pxc2會接到pxc1的資料,可以使用如下命令檢視狀態

[admin@pxc2 ~]$ mysql -u root -p

登入到 MySQL 後,輸入如下命令

mysql> show status like 'wsrep%';
+----------------------------------+--------------------------------------+
| Variable_name                    | Value                                |
+----------------------------------+--------------------------------------+
| wsrep_local_state_uuid           | 33e1e8eb-777b-11e9-b16f-9f9d75d90117 |
| ...                              | ...                                  |
| ...                              | ...                                  |
| wsrep_local_state                | 4                                    |
| wsrep_local_state_comment        | Synced                               |
| ...                              | ...                                  |
| ...                              | ...                                  |
| wsrep_cluster_size               | 2                                    |
| ...                              | ...                                  |
| wsrep_cluster_status             | Primary                              |
| wsrep_connected                  | ON                                   |
| ...                              | ...                                  |
| ...                              | ...                                  |
| wsrep_ready                      | ON                                   |
+----------------------------------+--------------------------------------+
71 rows in set (0.00 sec)

可以看到wsrep_cluster_size的值是2表示叢集已經有 2 個節點了。wsrep_local_state_comment的值是Synced表示已經同步了。

注意如果wsrep_local_state_comment的狀態是Joiner,表示正在同步,請不要啟動第三個節點的服務。

啟動第三個節點

注意確認好第二個節點的狀態為Synced後,再啟動第三個節點。

步驟與啟動第二個節點相同

[admin@pxc3 ~]$ sudo systemctl start mysqld

啟動完成後,pxc3會接到叢集,可以使用如下命令檢視狀態

[admin@pxc3 ~]$ mysql -u root -p

登入到 MySQL 後,輸入如下命令

mysql> show status like 'wsrep%';
+----------------------------------+--------------------------------------+
| Variable_name                    | Value                                |
+----------------------------------+--------------------------------------+
| wsrep_local_state_uuid           | 33e1e8eb-777b-11e9-b16f-9f9d75d90117 |
| ...                              | ...                                  |
| ...                              | ...                                  |
| wsrep_local_state                | 4                                    |
| wsrep_local_state_comment        | Synced                               |
| ...                              | ...                                  |
| ...                              | ...                                  |
| wsrep_cluster_size               | 3                                    |
| ...                              | ...                                  |
| wsrep_cluster_status             | Primary                              |
| wsrep_connected                  | ON                                   |
| ...                              | ...                                  |
| ...                              | ...                                  |
| wsrep_ready                      | ON                                   |
+----------------------------------+--------------------------------------+
71 rows in set (0.00 sec)

可以看到叢集數量是3,連線沒有問題,準備 write-set replication

驗證複製效果

當所有的節點都加入到叢集之後,可以驗證一下複製效果。

  1. 到第二個節點pxc2上建立一個新的資料庫
mysql@pxc2> CREATE DATABASE percona;
Query OK, 1 row affected (0.18 sec)
  1. 到第三個節點pxc3上,再這個資料庫上新建一個表
mysql@pxc3> USE percona;
Database changed

mysql@pxc3> CREATE TABLE example (node_id INT PRIMARY KEY, node_name VARCHAR(30));
Query OK, 0 rows affected (0.05 sec)
  1. 到第一個節點pxc1上,插入資料
mysql@pxc1> INSERT INTO percona.example VALUES (1, 'percona1');
Query OK, 1 row affected (0.02 sec)
  1. 再到第二個節點pxc2上查詢一下插入的資料
mysql@pxc2> SELECT * FROM percona.example;
+---------+-----------+
| node_id | node_name |
+---------+-----------+
|       1 | percona1  |
+---------+-----------+
1 row in set (0.01 sec)

可以看到,多主複製很好,3 個節點都是主節點,相互複製的效果也很好。

結束語

Percona XtraDB Cluster 搭建很簡單,多主複製很適合高併發環境。

當然,它也是有侷限的,具體可以參考:Percona XtraDB Cluster Limitations, 但是這些侷限不是太大的問題,因為一般情況不會用到。