Follow our step-by-step guide to set up Percona XtraDB Cluster on CentOS 7. Learn how to configure a high-availability MySQL cluster for optimal performance and reliability. #centlinux #linux #mysql
Table of Contents
What is Percona XtraDB Cluster?
Percona XtraDB Cluster (PXC) is a free and open source, high availability solution for MySQL database servers. It integrates Percona Server and Percona XtraBackup with the Galera library to enable synchronous multi-master replication. PXC is supported by Percona Experts and it is distributed under Attribution-ShareAlike 2.0 Generic (CC BY-SA 2.0) license.
To setup Percona XtraDB Cluster, there can be two or more nodes. Each node has the same data that was synchronized across the Cluster. Each node is an actual MySQL database server (Percona, MariaDB, MySQL, etc.) and we can easily add it to the cluster.
In this article, we will install and setup Percona XtraDB Cluster on CentOS 7. We are using just two nodes here, but the configurations do not vary, even if you are scaling to hundreds of nodes.
Read Also: Install Percona XtraDB Cluster on Rocky Linux 9
Features in Percona XtraDB Cluster
PXC provides the following MySQL Cluster advantages.
- Cost-effective HA and scalability for MySQL
- Higher availability
- Multi-master replication
- Automatic node provisioning
- Percona XtraDB Cluster “strict-mode”
- Zero data Loss
- Increased read/write scalability
- ProxySQL load balancer
- ProxySQL-assisted Percona XtraDB Cluster maintenance mode
- Percona Monitoring and Management compatibility
For more details please visit PXC Documentation.
Recommended Training for You: Learn SQL and MySQL in 3 Hours
MySQL Cluster Specification
We are using two CentOS 7 virtual machines with following specifications.
Node 1
- CPU – 3.4 Ghz (2 Cores)
- Memory – 1 GB
- Storage – 60 GB
- Hostname – percona-01.example.com
- IP Address – 192.168.116.204 /24
- Operating System – CentOS 7.6
Node 2
- CPU – 3.4 Ghz (2 Cores)
- Memory – 1 GB
- Storage – 60 GB
- Hostname – percona-02.example.com
- IP Address – 192.168.116.205 /24
- Operating System – CentOS 7.6
Allow PXC Service Ports in Linux Firewall
Connect with percona-01.example.com using ssh as root user.
To setup Percona XtraDB Cluster, we require following service ports for communication, therefore, we are allowing these service ports in CentOS 7 firewall.
# firewall-cmd --permanent --add-port={3306,4444,4567,4568}/tcp success # firewall-cmd --reload success
Percona XtraDB Cluster is not fully compatible with SELinux, and it is recommended in PXC official documentation to put SELinux in permissive mode prior to installation.
# setenforce 0 # sed -i 's/^SELINUX=.*/SELINUX=permissive/g' /etc/selinux/config && cat /etc/selinux/config # This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - No SELinux policy is loaded. SELINUX=permissive # SELINUXTYPE= can take one of three values: # targeted - Targeted processes are protected, # minimum - Modification of targeted policy. Only selected processes are protected. # mls - Multi Level Security protection. SELINUXTYPE=targeted
Install Percona Yum Repository
Download and install rpm package for Percona yum repository as follows.
# yum install -y https://repo.percona.com/yum/percona-release-latest.noarch.rpm Loaded plugins: fastestmirror percona-release-latest.noarch.rpm | 17 kB 00:00 Examining /var/tmp/yum-root-umE9yV/percona-release-latest.noarch.rpm: percona-release-1.0-13.noarch Marking /var/tmp/yum-root-umE9yV/percona-release-latest.noarch.rpm to be installed Resolving Dependencies --> Running transaction check ---> Package percona-release.noarch 0:1.0-13 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: percona-release noarch 1.0-13 /percona-release-latest.noarch 20 k Transaction Summary ================================================================================ Install 1 Package Total size: 20 k Installed size: 20 k Downloading packages: Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : percona-release-1.0-13.noarch 1/1 * Enabling the Percona Original repository <*> All done! The percona-release package now contains a percona-release script that can enable additional repositories for our newer products. For example, to enable the Percona Server 8.0 repository use: percona-release setup ps80 Note: To avoid conflicts with older product versions, the percona-release setup command may disable our original repository for some products. For more information, please visit: https://www.percona.com/doc/percona-repo-config/percona-release.html Verifying : percona-release-1.0-13.noarch 1/1 Installed: percona-release.noarch 0:1.0-13 Complete!
Build cache for all yum repositories.
# yum makecache fast
We have installed Percona yum repository.
Now, we can easily setup Percona XtraDB Cluster using yum command.
# yum install -y Percona-XtraDB-Cluster-57
Enable and start Percona database service.
# systemctl enable --now mysql.service
Percona installer generates a temporary password for root user in /var/log/mysqld.log file.
We can obtain this password using grep command.
# grep 'temporary password' /var/log/mysqld.log 2019-09-18T17:12:24.404976Z 1 [Note] A temporary password is generated for root@localhost: EAFR7kje,_HR
Login to Percona instance using this temporary password.
# 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.27-30-57-log 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. mysql>
Set a new password for root user.
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> exit Bye
Stop Percona database service.
# systemctl stop mysql.service
Repeat the above steps on percona-02.example.com.
Configure PXC nodes for Write-set Replication
Setup Percona XtraDB Cluster configurations on percona-01.example.com.
# vi /etc/percona-xtradb-cluster.conf.d/wsrep.cnf
Find and set following directives therein.
wsrep_cluster_address=gcomm://192.168.116.204,192.168.116.205 wsrep_node_address=192.168.116.204 wsrep_node_name=percona-01 wsrep_sst_auth="sstuser:Ahm3r"
Configure Percona XtraDB Cluster settings on percona-02.example.com.
# vi /etc/percona-xtradb-cluster.conf.d/wsrep.cnf
Find and set following directives therein.
wsrep_cluster_address=gcomm://192.168.116.204,192.168.116.205 wsrep_node_address=192.168.116.205 wsrep_node_name=percona-02 wsrep_sst_auth="sstuser:Ahm3r"
Bootstrap the first node
We have configured both PXC nodes on CentOS 7.
Now its time to bootstap or initialize the first node of the MySQL Cluster.
First node of Percona XtraDB Cluster is the one that contains the data, that you want to replicate to other nodes.
Bootstrap the percona-01.example.com node using following command.
(Note: Please ensure that the mysql.service is stopped on all nodes.)
# systemctl start mysql@bootstrap.service
Connect with Percona database instance and check the configurations.
# 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.27-30-57-log Percona XtraDB Cluster (GPL), Release rel30, Revision 64987d4, WSREP version 31.39, wsrep_31.39 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. mysql> show status like 'wsrep_cluster%'; +----------------------------------+--------------------------------------+ | Variable_name | Value | +----------------------------------+--------------------------------------+ | wsrep_cluster_conf_id | 1 | | wsrep_cluster_size | 1 | | wsrep_cluster_state_uuid | 83b4278c-da37-11e9-82ae-1e52dc50d51c | | wsrep_cluster_status | Primary | +----------------------------------+--------------------------------------+ 4 rows in set (0.00 sec)
Before adding any node to our cluster, we must create a user for SST (Snapshot State Transfer) for complete synchronization of new nodes with cluster.
mysql> CREATE USER 'sstuser'@'%' IDENTIFIED BY 'Ahm3r'; Query OK, 0 rows affected (0.06 sec) mysql> GRANT RELOAD, LOCK TABLES, PROCESS, REPLICATION CLIENT ON *.* TO 'sstuser'@'%'; Query OK, 0 rows affected (0.01 sec) mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec) mysql> EXIT Bye
Add Nodes in Percona XtraDB Cluster
Connect to percona-02.example.com using ssh as root user.
Start Percona service using systemctl command.
# systemctl start mysql.service
If our configurations are correct then the percona-02 node should receive the SST automatically.
# 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.27-30-57-log Percona XtraDB Cluster (GPL), Release rel30, Revision 64987d4, WSREP version 31.39, wsrep_31.39 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. mysql> show status like 'wsrep_cluster%'; +----------------------------------+-------------------------------------------+ | Variable_name | Value | +----------------------------------+-------------------------------------------+ | wsrep_cluster_conf_id | 2 | | wsrep_cluster_size | 2 | | wsrep_cluster_state_uuid | 83b4278c-da37-11e9-82ae-1e52dc50d51c | | wsrep_cluster_status | Primary | +----------------------------------+-------------------------------------------+ 4 rows in set (0.00 sec)
You can see that the wsrep_cluster_size is now 2, it shows that the percona-02 node has joined our Database Cluster.
Verify Database Replication
We can verify replication by manipulating data on one node and check if it replicated on the other node.
Connect with percona-02.example.com using ssh as root user.
Connect to Percona database instance and execute following commands.
mysql> CREATE DATABASE RECIPES; Query OK, 1 row affected (0.06 sec) mysql> USE RECIPES; Database changed mysql> CREATE TABLE TAB1 (CONTACT_ID INT PRIMARY KEY, CONTACT_NAME VARCHAR(20)); Query OK, 0 rows affected (0.05 sec) mysql> INSERT INTO TAB1 VALUES (1,'Ahmer'); Query OK, 1 row affected (0.04 sec) mysql> mysql> INSERT INTO TAB1 VALUES (2,'Mansoor'); Query OK, 1 row affected (0.00 sec) mysql> INSERT INTO TAB1 VALUES (3,'Salman'); Query OK, 1 row affected (0.00 sec)
Connect with percona-01.example.com using ssh as root user.
Connect with Percona database instance and query the data that we have inserted on percona-02 node.
# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 14 Server version: 5.7.27-30-57-log Percona XtraDB Cluster (GPL), Release rel30, Revision 64987d4, WSREP version 31.39, wsrep_31.39 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. mysql> USE RECIPES; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> SELECT * FROM TAB1; +------------+--------------+ | CONTACT_ID | CONTACT_NAME | +------------+--------------+ | 1 | Ahmer | | 2 | Mansoor | | 3 | Salman | +------------+--------------+ 3 rows in set (0.00 sec)
It shows that, our Percona XtraDB Cluster is working fine.
If you are new to MySQL database then, we recommend you to read Murach’s MySQL (3rd Edition) (PAID LINK) by Mike Murach & Associates. This book is a very good starting point for newbies.
Read Also: How to install ProxySQL on CentOS 7
Final Thoughts
Setting up Percona XtraDB Cluster on CentOS 7 is a fantastic way to enhance the performance, reliability, and scalability of your MySQL database environment. By following the steps outlined in this guide, you can achieve a robust, high-availability solution tailored to your needs.
If you’re looking for professional assistance to ensure your MySQL Cluster setup is optimized for performance and reliability, I’m here to help! Check out my Fiverr gig for expert installation, configuration, and support services: Linux Admin Expert
hi friend, i am using Percona cluster with three node(two database db1,db2 nodes, one arbiter node). After running smoothly one month, cluster down service down. now only one node up.
when i try to add problematic db2 node , it can not add to cluster but other arbiter node can join to cluster. db1 has 20gb database, how can add this problematic node db2 to cluster ?
Have a look at Percona Documentation
Can you please use Ansible role to set this Percona cluster
We will work on this a.s.a.p.