mariadb galera cluster(mysql)
As of October 28, 2018 there are 3 hosts running in the galera cluster. They are:
- siosql1.ucsd.edu
- siosql2.ucsd.edu
- siosql3.ucsd.edu
Networking Requirements
All hosts in the cluster, should have the following ports open to talk between hosts:
- TCP/3306
- TCP/4567
- TCP/4568
- TCP/4444
This can be accomplished by modifying /etc/sysconfig/iptables
with the following ruleset:
-A SIO-Firewall -m iprange --src-range 172.21.224.208-172.21.224.210 \ -p tcp -m state --state NEW -m tcp --match multiport --dports \ 3306,4567,4568,4444 -j ACCEPT
Configuration requires that the RHEL system be subscribed to the Red Hat Software Collections(SCL) repos. Once that has been completed, the following packages & their dependencies should be installed.
#yum install rh-mariadb102 rh-mariadb102-mariadb-server rh-mariadb102-mariadb-server-galera
Galera Terminology
quorum: The clustering term for a vote where the nodes decide whether they can agree on what nodes are in the cluster. A 2-node cluster that loses connectivity may have "split-brain" problems.
node: A host that is running mariadb/mysqld with galera clustering enabled(wsrep_on=1)
wsrep: Most/all configuration settings and variables related to Galera clustering are prefaces with "wsrep". This is the API that is used for replication/clustering, and is short for "Write Set Replication"
Red Hat Software Collection Specifics
To run the binaries and services from outside of systemd, you'll need to enable the software collection for use. This can be done interactively:
scl enable rh-mariadb102 bash
or for scripts or one off commands:
scl enable rh-mariadb102 'mysqldump --user=root -p --all-databases'
Paths of useful files
Root Environment:
/opt/rh/rh-mariadb102
Configuration Files:
/etc/opt/rh/rh-mariadb102
Data:
/var/opt/rh/rh-mariadb102/lib/mysql
Logs:
/var/opt/rh/rh-mariadb/log/mariadb
Configuration files
The configuration files will be in the following directory: /etc/opt/rh/rhmariadb102
either in my.cnf
or my.cnf.d
The main variables to configure are:
# wsrep_cluster_name must match across all nodes wsrep_cluster_name="{UNIQUE CLUSTER NAME}" wsrep_cluster_address="gcomm://{IP of NODE1},{IP OF NODE2},...,{IP OF LAST NODE}" wsrep_node_address={IP OF THIS NODE}
wsrep_cluster_name="sio-galera" wsrep_cluster_address="gcomm://172.21.224.208,172.21.224.209,172.21.224.210" wsrep_node_address=172.21.224.208
wsrep_cluster_name="sio-galera" wsrep_cluster_address="gcomm://172.21.224.208,172.21.224.209,172.21.224.210" wsrep_node_address=172.21.224.209
wsrep_cluster_name="sio-galera" wsrep_cluster_address="gcomm://172.21.224.208,172.21.224.209,172.21.224.210" wsrep_node_address=172.21.224.210
Note: selinux must either be disabled(as it is on these systems), or exceptions or policies must be created for mariadb/mysql
Bootstrapping the cluster
There are some unique steps required when initializing or bootstrapping the cluster. The goal is to start the mariadb/mysqld process with galera clustering turned on(wsrep_
on=1
), but to tell the cluster not to attempt connecting to any other nodes, as this is the first one. Mariadb includes a script that does this and integrates well with systemd. This script is named galera_new_cluster
. This script should only be run once on the first node.
Example(no pre-existing data)
The mariadb/mysql credentials for the root account are in 1Password, titled "siosql.ucsd.edu root"
systemctl start rh-mariadb102-mariadb
MariaDB [(none)]> SHOW STATUS LIKE 'wsrep_cluster_size'; +--------------------+-------+ | Variable_name | Value | +--------------------+-------+ | wsrep_cluster_size | 2 | +--------------------+-------+ 1 row in set (0.00 sec)
systemctl start rh-mariadb102-mariadb
MariaDB [(none)]> SHOW STATUS LIKE 'wsrep_cluster_size'; +--------------------+-------+ | Variable_name | Value | +--------------------+-------+ | wsrep_cluster_size | 3 | +--------------------+-------+ 1 row in set (0.01 sec)
Now we want to restart the mariadb daemon on siosql1, so that it is no longer in bootstrapping mode:
[root@siosql1 ~]# ps aux | grep mysqld | grep -v grep mysql 6440 0.1 1.6 2494048 128528 ? Ssl 10:22 0:00 /opt/rh/rh-mariadb102/root/usr/libexec/mysqld --basedir=/opt/rh/rh-mariadb102/root/usr --wsrep-new-cluster [root@siosql1 ~]# systemctl restart rh-mariadb102-mariadb [root@siosql1 ~]# ps aux | grep mysqld | grep -v grep mysql 6981 7.0 1.3 2346424 106892 ? Ssl 10:34 0:00 /opt/rh/rh-mariadb102/root/usr/libexec/mysqld --basedir=/opt/rh/rh-mariadb102/root/usr
By looking at the options passed to mysqld you can see that the galera_new_cluster
command passed the option --wsrep-new-cluster
. Once we restart the service, you can see that it is running in normal cluster mode were it will look for other nodes as defined in wsrep_cluster_address
variable in /etc/opt/rh/rh-mariadb102/my.cnf.d/galera.cnf
.
Now we can enable the service across all nodes
[root@siosql1 ~]# systemctl enable rh-mariadb102-mariadb.service Created symlink from /etc/systemd/system/multi-user.target.wants/rh-mariadb102-mariadb.service to /usr/lib/systemd/system/rh-mariadb102-mariadb.service. [root@siosql2 mariadb]# systemctl enable rh-mariadb102-mariadb Created symlink from /etc/systemd/system/multi-user.target.wants/rh-mariadb102-mariadb.service to /usr/lib/systemd/system/rh-mariadb102-mariadb.service. [root@siosql3 mysql]# systemctl enable rh-mariadb102-mariadb Created symlink from /etc/systemd/system/multi-user.target.wants/rh-mariadb102-mariadb.service to /usr/lib/systemd/system/rh-mariadb102-mariadb.service.
Looking at Cluster Variables
Once we have the cluster up and running, we may want to see the status of the cluster from various nodes.
SHOW STATUS LIKE 'wsrep_%';
Sample output from a healthy 3-node cluster(from siosql1)
Testing the Cluster
MariaDB [(none)]> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | +--------------------+ 3 rows in set (0.01 sec) MariaDB [(none)]> CREATE DATABASE galeratest; Query OK, 1 row affected (0.00 sec) MariaDB [(none)]> USE galeratest; Database changed MariaDB [galeratest]> INSERT INTO test_table (msg) -> VALUES ("Hello my dear cluster."); Query OK, 1 row affected (0.00 sec) MariaDB [galeratest]> INSERT INTO test_table(msg) -> VALUES ("Hello, again, cluster dear."); Query OK, 1 row affected (0.00 sec)
[root@siosql2 ~]# mysql -u root -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 10 Server version: 10.2.8-MariaDB MariaDB Server Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> USE galeratest; 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 MariaDB [galeratest]> SELECT * FROM test_table; +----+-----------------------------+ | id | msg | +----+-----------------------------+ | 4 | Hello my dear cluster. | | 7 | Hello, again, cluster dear. | +----+-----------------------------+ 2 rows in set (0.00 sec)
[root@siosql3 ~]# mysql -u root -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 11 Server version: 10.2.8-MariaDB MariaDB Server Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> USE galeratest; 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 MariaDB [galeratest]> SElECT * FROM test_table; +----+-----------------------------+ | id | msg | +----+-----------------------------+ | 4 | Hello my dear cluster. | | 7 | Hello, again, cluster dear. | +----+-----------------------------+ 2 rows in set (0.00 sec)
Other sources of documentation:
galeracluster.com:
- Getting Started: Galera Cluster Documentation (galeracluster.com)
- Testing the Cluster (galeracluster.com)
- Restarting the Cluster (galeracluster.com)
- Resetting the Quorum (galeracluster.com)
- Glossary (galeracluster.com)
mariadb.com:
- What is MariaDB Galera Cluster? (mariadb.com)
- MariaDB Galera Cluster KB (mariadb.com)
- Getting Started with MariaDB Galera Cluster (mariadb.com)
- Connection Routing with Galera Cluster
redhat.com
- Setting up Galera Cluster with the MariaDB 10.1 Software Collection (RHEL6 and RHEL7) (redhat.com)
- How to recover galera cluster? (redhat.com)
MariaDB 10.2 by Software Collections (softwarecollections.org)
Other: