Professional Documents
Culture Documents
How To Make MySQL Highly Available - by Vivek Puri - DevOps Enthusiast - Medium
How To Make MySQL Highly Available - by Vivek Puri - DevOps Enthusiast - Medium
Firstly, I am writing a short description of the problem, I am going to solve through this
article. In the world of large scale applications in terms of user base or the criticality, the
risk of downtime of even a single minute can cost you millions of dollars. As we know,
our applications are hosted on servers and servers have a lifespan. If you have hosted
your application on the cloud, then you don’t even have the control of the actual
machine. In our case, we use AWS and many times, we have faced StatusCheckFailure
which can lead to complete machine failure. In order to make it running again, it has to
be stopped and then started which can take 2–5 minutes. Similar is the case with any
server hosted locally. If it fails, it has to be replaced with the new one and that is going to
cost more in terms of time. Now, let’s see, how we can make MySQL highly available.
https://medium.com/devops-enthusiast/how-to-make-mysql-highly-available-a383111f19ad 1/6
2/10/2021 How to make MySQL highly available | by Vivek Puri | DevOps Enthusiast | Medium
DB1 as MASTER
As shown above, we are considering DB1 as our primary database server and DB2 is a
passive database server. DB1 and DB2 are in sync and are master-master replicated. DB1
is having two IPs, Primary IP 172.31.1.1 and a Secondary IP 172.31.1.23. DB2 is only
having one IP 172.31.1.2. On AWS, you can easily attach secondary private IP to a server
from the console. Please consider this secondary IP important as we are going to play
around it now. Application servers are connected to DB1 using its secondary IP address.
Let’s get introduced to a service called Keepalived.
Keepalived is a routing service that works on VRRP protocol and helps in load-balancing
and high-availability to Linux system and Linux based infrastructures. You can read more
about keepalived here.
https://medium.com/devops-enthusiast/how-to-make-mysql-highly-available-a383111f19ad 2/6
2/10/2021 How to make MySQL highly available | by Vivek Puri | DevOps Enthusiast | Medium
aws configure
AWS Access Key ID [****************ABCD]: <AccessKeyGoesHere>
AWS Secret Access Key [****************WXYZ]: <SecretKeyGoesHere>
Default region name [ap-south-1]: <RegionOfMySQLHere>
Default output format [None]: <LeaveItBlank>
vrrp_script chk_mysql {
script "/usr/bin/mysqladmin ping -h 127.0.0.1 -P 3306 -u dbuser -
pdbpass > /dev/null 2>&1"
interval 2
timeout 1
fall 3
rise 3
}
vrrp_instance VI_1 {
debug 2
interface eth0 # interface to monitor
state MASTER
virtual_router_id 51 # Assign one ID for this route
priority 101 # 101 on master, 100 on backup
unicast_src_ip 172.31.1.1 # Primary IP address
unicast_peer {
172.31.1.2 # Primary IP address of DB2
}
track_script {
chk_mysql
}
notify_master /etc/keepalived/master.sh
}
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/us
r/games:/usr/local/games
secondaryIp="172.31.1.23" # Secondary IP
https://medium.com/devops-enthusiast/how-to-make-mysql-highly-available-a383111f19ad 3/6
2/10/2021 How to make MySQL highly available | by Vivek Puri | DevOps Enthusiast | Medium
instanceIdA="<InstanceIDOfDB1>"
instanceIdB="<InstanceIDOfDB2>"
sleep 5
service network restart # On CentOS
vrrp_script chk_mysql {
script "/usr/bin/mysqladmin ping -h 127.0.0.1 -P 3306 -u dbuser -
pdbpass > /dev/null 2>&1"
interval 2
timeout 1
fall 3
rise 3
}
vrrp_instance VI_1 {
debug 2
interface eth0 # interface to monitor
state BACKUP
virtual_router_id 51 # Assign one ID for this route
https://medium.com/devops-enthusiast/how-to-make-mysql-highly-available-a383111f19ad 4/6
2/10/2021 How to make MySQL highly available | by Vivek Puri | DevOps Enthusiast | Medium
notify_master /etc/keepalived/master.sh
}
master.sh on DB2 is going to be the same as of DB1 except one change which is instance-id.
instanceIdA="<InstanceIDOfDB2>"
instanceIdB="<InstanceIDOfDB1>"
To test the failover, stop MySQL on DB1. In a span of 3 seconds, DB2 will become
MASTER. Secondary IP 172.31.1.23 will be detached from DB1 and will be attached to
DB2 now using the script master.sh on DB2. In the next 5 seconds, network service will
be restarted and the failover will happen in a total of 8 seconds. In case DB1 comes up
again, it will be re-elected as MASTER and again secondary IP will be attached to DB1.
https://medium.com/devops-enthusiast/how-to-make-mysql-highly-available-a383111f19ad 5/6
2/10/2021 How to make MySQL highly available | by Vivek Puri | DevOps Enthusiast | Medium
DB2 as MASTER
https://medium.com/devops-enthusiast/how-to-make-mysql-highly-available-a383111f19ad 6/6