0% found this document useful (0 votes)
13 views14 pages

MySQL InnoDB Cluster With HAProxy For High Availability 1746622861

The document outlines the setup of a MySQL InnoDB Cluster with HAProxy for high availability on Oracle Linux 9.5, detailing the architecture, environment, and step-by-step configuration process. It includes instructions for OS-level pre-configuration, MySQL installation, cluster bootstrapping, and HAProxy setup, ensuring automated failover and load-balanced read/write separation. Additionally, it provides troubleshooting tips for common errors encountered during the setup.

Uploaded by

KaNika TH11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views14 pages

MySQL InnoDB Cluster With HAProxy For High Availability 1746622861

The document outlines the setup of a MySQL InnoDB Cluster with HAProxy for high availability on Oracle Linux 9.5, detailing the architecture, environment, and step-by-step configuration process. It includes instructions for OS-level pre-configuration, MySQL installation, cluster bootstrapping, and HAProxy setup, ensuring automated failover and load-balanced read/write separation. Additionally, it provides troubleshooting tips for common errors encountered during the setup.

Uploaded by

KaNika TH11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

MySQL InnoDB Cluster with HAProxy - High Availability Architecture (Oracle Linux 9.

5)
Document by: Animesh Basak | [email protected]

Table of Contents
Architecture .............................................................................................................................................. 2
Environment Details: ................................................................................................................................ 2
Topology: .................................................................................................................................................. 2
Step-by-Step Configuration: ..................................................................................................................... 2
1. OS-Level Pre-configuration (All 3 MySQL Nodes): ............................................................................ 3
2. Install MySQL8 (All Nodes): ................................................................................................................... 5
Import the official MySQL GPG key: .................................................................................................... 5
Install MySQL8: ..................................................................................................................................... 5
Set MySQL Root Password: ................................................................................................................... 5
Log in using the temporary password: ................................................................................................ 5
You must change the root password on first login: ............................................................................ 5
Update MySQL config (/etc/my.cnf.d/mysql-server.cnf): ............................................... 6
3. Bootstrap Cluster on mysql-node1 ....................................................................................................... 7
On mysqlnode1: .................................................................................................................................... 7
If the above fails, you may need to enable MySQL module or use the official MySQL repo: .............. 7
After installation, confirm it works: ...................................................................................................... 7
Login to MySQL Shell: ........................................................................................................................... 7
Create the InnoDB Cluster: ................................................................................................................... 7
Add the Primary Instance to Cluster: .................................................................................................... 7
Add other nodes: .................................................................................................................................. 8
Check status: ......................................................................................................................................... 8
Common Errors & Fixes: ..................................................................................................................... 8
For Test: .................................................................................................................................................... 9
Simulate Failure of PRIMARY: ............................................................................................................. 10
Setup HAProxy: ....................................................................................................................................... 12
Install HAProxy: ................................................................................................................................... 12
Add Custom Health Check Script (port 9200): .................................................................................... 12
Configure HAProxy: ............................................................................................................................. 13
Open your browser: ................................................................................................................................ 14

Animesh Basak | LinkedIn: https://www.linkedin.com/in/animeshbasakcse/


Architecture:

Environment Details:

Server Name Role OS RAM Disk Notes


mysqlnode1 MySQL OL9.5 4GB 30G Start cluster from this node
Primary
Mysqlnode2 MySQL OL9.5 4GB 30G Join this to the cluster
Secondary
Mysqlnode3 MySQL OL9.5 4GB 30G Join this to the cluster
Secondary
haproxy-node HAProxy OL9.5 2GB 10G Load balancing & traffic routing

Topology:

Single-Primary MySQL InnoDB Cluster with automated failover and load-balanced read/write separation
using HAProxy.Step-by-Step Configuration:

Animesh Basak | LinkedIn: https://www.linkedin.com/in/animeshbasakcse/


1. OS-Level Pre-configuration (All 3 MySQL Nodes):

Update System and Install Basic Tools:

sudo dnf update -y

Set Hostname:

192.168.56.107 mysql-node1
192.168.56.108 mysql-node2
192.168.56.109 mysql-node3
192.168.56.110 haproxy-node

