Codership Tutorials On Galera Cluster
Codership Tutorials On Galera Cluster
Table of Contents
Introduction & Installation.........................................................................................................................3
Getting Started with Galera Cluster.........................................................................................................3
Installing Galera Cluster...........................................................................................................................7
Galera Cluster for MySQL—Binary Installation............................................................................11
Galera Cluster for MySQL - Source Installation..................................................................................18
MariaDB Galera Cluster - Binary Installation.....................................................................................21
MariaDB Galera Cluster - Source Installation.....................................................................................25
Installing a Galera Cluster on AWS........................................................................................................30
System Configuration............................................................................................................................38
Configuring the InnoDB Buffer Pool¶...............................................................................................41
Replication Configuration......................................................................................................................43
Starting the Cluster................................................................................................................................45
Getting Started Galera with Docker.......................................................................................................49
Testing a Cluster..................................................................................................................................54
Introduction & Installation
Galera Cluster is well suited for LAN, WAN, container and cloud environments. The
following tutorial articles provide you with the basics to setting up and deploying Galera
Cluster. Bear in mind before you get started that you need root access to at least three
Linux or FreeBSD hosts and their IP addresses.
Note
With the latest release Galerea Cluster begins the 4.x branch, introducing a number of
new features. For more information on these features, see What’s New.
The Galera Replication Plugin differs from the standard MySQL Replication by
addressing several issues, including multi-master write conflicts, replication lag and
slaves being out of sync with the master.
In a typical instance of a Galera Cluster, applications can write to any node in the cluster
and transaction commits, (RBR events), are then applied to all the servers, through
certification-based replication.
Note
For security and performance reasons, it’s recommended that you run Galera Cluster on
its own subnet.
Node Initialization
Individual nodes in Galera Cluster are MySQL, MariaDB or Percona XtraDB. But,
deploying a node is not exactly the same as the standard standalone instance of the
database server. You need to take a few additional steps in order to properly install and
configure the software. The software runs on any unix-like operating system. These
articles provide guides to installing and configuring nodes for Galera Cluster.
Installation
Replication Configuration
Cluster Initialization
With the software installed on the relevant servers in your your infrastructure, you can
now initialize Galera Cluster, by bootstrapping the Primary Component then starting all
the other nodes as you would any other database server instance. These tutorial articles
provide guides to starting the cluster, ways of testing that it’s operational and, when you
need to, how to restart the entire cluster.
Testing a Cluster
If your cluster runs on a single switch, use three nodes. If your cluster spans switches,
use three switches. If your cluster spans networks, use three networks. If your cluster
spans data centers, use three data centers. This ensures that the cluster can maintain a
Primary Component in the event of network outages.
Hardware Requirements
For server hardware, each node requires at a minimum the following components:
Note
Galera Cluster may occasionally crash when run on limited hardware due to insufficient
memory. To prevent this, make sure that you have allocated a sufficient amount of swap
space. For more information on how to create swap space, see Configuring Swap Space.
Software Requirements
For software, each node in the cluster requires at a minimum the following:
Note
Binary installation packages for Galera Cluster include the database server with the
wsrep API patch. When building from source, though, you must apply this patch
manually.
If SELinux (Security-Enhanced Linux) is enabled on the servers, it may block mysqld from
performing required operations. You must either disable SELinux for mysqld or configure
it to allow mysqld to run external programs and open listen sockets on unprivileged
ports—that is, operations that an unprivileged user may do.
To disable SELinux for mysqld, execute the following from the command-line:
This command switches SELinux into permissive mode when it registers activity from the
database server. While this is fine during the installation and configuration process, it is
not in general a good policy to disable security applications.
Rather than disable SELinux, so that your may use it along with Galera Cluster, you will
need to create an access policy. This will allow SELinux to understand and allow normal
operations from the database server. For information on how to create such an access
policy, see SELinux.
Note
For more information on writing SELinux policies, see SELinux and MySQL.
Firewall Configuration¶
Next, you will need to update the firewall settings on each node so that they may
communicate with the cluster. How you do this varies depending upon your distribution
and the particular firewall software that you use.
Note
If there is a NAT firewall between the nodes, you must configure it to allow for direct
connections between the nodes, such as through port forwarding.
As an example, to open ports between trusted hosts using iptables, you would execute
something like the following on each node:
This causes packet filtering on the kernel to accept TCP connections between the given
IP addresses.
Warning
The IP addresses in the example are for demonstration purposes only. Use the real
values from your nodes and netmask in the iptables configuration for your cluster.
The updated packet filtering rules take effect immediately, but are not persistent. When
the server reboots, it reverts to default packet filtering rules, which do not include your
updates. To use these rules after rebooting, you need to save them as defaults.
For systems that use systemd, you need to save the current packet filtering rules to the
path that the iptables unit reads when it starts. This path can vary by distribution, but
you can normally find it in the /etc directory.
/etc/sysconfig/iptables
/etc/iptables/iptables.rules
When you find the relevant file, you can save the rules using the iptables-save
command, then redirecting the output to overwrite this file.
# iptables-save > /etc/sysconfig/iptables
When iptables starts it now reads the new defaults, with your updates to the firewall.
For more information on setting up the firewall for Galera Cluster and other programs
for configuring packet filtering in Linux and FreeBSD, see Firewall Settings.
Disabling AppArmor¶
$ sudo ln -s /etc/apparmor.d/usr.sbin.mysqld
/etc/apparmor.d/disable/usr.sbin.mysqld
In some cases you may also need to restart AppArmor. If your system uses init scripts,
run the following command:
If instead, your system uses systemd, run the following command instead:
Note
In the event that you build or install Galera Cluster over an existing standalone instance
of MySQL or MariaDB, there are some additional steps that you need to take in order to
update your system to the new database server. For more information, see Migration.
Galera Cluster for MySQL may be installed on Linux servers using binary packages. These
files can be downloaded directly from the Codership repository, or by way of a package
manager: apt-get, yum or zypper.
For Debian and Debian-based Linux distributions, the procedure for adding a repository
requires that you first install the Software Properties. The package names vary depending
on the distribution. For Debian, at the command-line, execute the following command:
apt-get install python-software-properties
For Ubuntu or a distribution derived from Ubuntu, you would execute instead the
following:
Once you have the Software Properties installed, you can then enable the Codership
repository for your system. Start by adding the GnuPG key for the repository. This is
done by executing the following from the command-line:
Next, add the Codership repository to your sources list. Using a simple text editor, create
file called, galera.list in the /etc/apt/sources.list.d/ directory. Add these lines to that
file, with the necessary adjustments for the version used:
In the example above, you would change the repository addresses. The VERSION should
be set to MySQL-wsrep version you want to install. For example, it might be something
like, 5.6. The DIST should be replaced with the name of the Linux distribution on the
server. This could be ubuntu. Last, replace RELEASE with the distribution release (e.g.,
wheezy).
If you don’t know which release you have installed on your server, you can generally find
this using the following command:
lsb_release -a
Version 4 of Galera was recently released. If you’d like to install it, the configuration lines
in galera.list should read similar to the following:
# Codership Repository (Galera Cluster for MySQL)
Again, you may have to adust the version and release numbers, depending on which
you want to install. Please note that this will require at least version 18.04 of Ubuntu.
To be assured the proper version is installed and updated, set which repository you
prefer to the Codership repository (this is not only recommended, it is required). To do
this, using a text editor, create a file called, galera.pref in the /etc/apt/preferences.d/
directory. The contents should look like the following:
Package: *
Pin-Priority: 1001
This is needed to make sure the patched versions are preferred. This might be
important, for instance, if a third-party program requires libmysqlclient20 and the OS-
version for the library is newer.
Finally, you should update the local cache of the repository. Do this by entering the
following from the command-line:
apt-get update
Once you’ve done all of these tasks, the packages in the Codership repository will be
available for installation. For information on installing them using apt-get, skip ahead
on this page to the section entitled, Installing Galera Cluster for MySQL.
For rpm-based distributions of Linux (e.g., CentOS, Red Hat and Fedora), you will need
to enable the Codership repository. Using a simple text editor, create a file called,
galera.repo in the /etc/yum.repos.d/ directory. The contents of that file should look
something like the following:
[galera]
name = Galera
baseurl = https://releases.galeracluster.com/galera-3/DIST/RELEASE/ARCH
gpgkey = https://releases.galeracluster.com/GPG-KEY-galeracluster.com
gpgcheck = 1
[mysql-wsrep]
name = MySQL-wsrep
baseurl =
https://releases.galeracluster.com/mysql-wsrep-VERSION/DIST/RELEASE/ARCH
gpgkey = https://releases.galeracluster.com/GPG-KEY-galeracluster.com
gpgcheck = 1
In this sample repository configuration file, you would change the repository addresses
for the baseurl. The VERSION should be set to the whichever MySQL-wsrep version you
want (e.g., it might be 5.6). The DIST should be changed to the name of the Linux
distribution you’re using on your sever (e.g., centos or fedora). The RELEASE should be
replaced with the distribution’s release number. It might be 6 for CentOS, but 20 or 21
for Fedora. Last, the ARCH indicates the architecture of your hardware. This could be
changed to x86_64 for 64-bit systems.
After you’ve created, modified, and saved this repository file, you’ll be able to install the
packages from the Codership repository using yum. For an explanation on installing, skip
ahead on this page to the section entitled, Installing Galera Cluster for MySQL.
For servers with Linux distributions that use zypper for package management (e.g.,
openSUSE and SUSE Linux Enterprise Server), you can enable the Codership repository
by importing the GPG key and then creating a galera.repo file in the local directory.
To import the GPG key, execute the following from the command-line on your server:
[galera]
name = Galera
baseurl = https://releases.galeracluster.com/galera-3/DIST/RELEASE/ARCH
[MySQL-wsrep]
name = MySQL-wsrep
baseurl =
https://releases.galeracluster.com/mysql-wsrep-VERSION/DIST/RELEASE/ARCH
You’ll have to adjust these baseurl repository addresses. Replace where you see
VERSION above with the version of MySQL-wsrep you want to install (e.g., it might be
5.6). Next, substitute DIST in both addresses with the Linux distribution name (e.g.,
opensuse or sles). Where it says RELEASE, enter instead the distribution release number.
Last, change ARCH to the architecture of the server’s hardware. For example, you would
enter x86_64 for 64-bit systems. When you’re finished these modifications, save the file.
You’ll now need to add the Codership repository to zypper by executing the following
two lines from the command-line:
zypper refresh
Once that’s all done, you’ll be able to install the packages from the Codership repository
using zypper. This is covered in the next section.
If you want to install version 4 of Galera Cluster, you would instead execute the
following from the command-line:
On servers using the yum package manager (i.e., Red Hat, Fedora and CentOS
distributions), you would instead execute this command:
Please note that on CentOS 6 and 7, this command may generate a transaction check
error. For more information on that error and how to resolve it, see the section below on
MySQL Shared Compatibility Libraries.
If you want to install version 4 of Galera Cluster using yum, you would instead execute
the following from the command-line:
Please note that on CentOS 8 and RedHat 8, you need to disable MySQL and MariaDB
modules before installing Galera Cluster from a repository under
https://releases.galeracluster.com/. In order to do this, execute the following from the
command-line:
If you’re using the zypper utility, which would be used with openSUSE and SUSE Linux
Enterprise Server, you would execute the following from the command-line:
Once you’ve executed the line appropriate to your distribution and package manager,
Galera Cluster for MySQL should be installed on your server. You will then have to
repeat this process for each node in your cluster, including enabling the repository files
mentioned earlier.
Incidentally, when deciding which packages from the Codership repository to install, the
package manager may elect to install a newer major verion of Galera Cluster, newer than
the one you intended to install. Before confirming the installation of packages, make
sure that the package manager is planning to install the Galera Cluster version you want.
If you installed Galera Cluster for MySQL over an existing stand-alone instance of
MySQL, there are some additional steps that you’ll need to take to update your system
to the new database server. For more information, see Migrating to Galera Cluster.
mysql-wsrep-server-5.6-5.6.23-25.10.e16.x86_64 conflicts
This relates to a dependency problem between the version of the MySQL shared
compatibility libraries that CentOS uses, and the one that Galera Cluster requires. To
resolve this, you’ll have to upgrade, which can be done with the Codership repository
using yum.
There are two versions available for this package. Which version you’ll need will depend
on which version of the MySQL wsrep database server you want to install.
For CentOS 6, you would enter something like the following from the command-line:
You would, of course, replace VERSION here with 5.5 or 5.6, depending on the version of
MySQL you want to use. For CentOS 7, to install MySQL version 5.6, you would execute
the following from the command-line:
When yum finishes the upgrade, you can then install the MySQL wsrep database server
and the Galera Replication Plugin as described above.
Galera Cluster for MySQL is the reference implementation from Codership Oy. Binary
installation packages are available for Debian- and RPM-based distributions of Linux. If
your Linux distribution is based upon a different package management system, if your
server uses a different unix-like operating system, such as Solaris or FreeBSD, you will
need to build Galera Cluster for MySQL from source.
Note
If you built Galera Cluster for MySQL over an existing standalone instance of MySQL,
there are some additional steps that you need to take in order to update your system to
the new database server. For more information, see Migrating to Galera Cluster.
# yum-builddep MySQL-server
If neither command works on your system or that you use a different Linux distribution
or FreeBSD, the following packages are required:
MySQL Database Server with wsrep API: Git, CMake, GCC and GCC-C++,
Automake, Autoconf, and Bison, as well as development releases of libaio and
ncurses.
Galera Replication Plugin: SCons, as well as development releases of Boost, Check
and OpenSSL.
Check with the repositories for your distribution or system for the appropriate package
names to use during installation. Bear in mind that different systems may use different
names and that some may require additional packages to run. For instance, to run
CMake on Fedora you need both cmake and cmake-fedora.
1. Clone the Galera Cluster for MySQL database server source code.
3. Checkout the branch for the version that you want to use.
The main branches available for Galera Cluster for MySQL are:
o 5.6
o 5.5
You now have the source files for the MySQL database server, including the wsrep API
patch needed for it to function as a Galera Cluster node.
In addition to the database server, you need the wsrep Provider, also known as the
Galera Replication Plugin. In a separator directory, run the following command:
# cd ..
The database server for Galera Cluster is the same as that of the standard database
servers for standalone instances of MySQL, with the addition of a patch for the wsrep
API, which is packaged in the version downloaded from GitHub. You can enable the
patch through the wsrep API, requires that you enable it through the WITH_WSREP and
WITH_INNODB_DISALLOW_WRITES CMake configuration options.
To build the database server, cd into the mysql-wsrep/ directory and run the following
commands:
# make
# make install
The Galera Replication Plugin implements the wsrep API and operates as the wsrep
Provider for the database server. What it provides is a certification layer to prepare
write-sets and perform certification checks, a replication layer and a group
communication framework.
To build the Galera Replicator plugin, cd into the galera/ directory and run SCons:
# scons
This process creates the Galera Replication Plugin, (that is, the libgalera_smm.so file). In
your my.cnf configuration file, you need to define the path to this file for the
wsrep_provider parameter.
Note
For FreeBSD users, building the Galera Replicator Plugin from source raises certain Linux
compatibility issues. You can mitigate these by using the ports build at
/usr/ports/databases/galera.
Post-installation Configuration
After the build completes, there are some additional steps that you must take in order
to finish installing the database server on your system. This is over and beyond the
standard configurations listed in System Configuration and Replication Configuration.
Note
Unless you defined the CMAKE_INSTALL_PREFIX configuration variable when you ran
cmake above, by default the database server installed to the path /usr/local/mysql/. If
you chose a custom path, adjust the commands below to accommodate the change.
2. # groupadd mysql
3. # useradd -g mysql mysql
5. # cd /usr/local/mysql
6. # ./scripts/mysql_install_db --user=mysql
11. # cp /usr/local/mysql/supported-files/mysql.server \
12. /etc/init.d/mysql
13. # chmod +x /etc/init.d/mysql
14. # chkconfig --add mysql
This allows you to start Galera Cluster using the service command. It also
sets the database server to start during boot.
In addition to this procedure, bear in mind that any custom variables you enabled
during the build process, such as a nonstandard base or data directory, requires that
you add parameters to cover this in the configuration file, (that is, my.cnf).
Note
This tutorial omits MySQL authentication options for brevity.
For Debian and Debian-based Linux distributions, the procedure for adding a repository
requires that you first install the software properties. The package names vary
depending on your distribution. For Debian, at the command-line execute the following:
For Ubuntu or a distribution derived from Ubuntu, execute instead this command:
If you’re use a different Debian-based distribution and neither of these lines above work,
consult your distribution’s package listings for the appropriate package name.
Once you have the software properties installed, you can enable the MariaDB repository
for your server.
First, add the GnuPG key for the MariaDB repository by executing the following from the
command-line:
keyserver.ubuntu.com 0xcbcb082a1bb943db
Next, add the MariaDB repository to your sources list. You can do this by entering
something like the following from the command-line:
# add-apt-repository 'deb
https://mirror.jmu.edu/pub/mariadb/repo/version/distro release main'
You wouldn’t enter exactly the line above. You’ll have to adjust the repository address:
version indicates the version number of MariaDB that you want to use. (e.g., 5.6).
distro is the name of the Linux distribution you’re using’ (e.g., ubuntu).
release should be changed to your distribution release (e.g., wheezy).
If you don’t know which release is installed on your server, you can determine this by
using the entering the following from the command-line:
$ lsb_release -a
1. You should also update the local cache on the server. You can do this by entering
the following:
# apt-get update
For more information on the MariaDB repository, package names and available mirrors,
see the MariaDB Repository Generator.
Packages in the MariaDB repository are now available for installation through apt-get.
For RPM-based distributions (e.g., CentOS, Red Hat and Fedora), you can enable the
MariaDB repository by creating a text file with .repo as the file extension to the
/etc/yum/repos.d/ directory.
Using a simple text editor, create a new .repo file containing something like the
following:
# MariaDB.repo
[mariadb]
name = MariaDB
baseurl = https://yum.mariadb.org/version/package
gpgkey = httpss://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck = 1
For the value of baseurl, you’ll have to adjust the web address:
version should be changed to the version of MariaDB you want to use (e.g., 5.6).
package will have to be changed to the package name for your operating system
distribution, release and architecture. For example, rhel6-amd64 would reference
packages for a Red Hat Enterprise Linux 6 server running on 64-bit hardware.
For more information on the repository, package names or available mirrors, see the
MariaDB Repository Generator. It will generate the actual text you will need to put in
your repository configuration file. In fact, by clicking through the choices presented, you
can just copy the results and paste them into your configuration file without any
modification.
For Debian-based distributions, from the command-line run the following command:
mariadb-galera-server \
galera
For RPM-based distributions, execute instead from the command line the following:
MariaDB-Galera-server \
galera
Once you’ve done this, MariaDB Galera Cluster will be installed on your server. You’ll
need to repeat this process for each node in your cluster.
Note
MariaDB Galera Cluster is the MariaDB implementation of Galera Cluster for MySQL.
Binary installation packages are available for Debian- and RPM-based distributions of
Linux. If your Linux distribution is based on a different package management system, or
if it runs on a different unix-like operating system where binary installation packages are
not available, such as Solaris or FreeBSD, you will need to build MariaDB Galera Cluster
from source.
Note
If you built MariaDB Galera Cluster over an existing standalone instance of MariaDB,
there are some additional steps that you need to take in order to update your system to
the new database server. For more information, see Migration.
# yum-builddep MariaDB-server
In the event that neither command works for your system or that you use a different
Linux distribution or FreeBSD, the following packages are required:
MariaDB Database Server with wsrep API: Git, CMake, GCC and GCC-C++,
Automake, Autoconf, and Bison, as well as development releases of libaio and
ncurses.
Galera Replication Plugin: SCons, as well as development releases of Boost, Check
and OpenSSL.
Check with the repositories for your distribution or system for the appropriate package
names to use during installation. Bear in mind that different systems may use different
names and that some may require additional packages to run. For instance, to run
CMake on Fedora you need both cmake and cmake-fedora.
Building MariaDB Galera Cluster
The source code for MariaDB Galera Cluster is available through GitHub. Using Git you
can download the source code to build MariaDB and the Galera Replicator Plugin locally
on your system.
3. Checkout the branch for the version that you want to use.
o 10.1
o 10.0-galera
o 5.5-galera
Starting with version 10.1, MariaDB includes the wsrep API for Galera
Cluster by default.
Warning
You now have the source files for the MariaDB database server with the wsrep API
needed to function as a Galera Cluster node.
In addition to the database server, you also need the wsrep Provider, also known as the
Galera Replicator Plugin. In a separate directory run the following command:
# cd ..
Once Git finishes downloading the source files, you can start building the database
server and the Galera Replicator Plugin. You now have the source files for the database
server in a server/ directory and the Galera source files in galera/.
The database server for Galera Cluster is the same as that of the standard database
servers for standalone instances of MariaDB, with the addition of a patch for the wsrep
API, which is packaged in the version downloaded from GitHub. You can enable the
patch through the WITH_WSREP and WITH_INNODB_DISALLOW_WRITES CMake configuration
options.
To build the database server, cd into the server/ directory and run the following
commands:
# make
# make install
Note
In addition to compiling through cmake and make, there are also a number of build
scripts in the BUILD/ directory, which you may find more convenient to use. For example,
# ./BUILD/compile-pentium64-wsrep
This has the same effect as running the above commands with various build options
pre-configured. There are several build scripts available in the directory, select the one
that best suits your needs.
The Galera Replication Plugin implements the wsrep API and operates as the wsrep
Provider for the database server. What it provides is a certification layer to prepare
write-sets and perform certification checks, a replication layer and a group
communication framework.
To build the Galera Replication Plugin, cd into the galera/ directory and run SCons.
# scons
This process creates the Galera Replication Pluigin, (that is, the libgalera_smm.so file). In
your my.cnf configuration file, you need to define the path to this file for the
wsrep_provider parameter.
Note
For FreeBSD users, building the Galera Replication Plugin from source raises certain
issues due to Linux dependencies. You can mitgate these by using the ports build
available at /usr/ports/databases/galera or by installing the binary package:
Post-installation Configuration
After the build completes, there are some additional steps that you must take in order
to finish installing the database server on your system. This is over and beyond the
standard configuration process listed in System Configuration and Replication
Configuration.
Note
Unless you defined the CMAKE_INSTALL_PREFIX configuration variable when you ran
cmake above, by default the database is installed to the path /usr/local/mysql/. If you
chose a custom path, adjust the commands below to accommodate the change.
2. # groupadd mysql
3. # useradd -g mysql mysql
5. # cd /usr/local/mysql
6. # ./scripts/mysql_install_db --user=mysql
7. Change the user and group permissions for the base directory.
11. # cp /usr/local/mysql/supported-files/mysql.server \
12. /etc/init.d/mysql
13. # chmod +x /etc/init.d/mysql
14. # chkconfig --add mysql
This allows you to start Galera Cluster using the service command. It also
sets the database server to start during boot.
In addition to this procedure, bear in mind that any further customization variables you
enabled during the build process, such as a nonstandard base or data directory, may
require you to define additional parameters in the configuration file, (that is, my.cnf).
Amazon Web Services (AWS) is one of the largest providers of virtual servers and other
web services. Through an account with AWS, using their EC2 (Elastic Compute Cloud)
platform, you can create virtual servers, known as instances, and choose the operating
system distribution. From there you can log remotely into the instance like you would a
physical server, and install MySQL or MariaDB and Galera software. This means that you
can create multiple instances to form a Galera Cluster.
This tutorial will explain the steps to creating instances on AWS, and how to install and
configure the database and Galera software on each node. It’ll end by showing you how
to start the cluster.
We’re assuming you already have an account with AWS and know the basics of how to
use the EC2 platform to create an instance and related aspects. We’ll focus instead on
aspects relevant to forming a Galera Cluster.
Encryption Key
For this article, we’ll create three nodes to be used in the cluster. To access these nodes,
you’ll need an encryption key. You can use an existing key, but we recommend creating
a new one specifically for the Galera. There are many tools to do so, such as ssh-keygen.
See Generating an SSH Key for instructions on how to do this.
Once you have your encryption key, log into AWS and navigate to the EC2 console.
Under Security in the left margin, click on the Key Pairs link. Click on the white Import
Key Pair button. You can click on Choose File to load a .pem file from your computer, or
you can copy and paste the raw text from your key into the large box labeled, Public Key
Contents. Give the key a name like, galera-cluster. When finished, click Import.
Creating AWS Instances
With an encryption key in place, you’re ready to create three instances on AWS. You
could create more, but we’ll do three, which is the minimum recommended for a healthy
cluster. In the EC2 console, click on Instances in the left margin. That will show your
existing instances in the main pane. Now click on the large blue button at the top
labeled, Launch Instances.
The first choice you’ll have to make when creating a new instance is the operating
system distribution. For the examples in this article, we chose CentOS 7 (x86_64) - with
Updates HVM. This is a 64-bit operating system.
The second step is to choose an instance type. Because we’re using this cluster as a
training tool, we chose t2.micro since it’s Free Tier Eligible. This allows us to use it for a
year without having to pay anything.
Next, you’ll be asked configure the details of the instance. The first box asks how many
instances you want to launch: enter 3. You can leave all of the other settings at their
default values.
Adding storage is next. If you chose the free tier as did we, you’ll have to accept the
default of 8 gigabytes of storage. You may need more if you’re creating a cluster for
production use. For our training purposes, this is plenty. The next screen is about adding
tags, you can click past that.
Then you’ll be asked to configure a security group. This is basically AWS’s firewall. Even
if you already have one for your existing instances, create a new one just for Galera.
Name it something easy like, galera-servers. You’ll be able to give several inbound traffic
rules related to the three nodes we’ll create and the ports they will need. Until we create
the three instances, though, we won’t know their IP addresses. So, for now, just add a
rule that allows you to log into the nodes. To do this, select SSH type. For the source,
click on the pull-down menu and choose My IP—or choose Custom and type in the IP
address from which you’ll log in (e.g., 12.127.17.72/32).
With that all done, click on the blue button at the bottom right to review and launch the
instance. It will show you the choices you made. If everything is fine, click Launch.
A dialog box will then appear asking you to provide an encryption key. Click Choose an
Existing Key Pair and select the one you created for Galera. Then read and click the
check-box for the warning and then click Launch Instance. That’ll begin the process of
starting the instance.
Now, click on the Instances link in the margin again to see state of the instances. It’ll
take a little while before the new instances are running. You may want to label each
instance something like, galera - node 1.
It’s best to keep all three nodes the same. So update the software that’s already installed
on each node with a command-line package-management utility. Since we installed
CentOS on our nodes, we used yum on each like so:
We also installed rsync, which Galera will use to synchronize nodes when they first join
the cluster, or recovering nodes that have been offline for a while. We also installed
firewalld, which provides a firewall.
When you’ve finished updating and installing basic software on all three nodes, you can
then proceed to installing the database and Galera software. For our purposes, we chose
to install MariaDB. If you’d prefer to use MySQL, see the article on Installing MySQL and
Galera Cluster.
To install MariaDB, we first used the MariaDB repository (MariaDB Repository
Generator). We chose the CentOS distribution, the CentOS 7 (x86_64) release to match
what we’ve installed on our nodes. We then chose the MariaDB version 10.4. The
repository tool gave us the lines we needed to copy into a new repo file,
/etc/yum.repos.d/mariadb.repo.
Using yum again, we executed the following from the command-line on each node:
That will install everything we need, including the Galera software since it’s shipped with
MariaDB as of version 10.4. When you’re finished doing this one each node, you’ll be
ready to configure the MariaDB and Galera.
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
bind-address=0.0.0.0
user=mysql
default_storage_engine=InnoDB
innodb_autoinc_lock_mode=2
innodb_flush_log_at_trx_commit=0
innodb_buffer_pool_size=128M
binlog_format=ROW
log-error=/var/log/mysqld.log
There are some basic parameters related to MariaDB and using the InnoDB storage
engine—which is the only one that Galera uses. For parameters particular to Galera, add
a [galera] section to the same configuration file:
[galera]
wsrep_on=ON
wsrep_provider=/usr/lib64/galera-4/libgalera_smm.so
wsrep_node_name='galera1'
wsrep_node_address="172.31.19.208"
wsrep_cluster_name='galera-training'
wsrep_cluster_address="gcomm://172.31.19.208,172.31.26.197,172.31.15.54"
wsrep_provider_options="gcache.size=300M; gcache.page_size=300M"
wsrep_slave_threads=4
wsrep_sst_method=rsync
There are several parameters here, but we’ll talk about just a few of them. First, wsrep_on
enables Galera. The file path for the value of wsrep_provider may have to be adjusted
to your server.
The wsrep_node_name needs to be unique for each node (e.g., galera1, galera2, and
galera3). The wsrep_node_address is the IP address for the node. For AWS, use the
internal IP addresses, not the external ones. You’ll have to adjust this for each node.
Make sure the wsrep_node_name and the wsrep_node_address correspond to the names
you gave for each node in AWS. It will make it easier later to shut down a node for
maintenance.
The wsrep_cluster_name is a name you’ll assign for the cluster. It has to be the same on
all three nodes. The wsrep_cluster_address contains the IP addresses of all of the
nodes in the cluster, including the node on which it’s entered.
That’s all that we need to do to configure MariaDB and Galera. However, don’t start
MariaDB, yet. You first need to configure security on each node and on AWS.
Galera Cluster uses a few TCP ports: 3306 (MariaDB’s default), 4444, 4567, and 4568—
and 4567 for UDP. For SELinux, you will need to open these ports by executing the
following from the command-line on each node:
As for the firewall, you’ll have to first enable and start it by entering the first two lines
below. The six lines that follow those two will open the needed ports. The last line
reloads the firewalld daemon.
firewall-cmd --reload
After these lines have been executed on each node, they’re now ready to interact with
each other. You just need to go back to AWS and add some entries to the security
group you created earlier so that AWS would interfere.
Repeat these four entries for each node, adjusting the IP addresses and descriptions for
each. When you’re finished, click on Save.
Now that you’ve authorized each Galera node to communicate with the others on the
required ports, you’re ready to start Galera
Starting Galera
When first starting a new Galera cluster, you’ll have to start one node first. The problem
is that if there are no other nodes in the cluster, it will fail—unless you start MariaDB on
the first node telling it that it’s the first. To do this, you would use the --wsrep-new-
cluster option.
To make it easy, though, in version 4 of Galera, you can use the galera_new_cluster
script. Just execute it from the command-line on the first node, only. Incidentally, if you
ever shut-down all nodes in the cluster and want to use galera_new_cluster again, you
may have to edit /var/lib/mysql/grastate.dat and change the safe_to_bootstrap
variable in that file from 0 to 1.
Running galera_new_cluster on the first node will start MariaDB and Galera, but only
on that one node. On the other nodes, execute the following from the command-line:
Wait until the second node has started before starting the third. It doesn’t matter, but
it’s better to keep the clutter to a minimum when learning. Once MariaDB has started on
each node, enter the first line below from the command-line on one of the nodes.
Incidentally, there’s no password at this point, so just hit enter when asked.
Enter password:
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| wsrep_cluster_size | 3 |
+--------------------+-------+
You can see from the results here that there are three nodes in the cluster. That’s what
we want. You’ve successfully installed a Galera cluster using AWS instances. You can
start entering data and using the cluster.
If you first want to play with the cluster a bit, log into all three nodes with the mysql
client. Create a database on one node and check the other two nodes to see that it’s
there. Then create a table on another node and insert a row of data into that table on
the third node. Then execute a SELECT statement on each node to see that it’s
replicating the data across the cluster.
System Configuration
After you finish installing Galera Cluster on your server, you’re ready to configure the
database itself to serve as a node in a cluster. To do this, you’ll need to edit the MySQL
configuration file.
Using a text editor, edit the /etc/my.cnf file. You’ll need to include entries like the ones
shown in this sample excerpt:
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
binlog_format=ROW
bind-address=0.0.0.0
default_storage_engine=innodb
innodb_autoinc_lock_mode=2
innodb_flush_log_at_trx_commit=0
innodb_buffer_pool_size=122M
wsrep_provider=/usr/lib/libgalera_smm.so
wsrep_provider_options="gcache.size=300M; gcache.page_size=300M"
wsrep_cluster_name="example_cluster"
wsrep_cluster_address="gcomm://IP.node1,IP.node2,IP.node3"
wsrep_sst_method=rsync
[mysql_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
Depending on your system and the location of your installation of MySQL or MariaDB,
you will need to adjust the valuables for variables (e.g., the path to the data directory).
First, make sure that mysqld is not bound to 127.0.0.1. This is the IP address for
localhost. If the bind-address variable is in the file, comment it out by adding a hash
sign (i.e., #) at the start of the line:
# bind-address = 127.0.0.1
Next, ensure the configuration file includes the conf.d/ by adding a line with !
includedir at the start, followed by the file path:
!includedir /etc/mysql/conf.d/
Now, set the binary log format to use row-level replication, as opposed to statement-
level replication. You’d do this by adding the following line:
binlog_format=ROW
Don’t change this value later as it affects performance and consistency. The binary log
can only use row-level replication for Galera Cluster.
Galera Cluster will not work with MyISAM or other non-transactional storage engines.
So, make sure the default storage engine is InnoDB using the default_storage_engine
variable like so:
default_storage_engine=InnoDB
Next, ensure the InnoDB locking mode for generating auto-increment values is set to
interleaved lock mode. This is designated by a value of 2 for the appropriate variable:
innodb_autoinc_lock_mode=2
Don’t change this value afterwards. Other modes may cause INSERT statements to fail
on tables with AUTO_INCREMENT columns.
Warning
After all of that, make sure the InnoDB log buffer is written to file once per second,
rather than on each commit, to improve performance. To do this, set the
innodb_flush_log_at_trx_commit variable to 0 like so;
innodb_flush_log_at_trx_commit=0
Warning
After you make all of these changes and additions to the configuration file, you’re ready
to configure the database privileges.
The InnoDB storage engine uses its own memory buffer to cache data and for indexes of
tables. You can configure this memory buffer through the innodb_buffer_pool_size
parameter. The default value is 128 MB. To compensate for the increased memory usage
of Galera Cluster over a standalone MySQL database server, you should scale your usual
value back by five percent.
innodb_buffer_pool_size=122M
# swapon --summary
If swap is not configured, nothing will be returned from this command. If your system
doesn’t have swap space available or if the allotted space is insufficient, you can fix this
by creating swap files.
First, create an empty file on your disk, set the file size to whatever size you require. You
can do this with the fallocate tool like so:
Alternatively, you can manage the same using dd utility like this:
Be sure to secure the swap file by changing the permissions on the filesystem with chmod
like this:
$ ls -a / | grep swapfile
Now you’re read to format the swap file. You can do this with the mkswap utility. You’ll
then need to active the swap file.
mkswap /swapfile
swapon /swapfile
Using a text editor, update the /etc/fstab file to include the swap file by adding the
following line to the bottom:
After you save the /etc/fstab file, you run swapon again to see the results:
swapon --summary
Replication Configuration
In addition to the configuration for the database server, there are some specific options
that you need to set to enable write-set replication. You must apply these changes to
the configuration file (i.e., my.cnf) for each node in the cluster.
[mysqld]
wsrep_cluster_name=MyCluster
wsrep_cluster_address="gcomm://192.168.0.1,192.168.0.2,192.168.0.3"
wsrep_node_name=MyNode1
wsrep_node_address="192.168.0.1"
Backend Schema
There are two backend schemata available with Galera Cluster.
Cluster Addresses
For the cluster address section, you have to provide a comma-separate list of IP
addresses for all of the nodes in the cluster. You would do this using the
wsrep_cluster_address parameter. Cluster addresses are listed in the configuration file
using a particular syntax, like so:
Below is an example of how this line from the configuration file might look:
wsrep_cluster_address="gcomm://192.168.0.1,192.168.0.2,192.168.0.3"
Here, the backend schema is gcomm. The cluster addresses (i.e., 192.168.0.1, etc.) are
listed next, separted by commas. You can add options after that, within the quotes. You
would start with a question mark, followed by each option setting. Option key/value
pairs are separated by an ampersand. This is covered in the Options section below.
The IP addresses given in the configuration file should include any current members of
the cluster. The list may also include the IP addresses of any possible cluster members.
Members can belong to no more than one Primary Component;
If you start a node without proving an IP address for this parameter, the node will
assume that it’s the first node of a new cluster. It will initialize the cluster as though you
launched mysqld with the --wsrep-new-cluster option.
Options
When setting the IP address in the configuration file using the wsrep_cluster_address
parameter, you can also set some options. You can set backend parameters, such as the
listen address and timeout values.
Note
The wsrep_cluster_address options list is not durable. The node must resubmit the
options on each connection to a cluster. To make these options durable, set them in the
configuration file using the wsrep_provider_options parameter.
The options set in the URL take precedent over parameters set elsewhere. Parameters
you set through the options list are prefixed by evs (i.e., Extended Virtual Synchrony), pc
(i.e., Primary Component) and gmcast.
When listing options, start with a question mark after the IP address list. Then provide
the options in a key=value format. Key/value pairs must be separated by an ampersand.
Below is an example of how this might look:
wsrep_cluster_address="gcomm://192.168.0.1, 192.168.0.2, 192.168.0.3 ?
gmcast.segment=0 & evs.max_install_timeouts=1"
In this example, the segment option for gcomm and the max_install_timeouts option for
evs are set.
Incidentally, if the listen address and port are not set in the parameter list, gcomm will
listen on all interfaces. The listen port will be taken from the cluster address. If it’s not
specified in the cluster address, the default port is 4567.
After you finish installing MySQL (or MariaDB or Percona XtraDB Cluster to Galera
Cluster) and Galera and have added the necessary settings for the configuration file
needed for Galera Cluster, the next steps are to start the nodes that will form the cluster.
To do this, you will need to start the mysqld daemon on one node, using the --wsrep-
new-cluster option. This initializes the new Primary Component for the cluster. Each
node you start after that will connect to the component and begin replication.
Before you attempt to initialize the cluster, there are a few things you should verify are
in place on each node and related services:
wsrep_provider=/usr/lib64/
libgalera_smm.so
Once you have at least three hosts ready, you can initialize the cluster.
Note
The problem is that there is no Primary Component when a cluster starts, when the first
node is initiated. Therefore, you need explicitly to tell that first node to do so with the --
wsrep-new-cluster argument. Althought this initiate node is said to be the first node, it
can fall behind and leave the cluster without necessarily affecting the Primary
Component.
Note
When you start a new cluster, any node can serve as the first node, since all the
databases are empty. When you migrate from MySQL to Galera Cluster, use the original
master node as the first node. When restarting the cluster, use the most advanced node.
For more information, see Migration and Quorum Reset.
To start the first node–which should have MySQL, MariaDB or Percona XtraDB Cluster,
and Galera installed–you’ll have to launch the database server on it with the --wsrep-
new-cluster option. There are different ways to do this, depending on the operating
system. For systems that use init, execute the following from the command-line:
On Galera:
mysqld_bootstrap --wsrep-new-cluster
On MariaDB:
mysqld --wsrep-new-cluster
On Percona XtraDB Cluster
Warning
Use the --wsrep-new-cluster argument only when initializing the Primary Component.
Don’t use it to connect a new node to an existing cluster.
For operating systems that use systemd, you would instead enter the following from the
command-line:
/usr/bin/mysqld_bootstrap
In MariaDB, use the command below to start MariaDB, Galera, and to establish the
Primary Component:
galera_new_cluster
Once the first node starts the database server, verify that the cluster has started, albeit a
one-node cluster, by checking wsrep_cluster_size. With the database client, execute the
following SQL statement:
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| wsrep_cluster_size | 1 |
+--------------------+-------+
This status variable indicates the number of nodes that are connected to the cluster.
Since only the first node has been started, the value is 1 here. After you start other
nodes that will be part of this same cluster, execute this SQL statement again–on thee
first node or any node you’ve verified are in the cluster. The value should reflect the
number of nodes in the cluster.
Once you get the first node started and the Primary Component initialized, don’t restart
mysqld. Instead, wait until you’ve added more nodes to the cluster so that it can stay
viable without the first node. If you must restart the first node before adding other
nodes, shutdown mysqld and then bootstrap start it again (e.g., execute
galera_new_cluster). If it won’t start as easily as it did the first time, you may have to
edit the file containing the Galera Saved State (i.e., /var/lib/mysql/grastate.dat). The
contents of that file will look something like this:
The variable safe_to_bootstrap is set to 0 on the first node after it’s been bootstrapped
to protect against you inadvertently bootstrapping again while the cluster is runnning.
You’ll have to change the value to 1 to be able to bootstrap anew.
When the database server initializes as a new node, it will try to connect to the cluster
members. It knows where to find these other nodes based on the IP addresses listed in
the wsrep_cluster_address parameter in the configuration file.
You can verify that the node connection was successful checking the wsrep_cluster_size
status variable. In the database client of any node in the cluster, run the following SQL
statement:
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| wsrep_cluster_size | 2 |
+--------------------+-------+
This indicates that the two nodes are now connected to the cluster. When the nodes in
the cluster agree on the membership state, they initiate state exchange. In state
exchange, a new node will check the cluster state. If the state of a new node differs from
the cluster state–which is normally the case–the new node requests a state snapshot
transfer (SST) from the cluster and it installs it on its local database. After this is done,
the new node is ready for use.
Docker is an open platform for developers and sysadmins to build, ship, and run
distributed applications. Consisting of Docker Engine, a portable, lightweight runtime
and packaging tool, and Docker Hub, a cloud service for sharing applications and
automating workflows, Docker enables apps to be quickly assembled from components
and eliminates the friction between development, QA, and production environments. As
a result, it can ship faster and run the same app, unchanged, on laptops, data center
VMs, and any cloud.
let’s first look at how to build a basic Docker Image, which we will extend later. Then
we’ll deploy on a test cluster on a local machine. The examples here have been tested
on Ubuntu 14.04 with Docker 1.5.
FROM ubuntu:14.04
MAINTAINER Erkan Yanar <[email protected]>
ENV DEBIAN_FRONTEND noninteractive
[mysqld]
user = mysql
bind-address = 0.0.0.0
wsrep_provider = /usr/lib/galera/libgalera_smm.so
wsrep_sst_method = rsync
default_storage_engine = innodb
binlog_format = row
innodb_autoinc_lock_mode = 2
innodb_flush_log_at_trx_commit = 0
query_cache_size = 0
query_cache_type = 0
A pre-built image is available from Docker Hub. You can pull it by running:
Starting a Cluster
There have been a number of blog posts showing how to start Galera Cluster on a single
host. This post is going to show the simplest way to do that in Docker by using simple
commands, which will not work for a multi-host installation. First, if working on Ubuntu,
we need to put AppArmor’s Docker profile in complain mode in advance.
Then we can start the first Galera node by instructing Docker to create a container and
run mysqld in it.
$ sudo docker run --detach=true --name node1 -h node1 erkules/galera:basic --
wsrep-cluster-name=local-test --wsrep-cluster-address=gcomm://
In addition to defining the internal name and hostname, we also define the name of the
cluster.
MySQLs error log is not configured explicitly, and Docker records STDOUT and STDERR
of every container. So, using sudo docker logs node1, the log output from the first node
can be displayed without having to enter the container. For the next two containers, we
use a simple Docker trick. The –link option writes the name and the IP of host1 into the
/etc/hosts file of the container. This way, we can connect the remaining nodes to node1
without having to obtain its IP from its container.
Now we have a running Galera cluster. We can check the number of nodes in the Cluster
by running the mysql client from inside one of the containers:
$ sudo docker exec -ti node1 mysql -e 'show status like "wsrep_cluster_size"'
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| wsrep_cluster_size | 3 |
+--------------------+-------+
We built a simple Galera Cluster on one host, without using SSH and without the need
to configure any IP addresses. This setup does not support restarting the container—
you should remove the container and recreate it instead.
3306-MySQL port
4567-Galera Cluster
4568-IST port
4444-SST port
$ aa-complain /etc/apparmor.d/docker
nodea 10.10.10.10
nodeb 10.10.10.11
nodec 10.10.10.12
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| wsrep_cluster_size | 3 |
+--------------------+-------+
In this example, we used the image from the previous blog post. Docker is going to
download the image if it is not already present on the node.
For the purpose, we set Galera Cluster to use non-default ports and then map MySQL’s
default port to 4306:
The docker command line part is straightforward. Please note the additional command-
line options used to configure Galera
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| wsrep_cluster_size | 3 |
+--------------------+-------+
The following Galera Cluster configuration options are used to specify each port:
Summary
That concludes this tutorial. As you can see, it’s easy to run Galera on Docker and inside
Docker on multiple hosts, even with non-standard ports. It is also possible to use
solutions such as weave, socketplane.io and flannel that provide a multi-host network
for the containers.
Testing a Cluster
When you have a cluster running, you may want to test certain features to ensure that
they are working properly or to prepare yourself for handling actual problems that may
arise.
Replication Testing
There are a few step to do to test that Galera Cluster is working as expected. First, using
the database client, verify that all nodes have connected to each other. To do this,
execute the SHOW STATUS statement like so:
+---------------------------+------------+
| Variable_name | Value |
+---------------------------+------------+
...
| wsrep_local_state_comment | Synced (6) |
| wsrep_cluster_size | 3 |
| wsrep_ready | ON |
+---------------------------+------------+
Because of the LIKE operator, only variables beginning with wsrep_ are returned. The
three variables pertinent here are the ones shown in the example above.
For the next test, try creating a table and inserting data into it. Use a database client on
node1 to enter these SQL statements:
USE galeratest;
CREATE TABLE test_table (
id INT PRIMARY KEY AUTO_INCREMENT,
id msg
1
Hello my dear cluster. Hello, again, cluster dear.
2
Split-Brain Testing
There are a few steps to test Galera Cluster
for split-brain situations on a two-node
cluster. First, disconnect the network
connection between the two nodes. At this
point, the Quorum will be lost and the nodes
won’t serve requests.
Failure Simulation
You can also test Galera Cluster by simulating
various failure situations on three nodes. To
simulate a crash of a single mysqld process,
execute the following from the command-line
on one of the nodes:
$ killall -9 mysqld