Mysql High-Availablity Setup

MySQL MariaDB Shell Scripting Network Appliances Replication

Project Overview

Robust MySQL server setup for high-availability and high-performance with minimal operational oversight.

Key Features

  • External Health Monitoring
  • Database Replication
  • Network Appliance Load Balancing
  • Automatic Failure response

Technical Implementation

MariaDB servers setup to replicate data changes in a ring arrangement. Network appliance such as an A10 network's Thunder ADC setup to load-balance data read and writes to any of the available database servers. The network applicance is not able to directly execute SQL commands so an external script is written to handle health monitoring and responding to failure. This script runs natively on the A10 Thunder device. It checks the primary MySQL server and, upon failure, executes the CHANGE REPLICATION SOURCE TO (or CHANGE MASTER TO depending on the MySQL/MariaDB version) command on the downstream replica.

Diagram

Health Monitoring Script

#!/bin/bash
# External Health Monitor Script for A10 Thunder ADC
# Arguments passed automatically by A10: $1 = IP, $2 = Port

NODE_IP=$1
NODE_PORT=$2

# 1. Define the ordered array of MySQL IP addresses
# Array order matters: if index N fails, failover targets index N-1
MYSQL_NODES=("192.168.1.51" "192.168.1.52" "192.168.1.53" "192.168.1.54")

# Configuration Variables
DB_USER="adc_orchestrator"
DB_PASS="YourSecurePassword"
REPLICA_PORT="3306"

# 2. Run native MySQL health check against the pool member
mysqladmin -u"$DB_USER" -p"$DB_PASS" -h "$NODE_IP" -P "$NODE_PORT" ping > /dev/null 2>&1

if [ $? -eq 0 ]; then
    # Node is UP
    echo "UP"
    exit 0
else
    # Node is DOWN -> Locate target replica (one index less)
    TARGET_REPLICA=""
    ARRAY_LENGTH=${#MYSQL_NODES[@]}

    for i in "${!MYSQL_NODES[@]}"; do
        if [ "${MYSQL_NODES[$i]}" == "$NODE_IP" ]; then
            # Calculate one index less. If current index is 0, wrap around to the last item.
            if [ $i -eq 0 ]; then
                TARGET_INDEX=$((ARRAY_LENGTH - 1))
            else
                TARGET_INDEX=$((i - 1))
            fi
            TARGET_REPLICA=${MYSQL_NODES[$TARGET_INDEX]}
            break
        fi
    done

    # 3. Execute failover if a valid target replica was determined
    if [ -n "$TARGET_REPLICA" ]; then
        # Note: Adjust to 'CHANGE MASTER TO' if running MySQL versions older than 8.0.23
        SQL_FAILOVER="STOP REPLICA; CHANGE REPLICATION SOURCE TO SOURCE_HOST='${TARGET_REPLICA}'; START REPLICA;"
        
        mysql -u"$DB_USER" -p"$DB_PASS" -h "$TARGET_REPLICA" -P "$REPLICA_PORT" -e "$SQL_FAILOVER" > /dev/null 2>&1
    fi
    
    # Exit with failure to ensure A10 ADC marks the current pool node down
    exit 1
fi