Skip to main content
ANNOUNCEMENTMeet MariaDB Enterprise Platform 2027. Read more.

Menu

WEBINAR

Built for the Agentic Era: Introducing MariaDB Enterprise Platform 2027

Watch now

Product

High Availability on AWS with MariaDB Galera Cluster and MariaDB MaxScale

By Anders Karlsson

23 min read
Share

Key takeaways

  • This example uses three MariaDB Galera Cluster nodes across AWS Availability Zones and two MariaDB MaxScale instances to provide high availability across the database and proxy layers..
  • MariaDB MaxScale manages database traffic and server selection, while MariaDB Connector/J provides failover between the two MaxScale instances.
  • The walkthrough covers AWS infrastructure setup, Galera configuration, MaxScale configuration, database failover and proxy failover.
$ chmod 600 myawkkey.ppk
$ ssh -i myawkkey.ppk \
  ec2-user@ec2-16-21-12-20.eu-north-1.compute.amazonaws.com
172.31.29.75    galera1
172.31.32.109   galera2
172.31.0.82     galera3
172.31.24.251   maxscale1
172.31.11.71    maxscale2
$ curl -LsSO \
  https://dlm.mariadb.com/enterprise-release-helpers/mariadb_es_repo_setup
$ chmod +x mariadb_es_repo_setup
$ sudo ./mariadb_es_repo_setup \
  --token="xxxxxxxxxxxxx" –apply --skip-tools \
  --mariadb-server-version="10.6"
$ sudo yum install -y MariaDB-server MariaDB-backup
[mysqld]
proxy-protocol-networks=*
[galera]
# Mandatory settings
wsrep_on=ON
wsrep_provider=/usr/lib64/galera/libgalera_enterprise_smm.so
wsrep_cluster_address=gcomm://galera1,galera2,galera3
binlog_format=row
innodb_autoinc_lock_mode=2
log_error=galera1
$ sudo galera_new_cluster
$ sudo mysql -u root
MariaDB> SHOW GLOBAL STATUS LIKE 'wsrep_cluster_size';
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| wsrep_cluster_size | 1     |
+--------------------+-------+
1 row in set (0.001 sec)
$ sudo systemctl start mariadb
$ sudo mariadb -u root
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 19
Server version: 10.6.19-15-MariaDB-enterprise MariaDB Enterprise Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB> CREATE DATABASE appdb;
Query OK, 1 row affected (0.007 sec)

MariaDB> CREATE USER 'appuser'@'%' IDENTIFIED BY 'OGLom+3J';
Query OK, 0 rows affected (0.008 sec)

MariaDB> GRANT ALL ON appdb.* TO 'appuser'@'%';
Query OK, 0 rows affected (0.008 sec)
$ mariadb -u appuser appdb -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 23
Server version: 10.6.19-15-MariaDB-enterprise MariaDB Enterprise Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB> CREATE TABLE customer(cust_id INTEGER NOT NULL PRIMARY KEY,
    -> cust_name VARCHAR(100) NOT NULL);
Query OK, 0 rows affected (0.013 sec)

MariaDB> INSERT INTO customer VALUES(1, 'John Doe'),(2, 'Jane Doe');
Query OK, 2 rows affected (0.004 sec)
Records: 2  Duplicates: 0  Warnings: 0
$ sudo yum install -y maxscale
[server1]
type=server
address=galera1
port=3306
priority=1
proxy_protocol=true
[server2]
[server3]
[galera-cluster]
type=monitor
module=galeramon
servers=server1,server2,server3
use_priority=true
user=maxuser
password="6MT7|EmZ"
monitor_interval=2s
[galera-cluster-service]
type=service
router=readwritesplit
cluster=galera-cluster
user=maxuser
password="6MT7|EmZ"
[galera-cluster-listener]
type=listener
service=galera-cluster-service
protocol=mariadbprotocol
port=3306
CREATE USER IF NOT EXISTS 'maxuser'@'maxscale1' IDENTIFIED BY '6MT7|EmZ';
GRANT SELECT ON mysql.user TO 'maxuser'@'maxscale1';
GRANT SELECT ON mysql.db TO 'maxuser'@'maxscale1';
GRANT SELECT ON mysql.tables_priv TO 'maxuser'@'maxscale1';
GRANT SELECT ON mysql.columns_priv TO 'maxuser'@'maxscale1';
GRANT SELECT ON mysql.procs_priv TO 'maxuser'@'maxscale1';
GRANT SELECT ON mysql.proxies_priv TO 'maxuser'@'maxscale1';
GRANT SELECT ON mysql.roles_mapping TO 'maxuser'@'maxscale1';
GRANT SHOW DATABASES ON *.* TO 'maxuser'@'maxscale1';
GRANT CONNECTION ADMIN ON *.* TO 'maxuser'@'maxscale1';
$ sudo systemctl start maxscale
$ mariadb -h maxscale1 -u appuser -p appdb
Enter password:
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 10.6.19-15-MariaDB-enterprise MariaDB Enterprise Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB> SELECT * FROM customer;
+---------+-----------+
| cust_id | cust_name |
+---------+-----------+
|       1 | John Doe  |
|       2 | Jane Doe  |
+---------+-----------+
2 rows in set (0.002 sec)
$ sudo yum install -y maxscale
$ sudo mysql -u root
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 25
Server version: 10.6.19-15-MariaDB-enterprise MariaDB Enterprise Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB> CREATE OR REPLACE USER 'maxsyncuser'@'maxscale1'
  IDENTIFIED BY '~5U0hN^G';
Query OK, 0 rows affected (0.008 sec)

MariaDB> CREATE OR REPLACE USER 'maxsyncuser'@'maxscale2'
  IDENTIFIED BY '~5U0hN^G';
Query OK, 0 rows affected (0.008 sec)

MariaDB> GRANT ALL ON mysql.maxscale_config TO 'maxsyncuser'@'maxscale1';
Query OK, 0 rows affected (0.008 sec)
MariaDB> GRANT ALL ON mysql.maxscale_config TO 'maxsyncuser'@'maxscale2';
Query OK, 0 rows affected (0.008 sec)
[maxscale]
threads=auto
config_sync_cluster=galera-cluster
config_sync_user=maxsyncuser
config_sync_password="~5U0hN^G"
$ sudo systemctl restart maxscale
$ sudo systemctl restart maxscale
$ maxctrl show server server1
$ curl -s -u admin:mariadb http://localhost:8989/v1/servers \
 | jq '.data[] | select(.attributes.name == "server1")' \
 | jq '.attributes.parameters.priority'
1
$ maxctrl alter server server1 priority=10
OK
$ curl -s -u admin:mariadb http://localhost:8989/v1/servers \
 | jq '.data[] | select(.attributes.name == "server1")' \
 | jq '.attributes.parameters.priority'
10
import java.sql.*;

public class TestAws {
   public static void main(String[] args) {
      try {
         Connection conn;
         Statement stmt;
         ResultSet rs;

         conn = DriverManager.getConnection(
           "jdbc:mariadb:sequential://maxscale1,maxscale2/appdb",
           "appuser", "OGLom+3J");
         stmt = conn.createStatement();
         for(int i = 0; i < 1000; i++) {
            try {
               rs = stmt.executeQuery(
                 "SELECT cust_id, cust_name FROM customer");
               while(rs.next()) {
                  System.out.println("id: " + rs.getString("cust_id")
                    + " name: " + rs.getString("cust_name"));
               }
               rs.close();
            } catch(SQLException e) {
               System.out.println("MariaDB Error: " + e.getMessage());
            }
            Thread.sleep(1000);
         }
      } catch(Exception e) {
         e.printStackTrace();
      }
   }
}

The architecture is designed to continue operating when an individual database node becomes unavailable, subject to the cluster's quorum and configuration. This example uses three Galera nodes distributed across AWS Availability Zones.

MariaDB MaxScale monitors the database servers and can select another server according to the configured server priorities. The article demonstrates this by changing the priority of server1 and observing a different primary selection.

The example uses two MaxScale instances so the proxy layer does not become a single point of failure. MariaDB Connector/J is configured with both MaxScale instances so the application can reconnect through the available proxy.

Two MaxScale instances provide redundancy at the proxy layer. If one instance becomes unavailable, the application can connect through the other instance using the MariaDB Connector/J configuration shown in this article.

This article documents a self-managed EC2 deployment using Galera Cluster and MaxScale. For new deployments, MariaDB Cloud offers a fully managed option, so teams can use a managed deployment instead of operating the infrastructure described here.