本篇主要教大家怎樣在三台CentOS 6系統上安裝Percona XtraDB Cluster,作用主要是資料庫提供高可用性的服務,如叢集由三台伺服器組成,當其中兩台出現問題時,仍有一台可以提供服務,令服務可以正常運作。
如下是叢集裹的三台伺服器:
伺服器#1 主機名:percona1 IP地址:192.168.2.11 伺服器#2 主機名:percona2 IP地址:192.168.2.12 伺服器#3 主機名:percona3 IP地址:192.168.2.13
需要注意的設置
- 防火牆需要允許埠 ports 3306、4444、4567、4568
- 關閉SELinux
安裝
Yum需要加入Percona提供的庫
$ rpm -Uhv https://www.percona.com/redir/downloads/percona-release/redhat/0.0-1/percona-release-0.0-1.x86_64.rpm
使用Yum安裝Percona XtraDB Cluster套件
$ yum install Percona-XtraDB-Cluster-server-55 Percona-XtraDB-Cluster-client-55 Percona-XtraDB-Cluster-galera-2
三台伺服器都需要執行上述部驟安裝Percona XtraDB Cluster
配置
三台伺服器都需要配置 /etc/my.cnf 檔案。
伺服器#1
[mysqld] ...... wsrep_provider=/usr/lib64/libgalera_smm.so wsrep_cluster_address=gcomm://192.168.2.11,192.168.2.12,192.168.2.13 binlog_format=ROW default_storage_engine=InnoDB innodb_autoinc_lock_mode=2 wsrep_node_address=192.168.2.11 wsrep_sst_method=xtrabackup-v2 wsrep_cluster_name=centos_cluster wsrep_sst_auth="sstuser:sstpassword"
完成配置便可以啓動MySQL服務。
[root@percona1 ~]# /etc/init.d/mysql bootstrap-pxc
如作業系統為CentOS 7,啓動指令需要使用如下。
[root@percona1 ~]# systemctl start mysql@bootstrap.service
伺服器#1啓動成功後,進入mysql檢查是否有正常運作。
mysql> show status like 'wsrep%'; +----------------------------+--------------------------------------+ | Variable_name | Value | +----------------------------+--------------------------------------+ | wsrep_local_state_uuid | xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx | ... | wsrep_local_state | 4 | | wsrep_local_state_comment | Synced | ... | wsrep_cluster_size | 1 | | wsrep_cluster_status | Primary | | wsrep_connected | ON | ... | wsrep_ready | ON | +----------------------------+--------------------------------------+ 40 rows in set (0.01 sec)
為了可以使用「State Snapshot Transfer」方式同步資料庫內容,需要建立一個可存取資料庫的用戶。
mysql@percona1> CREATE USER 'sstuser'@'localhost' IDENTIFIED BY 'sstpassword';
mysql@percona1> GRANT PROCESS, RELOAD, LOCK TABLES, REPLICATION CLIENT ON *.* TO 'sstuser'@'localhost';
mysql@percona1> FLUSH PRIVILEGES;
伺服器#1已設置完成。
伺服器#2
[mysqld] ...... wsrep_provider=/usr/lib64/libgalera_smm.so wsrep_cluster_address=gcomm://192.168.2.11,192.168.2.12,192.168.2.13 binlog_format=ROW default_storage_engine=InnoDB innodb_autoinc_lock_mode=2 wsrep_node_address=192.168.2.12 wsrep_sst_method=xtrabackup-v2 wsrep_cluster_name=centos_cluster wsrep_sst_auth="sstuser:sstpassword"
完成配置便可以啓動MySQL服務。
[root@percona2 ~]# /etc/init.d/mysql start
伺服器#2啓動成功後,進入mysql檢查是否有正常運作。
mysql> show status like 'wsrep%'; +----------------------------+--------------------------------------+ | Variable_name | Value | +----------------------------+--------------------------------------+ | wsrep_local_state_uuid | xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx | ... | wsrep_local_state | 4 | | wsrep_local_state_comment | Synced | ... | wsrep_cluster_size | 2 | | wsrep_cluster_status | Primary | | wsrep_connected | ON | ... | wsrep_ready | ON | +----------------------------+--------------------------------------+ 40 rows in set (0.01 sec)
伺服器#3
[mysqld] ...... wsrep_provider=/usr/lib64/libgalera_smm.so wsrep_cluster_address=gcomm://192.168.2.11,192.168.2.12,192.168.2.13 binlog_format=ROW default_storage_engine=InnoDB innodb_autoinc_lock_mode=2 wsrep_node_address=192.168.2.13 wsrep_sst_method=xtrabackup-v2 wsrep_cluster_name=centos_cluster wsrep_sst_auth="sstuser:sstpassword"
完成配置便可以啓動MySQL服務。
[root@percona3 ~]# /etc/init.d/mysql start
伺服器#3啓動成功後,進入mysql檢查是否有正常運作。
mysql> show status like 'wsrep%'; +----------------------------+--------------------------------------+ | Variable_name | Value | +----------------------------+--------------------------------------+ | wsrep_local_state_uuid | xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx | ... | wsrep_local_state | 4 | | wsrep_local_state_comment | Synced | ... | wsrep_cluster_size | 3 | | wsrep_cluster_status | Primary | | wsrep_connected | ON | ... | wsrep_ready | ON | +----------------------------+--------------------------------------+ 40 rows in set (0.01 sec)
測試
測試三台資料庫的複製功能是否正常。
在伺服器#2建立新資料庫「percona」:
mysql@percona2> CREATE DATABASE percona; Query OK, 1 row affected (0.01 sec)
在伺服器#3進入新建的資料庫「percona」,建立新資料表「example」:
mysql@percona3> USE percona; Database changed mysql@percona3> CREATE TABLE example (node_id INT PRIMARY KEY, node_name VARCHAR(30)); Query OK, 0 rows affected (0.05 sec)
在伺服器#1進入新資料表「example」,插入一筆記錄:
mysql@percona1> INSERT INTO percona.example VALUES (1, 'percona1'); Query OK, 1 row affected (0.02 sec)
在伺服器#2從新資料表「example」選取數據:
mysql@percona2> SELECT * FROM percona.example; +---------+-----------+ | node_id | node_name | +---------+-----------+ | 1 | percona1 | +---------+-----------+ 1 row in set (0.00 sec)
如上述測試都能正常執行,代表三台資料庫都是同步狀態。
鏈結到這頁!