Percona XtraDB Cluster 是MySQL数据库的一种集群方案。并且与 MySQL Server 社区版本、Percona Server 和 MariaDB 兼容。
一、在Ubuntu上安装Percona XtraDB Cluster
实验环境:
假设有3台计算机设备安装了ubuntu系统,将被用作3个节点:
Node Host IP
Node1 pxc1 172.16.24.209
Node2 pxc2 172.16.24.208
Node3 pxc3 172.16.24.207
前置条件:
(1) 确保一下端口没被防火墙屏蔽或被其他进程占用:
3306
4444
4567
4568
(2) 卸载 apparmor
sudo apt-get remove apparmor
安装步骤:
在每一台设备上执行下列命令:
wget https://repo.percona.com/apt/percona-release_0.1-4.$(lsb_release -sc)_all.deb
sudo dpkg -i percona-release_0.1-4.$(lsb_release -sc)_all.deb
sudo apt update
sudo apt install percona-xtradb-cluster-full-57
passord:frank
至此,percona-xtradb-cluster已经安装,登录 MySQL.
mysql -u root -p
(输入密码 "frank")。
添加用户
mysql> CREATE USER 'sstuser'@'localhost' IDENTIFIED BY 'passw0rd';
mysql> GRANT RELOAD, LOCK TABLES, PROCESS, REPLICATION CLIENT ON *.* TO 'sstuser'@'localhost';
mysql> FLUSH PRIVILEGES;
mysql> quit
sudo service mysql stop
二、配置节点
1. 初始化集群
以第1台设备作为第1个集群节点。在 /etc/mysql/my.cnf 添加如下配置:
###############################################################
[mysqld]
wsrep_provider=/usr/lib/libgalera_smm.so
wsrep_cluster_name=pxc-cluster
wsrep_cluster_address=gcomm://
wsrep_node_name=pxc1
wsrep_node_address=172.16.24.209
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
###############################################################
完成修改之后,执行:
sudo /etc/init.d/mysql bootstrap-pxc
数据库将以自举模式启动。至此,集群初始化工作已经完成。登录MySQL,执行如下命令,查看初始化结果:
mysql> show status like 'wsrep%';
+----------------------------------+--------------------------------------+
| Variable_name | Value |
+----------------------------------+--------------------------------------+
| wsrep_local_state_uuid | 0d718de1-a19c-11e7-81e3-127c64915155 |
| wsrep_protocol_version | 7 |
| wsrep_last_committed | 4 |
...
| wsrep_local_state_comment | Synced |
...
| wsrep_cluster_conf_id | 1 |
| wsrep_cluster_size | 1 |
| wsrep_cluster_state_uuid | 0d718de1-a19c-11e7-81e3-127c64915155 |
| wsrep_cluster_status | Primary |
| wsrep_connected | ON |
| wsrep_local_bf_aborts | 0 |
| wsrep_local_index | 0 |
| wsrep_provider_name | Galera |
| wsrep_provider_vendor | Codership Oy <info@codership.com> |
| wsrep_provider_version | 3.22(r8678538) |
| wsrep_ready | ON |
+----------------------------------+--------------------------------------+
67 rows in set (0.00 sec)
2. 添加节点
添加第2台设备,修改其配置文件 /etc/mysql/my.cnf ,添加如下语句:
###############################################################
[mysqld]
wsrep_provider=/usr/lib/libgalera_smm.so
wsrep_cluster_name=pxc-cluster
wsrep_cluster_address=gcomm://172.16.24.209,172.16.24.208,172.16.24.207
wsrep_node_name=pxc2
wsrep_node_address=172.16.24.208
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
###############################################################
退出重启即可。重启后也登录MySQL,执行“show status like 'wsrep%';”,查看添加结果。
添加第3台设备,修改其配置文件 /etc/mysql/my.cnf ,添加如下语句:
###############################################################
[mysqld]
wsrep_provider=/usr/lib/libgalera_smm.so
wsrep_cluster_name=pxc-cluster
wsrep_cluster_address=gcomm://172.16.24.209,172.16.24.208,172.16.24.207
wsrep_node_name=pxc3
wsrep_node_address=172.16.24.207
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
###############################################################
退出重启即可。重启后也登录MySQL,执行“show status like 'wsrep%';”,查看添加结果。
使第一台设备工作在正常模式。修改其配置文件 /etc/mysql/my.cnf ,添加如下语句:
###############################################################
[mysqld]
wsrep_provider=/usr/lib/libgalera_smm.so
wsrep_cluster_name=pxc-cluster
wsrep_cluster_address=gcomm://172.16.24.209,172.16.24.208,172.16.24.207
wsrep_node_name=pxc1
wsrep_node_address=172.16.24.209
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
###############################################################
重启。
三、验证写集复制功能(Write-Set Replication)
1. Create a new database on the second node:
mysql> CREATE DATABASE testDB1;
Query OK, 1 row affected (0.00 sec)
2. Create a table on the third node:
mysql> USE testDB1
Database changed
mysql> CREATE TABLE example (node_id INT PRIMARY KEY, node_name VARCHAR(30));
Query OK, 0 rows affected (0.01 sec)
3. Insert records on the first node:
mysql> INSERT INTO testDB1.example VALUES (1, 'percona1');
Query OK, 1 row affected (0.01 sec)
4. Retrieve rows from that table on the second node:
mysql> SELECT * FROM testDB1.example;
+---------+-----------+
| node_id | node_name |
+---------+-----------+
| 1 | percona1 |
+---------+-----------+
1 row in set (0.00 sec)
参考文档:
Percona-XtraDB-Cluster-5.7.18-29.20.pdf