MySQL InnoDB Cluster With HAProxy For High Availability 1746622861
MySQL InnoDB Cluster With HAProxy For High Availability 1746622861
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
Environment Details:
Topology:
Single-Primary MySQL InnoDB Cluster with automated failover and load-balanced read/write separation
using HAProxy.Step-by-Step Configuration:
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-3: create new file local.repo and add the following lines in
the file.
# vi local.repo
[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
enabled=0
Step-4: Clear the cache and check whether you can get the packages
list from the DVD repo:
SELINUX=permissive
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
# mysql -u root -p
[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.
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
mysqlsh root@localhost:3306
\connect clusteradmin@mysql-node1:3306
dba.checkInstanceConfiguration()
dba.configureInstance()
dba.createCluster("myCluster")
cluster = dba.getCluster()
cluster.addInstance("[email protected]:3306")
cluster.addInstance("[email protected]:3306")
Check status:
cluster.status()
On Node2:
Output:
Install HAProxy:
dnf install haproxy
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
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 /