Bobcares

Automatic Failover MariaDB | All About

by | Sep 19, 2022

How to enable the automatic Failover with MariaDB? With our Server Management Services, bobcares offers solutions for your MariaDB issues.

 

Automatic Failover with MariaDB Monitor

MariaDB Monitor keeps an eye on a Master-Slave replication cluster. It checks the condition of the backends and assigns server roles like master and slave, which help routers determine where to route a query. Additionally, It can also change the replication cluster’s configuration by conducting Failover, switchover, and rejoin.

 

automatic failover mariadb

 

Failover and other comparable operations require MariaDB 10.0.2 or later and are also initially only supported in conjunction with GTID-based replication and for simple topologies, i.e., one master and numerous slaves. The MariaDB Monitor includes Failover, switchover, and rejoin capability, although neither automated Failover nor automated rejoin is in enable mode by default.

 

How does the Failover functionality works?

The basic operation it performs is replacing a failed master with a running slave. Let’s see the steps in detail:

  • Choose the most latest slave of the old master to be the new master. The following criteria are in ascending order of priority:
     
    1. disk space is not low.
     
    2. log_slave_updates is on.
     
    3. gtid_current_pos (most processed events).
     
    4. gtid_IO_pos (latest event in relay log).
     
  • Cancel and try again later if the new master has unprocessed relay log items.
     
  • Get the new master ready as follows:
     
    1. Remove the slave connection that the new master uses to replicate from the old master.
     
    2. Deactivate the read_only-flag.
     
    3. Activate planned server events (if event handling is on). Only events that were enabled on the previous master are active.
     
    4. Execute the commands in promotion_sql_file.
     
    5. If an external master exists, then begin replication from it.
     
  • Instruct all other slaves to replicate the new master.
     
    1. STOP SLAVE and RESET SLAVE.
     
    2. CHANGE MASTER TO.
     
    3. START SLAVE.
     
  • Confirm that all slaves are replicating. A successful Failover happens when the initial three steps execute successfully.
     

How to create automatic Failover?

Consider there are four servers – server1, server2, server3, and lastly, server4. The server1 is the initial master whereas the others are slaves. In addition, there is a monitor called TheMonitor that monitors those servers. The MaxScale configuration file would look something like this:

 
[server1]
type=server
IP Address =192.168.121.52
port=3301
protocol=MariaDBBackend

[server2]
...

[server3]
...

[server4]
...

[TheMonitor]
type=monitor
module=mariadbmon
servers=server1,server2,server3,server4
...
 

In order to enable the automatic failover, we have to add auto_failover=true to the monitor section in the configuration file.

 
[TheMonitor]
type=monitor
module=mariadbmon
servers=server1,server2,server3,server4
auto_failover=true
...
 

We can use the command $ maxctrl list servers in order to confirm all servers are running fine.

$ maxctrl list servers
┌─────────┬─────────────────┬──────┬─────────────┬─────────────────┐
│ Server  │ IP Address      │ Port │ Connections │ State           │
├─────────┼─────────────────┼──────┼─────────────┼─────────────────┤
│ server1 │ 192.168.121.52  │ 3301 │ 0           │ Master, Running │
├─────────┼─────────────────┼──────┼─────────────┼─────────────────┤
│ server2 │ 192.168.121.191 │ 3301 │ 0           │ Slave, Running  │
├─────────┼─────────────────┼──────┼─────────────┼─────────────────┤
│ server3 │ 192.168.121.114 │ 3301 │ 0           │ Slave, Running  │
├─────────┼─────────────────┼──────┼─────────────┼─────────────────┤
│ server4 │ 192.168.121.211 │ 3301 │ 0           │ Slave, Running  │
└─────────┴─────────────────┴──────┴─────────────┴─────────────────┘
 

Consider that server1 goes down now. Then the Failover will occur automatically, and an existing slave will be promoted to the new master.

$ maxctrl list servers
┌─────────┬─────────────────┬──────┬─────────────┬────────────────────────┐
│ Server  │ IP Address      │ Port │ Connections │ State                  │
├─────────┼─────────────────┼──────┼─────────────┼────────────────────────┤
│ server1 │ 192.168.121.52  │ 3301 │ 0           │ Down                   │
├─────────┼─────────────────┼──────┼─────────────┼────────────────────────┤
│ server2 │ 192.168.121.191 │ 3301 │ 0           │ Master, Slave, Running │
├─────────┼─────────────────┼──────┼─────────────┼────────────────────────┤
│ server3 │ 192.168.121.114 │ 3301 │ 0           │ Slave, Running         │
├─────────┼─────────────────┼──────┼─────────────┼────────────────────────┤
│ server4 │ 192.168.121.211 │ 3301 │ 0           │ Slave, Running         │
└─────────┴─────────────────┴──────┴─────────────┴────────────────────────┘
 

For a short moment, we may observe that the state of server1 is Down but the state of server2 remains Slave, Running.

 

[Looking for a solution to another query? We are just a click away.]

 

Conclusion

In conclusion, we have seen the operational details of Failover. We have also included the steps from our Tech team on how to automate Failover functionality in MariaDB.

 

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF