1. 程式人生 > >mysql之PXC5.7.18集群系列——1.Percona XtraDB Cluster搭建

mysql之PXC5.7.18集群系列——1.Percona XtraDB Cluster搭建

1. 相關準備

備註:rhel不安裝epel,很多叢集搭建依賴的包無法通過yum安裝,很是不便 

2. 叢集節點

Nodes Hostname IP
HAProxy haproxy 172.16.3.30
Node1 mysql01 172.16.3.31
Node2 mysql02 172.16.3.32
Node3 mysql03 172.16.3.33
備註:RHEL6.8,4G記憶體

3. 先決條件

(1) 開放埠

# iptables -I INPUT -p tcp -m multiport –dport 3306,4444,4567,4568 -j ACCEPT
# service iptables save
# service iptables restart

備註:1.service iptables status 檢視防火牆狀態  

(2) SELinux

# vim /etc/selinux/config
SELINUX = disabled
# setenforce 0

備註:1.SELINUX = permissive 或者 setenforce 0 都是設定SELINUX狀態為permissive,只不過前者需要重啟伺服器,後者立即生效
備註:2.getenforce 檢視SELINUX狀態  

4. 安裝檔案

檔案 描述
Percona-XtraDB-Cluster Percona-XtraDB-Cluster-5.7.18-29.20-r346-el6-x86_64-bundle.tar
socat yum -y install socat
libev yum -y install libev
perl-DBD-MySQL yum -y install perl-DBD-MySQL
xtrabackup percona-xtrabackup-24-2.4.6-2.el6.x86_64.rpm
cluster Percona-XtraDB-Cluster-57-5.7.18-29.20.1.el6.x86_64.rpm
client Percona-XtraDB-Cluster-client-57-5.7.18-29.20.1.el6.x86_64.rpm
server Percona-XtraDB-Cluster-server-57-5.7.18-29.20.1.el6.x86_64.rpm
shared Percona-XtraDB-Cluster-shared-57-5.7.18-29.20.1.el6.x86_64.rpm

5. 建立目錄

# mkdir -p /home/mysql/{data,log,run,backup}

備註:data,mysql資料目錄
備註:log,mysql日誌目錄
備註:run,mysql執行目錄
備註:backup,mysql備份目錄,推薦使用xtrabackup

6. my.cnf 配置

(1) Node1

[mysql]
port = 3306
socket = /home/mysql/run/mysql.soc

[mysqld]
### Percona XtraDB Cluster 官方配置 begin
wsrep_provider = /usr/lib64/galera3/libgalera_smm.so
wsrep_cluster_name = pxc-cluster
wsrep_cluster_address = gcomm://172.16.3.31,172.16.3.32,172.16.3.33
wsrep_node_name = pxc1
wsrep_node_address = 172.16.3.31
wsrep_sst_method = xtrabackup-v2
wsrep_sst_auth = sstuser:sstpassword
wsrep_provider_options="gcache.size=8G;" #個人(非官方)強烈推薦
pxc_strict_mode = enforcing
binlog_format = ROW
default_storage_engine = InnoDB
innodb_autoinc_lock_mode = 2
### Percona XtraDB Cluster 官方配置 end

#### 自定義配置 begin
user = mysql
port = 3306

datadir = /home/mysql/data
log-bin = /home/mysql/log/mysql-bin
expire_logs_days = 14
log-error = /home/mysql/log/mysql.err

pid-file = /home/mysql/run/mysql.pid
socket = /home/mysql/run/mysql.soc

character_set_server = utf8
collation_server = utf8_general_ci
### 自定義配置 end

### 效能優化 begin
innodb_buffer_pool_size = 3G #80%
innodb_log_file_size = 256M #5%
innodb_flush_method = O_DIRECT #避免雙緩衝技術
max_allowed_packet = 16777216 #最大允許的資料包大小,16M,預設1024*1024*4

sync_binlog = 0 #事務已提交,而無需同步到磁碟
innodb_flush_log_at_trx_commit = 0 #0意味著重新整理到磁碟,但不同步(在提交時不執行實際IO)
### 效能優化 end

備註:wsrep_provider_options="gcache.size=8G;",個人強烈推薦,原因:
利用xtrabackup+IST方式而不是SST向已有叢集中加入新Node,詳見接下來的系列教程。     

(2) Node2

wsrep_node_name = pxc2
wsrep_node_address = 172.16.3.32

(3) Node3

wsrep_node_name = pxc3
wsrep_node_address = 172.16.3.33

7. 檔案許可權

# chowm -R mysql.mysql /home/mysql

8. 叢集啟動

(1) Node1

# service mysql bootstrap-pxc

備註:謹記,只要是啟動叢集的第一個Node(首次搭建叢集或者叢集全部關閉),都要用此命令  

(2) Node2

# service mysql start

備註:謹記,只要叢集有一個Node啟動,其餘節點都是用此命令  

(3) Node3

# service mysql start

9. 建立賬號

mysql> create user 'sstuser'@'localhost' identified by 'sstpassword';
mysql> grant process, reload, lock tables, replication client on *.* to 'sstuser'@'localhost';

備註:sstuser,用於xtrabackup在叢集之間同步資料    

10. 檢視叢集狀態

mysql> show status like 'wsrep%';