Skip to main content

Implement a Primary-Replica Cluster Using MariaDB Galera Cluster and Galera Load Balancer

Hands-On Lab

 

Photo of David  Thomas

David Thomas

Database Training Architect II

Length

04:00:00

Difficulty

Intermediate

In this activity, you are working as a DBA and have been tasked with configuring a MariaDB Galera Cluster in a Primary-Replica configuration. The cluster is already up and running, but you will need to install the Galera Load Balancer and configure it appropriately.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Implement a Primary-Replica Cluster Using MariaDB Galera Cluster and Galera Load Balancer

Introduction

In this activity, you are working as a DBA and have been tasked with configuring a MariaDB Galera Cluster in a Primary-Replica configuration. The cluster is already up and running, but you will need to install the Galera Load Balancer and configure it appropriately.

To do this, you will need to complete the following steps:

  1. Set the root database user's password and create a database user account.
  2. Install the prerequisite packages.
  3. Build the Galera Load Balancer binaries.
  4. Configure and start Galera Load Balancer.
  5. Confirm connections to Primary and Replica nodes.
  6. Promote Replica to Primary.

Logging In

Use the credentials provided on the hands-on lab overview page, and log into node0 as cloud_user.

Set the root Database User's Password and Create a Database User Account

The database nodes are running and connected to the cluster, however the root user's password has not been set. You must first set the root user's password by running the mysql_secure_installation script:

[cloud_user@node0 ]$ sudo mysql_secure_installation

Press Enter for none at the first prompt, y at the second to switch to unix_socket authentication, y again to change the root user's password, and then enter and confirm a new password. Answer y to remove anonymous users, and y to disallow root login remotely. Finally, answer y to remove the test database and access to it, and then type y to reload privilege tables now.

Now you can connect to the database as the root user and create a user account:

[cloud_user@node0 ]$ mysql -u root -p

Once connected, you can use the following SQL to create a user named remote who is able to log in remotely. Be sure to replace mypasswd with a password of your choice:

MariaDB [(none)]> create user remote identified by 'mypasswd';
MariaDB [(none)]> grant usage on *.* to 'remote'@'%' identified by 'mypasswd';
MariaDB [(none)]> quit

Then confirm that the user was created:

[cloud_user@node0 ]$ mysql -u remote -p

Once we enter the password we set for remote we'll land at the database prompt again. Type quit to get back out to a shell.

Install the Prerequisite Packages

This will install the packages we need for building Galera Load Balancer:

[cloud_user@node0 ]$ sudo yum -y install git autoconf automake libtool gcc-c++ nmap-ncat

Build the Galera Load Balancer Binaries

First we'll clone the GitHub repository:

[cloud_user@node0 ]$ git clone https://github.com/codership/glb

Then get into the newly created directory and run these scripts:

[cloud_user@node0 ]$ cd glb
[cloud_user@node0 ]$ ./bootstrap.sh
[cloud_user@node0 ]$ ./configure

Now create the binaries and install them:

[cloud_user@node0 ]$ make
[cloud_user@node0 ]$ sudo make install

Configure and Start Galera Load Balancer

First we'll copy the MySQL Watchdog script into place:

[cloud_user@node0 ]$ sudo cp /home/cloud_user/glb/files/mysql.sh /usr/local/bin/mysql-check.sh

Then we'll copy the Galera Load Balancer service file:

[cloud_user@node0 ]$ sudo cp /home/cloud_user/glb/files/glbd.sh /etc/init.d/glb

Now we can create the configuration file:

[cloud_user@node0 ]$ sudo vi /etc/sysconfig/glbd

It should have these contents:

# Address to listen for client connections at. Mandatory parameter.
# To bind to all interfaces only port should be specified.
LISTEN_ADDR="10.0.1.100:13306"

# Address for controlling connection. Mandatory part is port.
# If not specified control socket will not be opened
CONTROL_ADDR="127.0.0.1:4444"

# Target servers for client connections, space separated.
# Target specification: IP[:PORT[:WEIGHT]] (WEIGHT and PORT optional)
# WEIGHT defaults to 1, PORT defaults to LISTEN_ADDR port.
DEFAULT_TARGETS="10.0.1.100:3306:10 10.0.1.110:3306:1"

# Other glbd options if any as they would appear on the command line.
OTHER_OPTIONS="--single --watchdog exec:'/usr/local/bin/mysql-check.sh -uremote -pmypasswd'"

Start the service, and check its status:

[cloud_user@node0 ]$ sudo service glb start
[cloud_user@node0 ]$ sudo service glb status

Confirm Connections to Primary and Replica Nodes

Open up a new terminal, on the other node, and connect to the load balancer:

[cloud_user@node1 ]$ mysql -h 10.0.1.100 -P 13306 -u remote -p

On node0, check for connections:

[cloud_user@node0 ]$ sudo service glb status

The connection was sent to the single server with the highest weight of those available, in this case node0.

Connections to the replica can be made directly on port 3306, but we'll only be able to run read-only queries:

[cloud_user@node0 ]$ mysql -h 10.0.1.110 -P 3306 -u remote -p

Promote Replica to Primary

A replica node can become the primary node by changing each of their weights:

[cloud_user@node0 ]$ service glb add 10.0.1.100:3306:1
[cloud_user@node0 ]$ service glb add 10.0.1.110:3306:10

Check the status again to see the weight changes:

[cloud_user@node0 ]$ sudo service glb status

Create another connection:

[cloud_user@node1 ]$ mysql -h 10.0.1.100 -P 13306 -u remote -p

Then make sure that connections are routed to node1:

[cloud_user@node0 ]$ service glb status

Conclusion

We've got connections going exactly where we want them going, now that we've implemented replication and Galera Load Balancer. Congratulations!