Yum Configuration:
Step-1: mount the iso DVD under /mnt directory.
# mount /dev/sr0 /mnt/

Step-2: navigate to the following directory.


# cd /etc/yum.repos.d/

Step-3: create new file local.repo and add the following lines in
the file.
# vi local.repo

Add this Lines:


[InstallMedia-BaseOS]
name=Red Hat Enterprise Linux 8 – BaseOS
metadata_expire=-1
gpgcheck=0
enabled=1
baseurl=file:///mnt/BaseOS/
#gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release

[InstallMedia-AppStream]
name=Red Hat Enterprise Linux 8 – AppStream
metadata_expire=-1
gpgcheck=0
enabled=1
baseurl=file:///mnt/AppStream/
#gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release

Animesh Basak | LinkedIn: https://www.linkedin.com/in/animeshbasakcse/


#vi /etc/yum/pluginconf.d/subscription-manager.conf

enabled=0

Step-4: Clear the cache and check whether you can get the packages
list from the DVD repo:

#yum clean all


#yum update

Step 5:- Add the following to /etc/selinux/config

SELINUX=permissive

Step 6: Disable Firewall [or you can configure port]

systemctl stop firewalld


systemctl disable firewalld

Optionally: if you want to configure ports:


sudo systemctl enable firewalld --now
sudo firewall-cmd --permanent --add-port=3306/tcp #
MySQL
sudo firewall-cmd --permanent --add-port=33060/tcp #
X Plugin
sudo firewall-cmd --permanent --add-port=6446/tcp #
MySQL Router (future)
sudo firewall-cmd --reload

Animesh Basak | LinkedIn: https://www.linkedin.com/in/animeshbasakcse/


2. Install MySQL8 (All Nodes):

Import the official MySQL GPG key:


sudo rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022

Install MySQL8:
# sudo dnf install -y https://dev.mysql.com/get/mysql80-community-
release-el9-1.noarch.rpm
# sudo dnf install -y mysql-community-server

# dnf install -y mysql-server


# systemctl enable --now mysqld

Set MySQL Root Password:


# mysql_secure_installation

Note: Sometime you face that automated tools not works:

temporary password was generated:

# sudo grep 'temporary password' /var/log/mysqld.log

Log in using the temporary password:

# mysql -u root -p

You must change the root password on first login:

# ALTER USER 'root'@'localhost' IDENTIFIED BY 'StrongRoot@1234';

Create Admin User for InnoDB Cluster:

CREATE USER 'clusteradmin'@'%' IDENTIFIED BY 'your_password';


GRANT ALL PRIVILEGES ON *.* TO 'clusteradmin'@'%' WITH GRANT OPTION;

Animesh Basak | LinkedIn: https://www.linkedin.com/in/animeshbasakcse/


Update MySQL config (/etc/my.cnf.d/mysql-server.cnf):

[mysqld]
max_connect_errors = 10000
server_id=1
gtid_mode=ON
enforce_gtid_consistency=ON
master_info_repository=TABLE
relay_log_info_repository=TABLE
binlog_checksum=NONE
log_slave_updates=ON
log_bin=binlog
binlog_format=ROW
transaction_write_set_extraction=XXHASH64
loose-group_replication_group_name="aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"
loose-group_replication_start_on_boot=OFF
loose-group_replication_ssl_mode=REQUIRED
loose-group_replication_local_address= "192.168.56.107:33061"
loose-group_replication_group_seeds=
"192.168.56.107:33061,192.168.56.108:33061,192.168.56.109:33061"
loose-group_replication_bootstrap_group=OFF

Note: On the Other Node Just Change the Server-id=2 or 3 & the loose-
group_replication_local_address Just change the actual ip of node2 &
node3.

Animesh Basak | LinkedIn: https://www.linkedin.com/in/animeshbasakcse/


3. Bootstrap Cluster on mysql-node1:
On mysqlnode1:
# sudo dnf install -y mysql-shell

If the above fails, you may need to enable MySQL module or use the official MySQL repo:
# sudo rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-
el9-1.noarch.rpm
# sudo dnf install -y mysql-shell

After installation, confirm it works:


mysqlsh --version

Login to MySQL Shell:

mysqlsh root@localhost:3306

Create the InnoDB Cluster:

var cluster = dba.createCluster('myCluster')

Add the Primary Instance to Cluster:

\connect clusteradmin@mysql-node1:3306
dba.checkInstanceConfiguration()
dba.configureInstance()
dba.createCluster("myCluster")

Animesh Basak | LinkedIn: https://www.linkedin.com/in/animeshbasakcse/


Add other nodes:

cluster = dba.getCluster()
cluster.addInstance("[email protected]:3306")
cluster.addInstance("[email protected]:3306")

Check status:

cluster.status()

⚠️ Common Errors & Fixes:

Error: Authentication plugin 'caching_sha2_password' reported error:


Authentication requires secure connection

When rejoining or connecting from node1 to others, use:


# cluster.rejoinInstance("clusteradmin@mysql-node2:3306?ssl-
mode=DISABLED")
If Needed:
# mysqlsh --uri clusteradmin@mysql-node1:3306 --ssl-mode=DISABLED

Error: Dba.rebootClusterFromCompleteOutage: Could not determine if Cluster is


completely OFFLINE

# dba.rebootClusterFromCompleteOutage({ "force": true })

Animesh Basak | LinkedIn: https://www.linkedin.com/in/animeshbasakcse/


For Test:

# mysqlsh --uri clusteradmin@localhost:3306


# cluster.status()

Animesh Basak | LinkedIn: https://www.linkedin.com/in/animeshbasakcse/


Note: The cluster is healthy with all nodes online, and mysql-node1 is
the primary node handling read and write operations, while mysql-node2
and mysql-node3 are secondary, read-only nodes. All nodes are running
MySQL 8.0.42, and the SSL encryption requirement is enforced for all
connections.

Simulate Failure of PRIMARY:

On mysql-node1, stop MySQL:

sudo systemctl stop mysqld

Observe Automatic Failover:

On Node2:

mysqlsh --uri clusteradmin@localhost:3306


cluster = dba.getCluster()
cluster.status()

Output:

Animesh Basak | LinkedIn: https://www.linkedin.com/in/animeshbasakcse/


Start Old Primary Again:

sudo systemctl start mysqld

It will rejoin as a SECONDARY.

Animesh Basak | LinkedIn: https://www.linkedin.com/in/animeshbasakcse/


Setup HAProxy:

Install HAProxy:
dnf install haproxy

Add Custom Health Check Script (port 9200):

Create /usr/local/bin/mysqlchk.sh:

#!/bin/bash
MYSQL_USER="clusteradmin"
MYSQL_PASSWORD="your_password"
mysqladmin ping -h 127.0.0.1 -u $MYSQL_USER -p$MYSQL_PASSWORD > /dev/null 2>&1

Gave permission:
chmod +x /usr/local/bin/mysqlchk.sh

Animesh Basak | LinkedIn: https://www.linkedin.com/in/animeshbasakcse/


Configure HAProxy:

Edit /etc/haproxy/haproxy.cfg

frontend mysql_rw_front
bind *:3307
default_backend mysql_rw_back

backend mysql_rw_back
option mysql-check user clusteradmin
server mysql-node1 192.168.56.107:3306 check
server mysql-node2 192.168.56.108:3306 check backup
server mysql-node3 192.168.56.109:3306 check backup

frontend mysql_ro_front
bind *:3308
default_backend mysql_ro_back

backend mysql_ro_back
option mysql-check user clusteradmin
server mysql-node2 192.168.56.108:3306 check
server mysql-node3 192.168.56.109:3306 check

listen stats
bind *:8404
stats enable
stats uri /

Animesh Basak | LinkedIn: https://www.linkedin.com/in/animeshbasakcse/


Start HAProxy:

# systemctl enable --now haproxy

Open your browser:


http://192.168.56.110:8404 to view stats page.

Animesh Basak | LinkedIn: https://www.linkedin.com/in/animeshbasakcse/

You might also like