SlideShare a Scribd company logo
1 of 45
Download to read offline
Automated MySQL
Failover With MHA:
Getting Started And
Moving Past Its Quirks
Colin Charles, Monty Program Ab SkySQL Ab
colin@mariadb.org | byte@bytebot.net
http://skysql.com/ | http://mariadb.org/
http://bytebot.net/blog/ | @bytebot on Twitter
Velocity 2013, Santa Clara, CA, USA
20 June 2013
1
whoami
Work on MariaDB at Monty Program Ab
- Merging with SkySQL Ab
Formerly MySQL AB (exit: Sun Microsystems)
Past lives include Fedora Project (FESCO), OpenOffice.org
MHA experience
- since November 2011 (MHA 0.52, 0.53)
- NRE work to make it run in a Solaris 10 environment... with no
Internet access!
- Continued deployment advice + work for data centre use
- Much thanks to SkySQL for the experience
2
Aims
Why MHA?
What does MHA do?
How does MHA do it?
Running MHA, VIP failover, integration, etc
Who uses MHA?
Is fully automated failover a good idea?
3
Why this talk
High Performance MySQL, 3rd Edition
Published: March 16 2012
4
But first... MySQL replication
Single master, multiple slave architecture
- When master is unavailable, writes stop being accepted
Promoting a new master is not that easy
- New master needs to wait to apply all relay log events
- Slaves need to be consistent
- MySQL clients need to reconnect to new master
- All slaves need to start replication from proper binlog
position
5
Where did MHA come from?
DeNA won 2011 MySQL Community
Contributor of the Year (April 2011)
MHA came in about 3Q/2011
Written by Yoshinori Matsunobu, Oracle
ACE Director
6
What is MHA?
MHA for MySQL: Master High Availability Manager tools for
MySQL
Goal: automating master failover & slave promotion with
minimal downtime
Set of Perl scripts
http://code.google.com/p/mysql-master-ha/
7
Why MHA?
Automating monitoring of your replication topology for master
failover
Scheduled online master switching to a different host for
online maintenance
- Switch back after OPTIMIZE/ALTER table, software or
hardware upgrade
- Schema changes without stopping services
- pt-online-schema-change, oak-online-alter-table, Facebook OSC
Interactive/non-interactive master failover (just for failover,
with detection of master failure + VIP takeover to Pacemaker)
8
Why is master failover hard?
When master fails, no more writes
till failover complete
MySQL replication is
asynchronous (MHA works with
async + semi-sync replication)
slave2 is latest, slave1+3 have
missing events, MHA does:
copy id=10 from master if possible
apply all missing events
9
Semi-sync replication
If master crashes (or is shutdown :P), MHA cannot save
binlogs, latest data is lost
Minimises risk of binlog event existing only on crashed
master
Guarantees at least one slave receives binlog events at
commit
http://code.google.com/p/mysql-master-ha/wiki/
UseCases#Using_together_with_Semi-
Synchronous_Replication
10
MHA: Typical scenario
Monitor replication topology
If failure detected on master, immediately switch to a
candidate master or the most current slave to become new
master
- MHA must fail to connect to master server three times
CHANGE MASTER for all slaves to new master
Print (stderr)/email report, stop monitoring
11
So really, what does MHA do?
12
Typical timeline
Usually no more than 10-30 seconds
0-10s: Master failover detected in around 10 seconds
- (optional) check connectivity via secondary network
(optional) 10-20s: 10 seconds to power off master
10-20s: apply differential relay logs to new master
Practice: 4s @ DeNA, usually less than 10s
13
How does MHA work?
Save binlog events from crashed master
Identify latest slave
Apply differential relay log to other slaves
Apply saved binlog events from master
Promote a slave to new master
Make other slaves replicate from new master
14
Getting Started
MHA requires no changes to your
application
You are of course to write to a
virtual IP (VIP) for your master
MHA does not build replication
environments for you - that’s DIY
15
MHA Node
Download mha4mysql-node & install this on all machines:
master, slaves, monitor
Packages (DEB, RPM) available
Manually, make sure you have DBD::mysql & ensure it
knows the path of your MySQL
16
What’s in MHA node
save_binary_logs - save & copy master’s binlogs
apply_diff_relay_logs - find differential relay log events
& apply missing events
purge_relay_logs - purge relay log files
filter_mysqlbinlog - OBSOLETE + not used by MHA
(trims ROLLBACK statements & equivalent binlog events
added by mysqlbinlog)
17
MHA Manager server
Monitor server doesn’t have to be powerful at all, just remain
up
This is a single-point-of-failure so monitor the manager server
where MHA Manager gets installed
If MHA Manager isn’t running, your app still runs, but
automated failover is now disabled
18
MHA Manager
You must install mha4mysql-node then mha4mysql-
manager
Manager server has many Perl dependencies:
DBD::mysql, Config::Tiny, Log::Dispatch,
Parallel::ForkManager, Time::HiRes
Package management fixes dependencies, else use CPAN
19
Configuring MHA
Application configuration file: see samples/conf/
app1.cnf
- Place this in /etc/MHA/app1.cnf
Global configuration file: see /etc/MHA/
masterha_default.cnf (see samples/conf/
masterha_default.cnf)
20
app1.cnf
[server default]
manager_workdir=/var/log/masterha/app1
manager_log=/var/log/masterha/app1/manager.log
[server1]
hostname=host1
[server2]
hostname=host2
candidate_master=1
[server3]
hostname=host3
[server4]
hostname=host4
no_master=1
21
no need to specify master as
MHA auto-detects this
sets priority, but doesn’t necessarily mean it gets promoted
as a default (say its too far behind replication).
But maybe this is a more powerful box, or has a better setup
will never be the master. RAID0 instead of RAID1+0?
Slave is in another data centre?
masterha_default.cnf
[server default]
user=root
password=rootpass
ssh_user=root
master_binlog_dir= /var/lib/mysql,/var/log/mysql
remote_workdir=/data/log/masterha
ping_interval=3
# secondary_check_script=masterha_secondary_check -s remote_host1 -s
remote_host2
# master_ip_failover_script= /script/masterha/master_ip_failover
# shutdown_script= /script/masterha/power_manager
# report_script= /script/masterha/send_report
# master_ip_online_change_script= /script/masterha/master_ip_online_change
22
check master activity from
manager->remote_hostN->
master (multiple hosts to
ensure its not a network issue)
STONITH
MHA uses SSH
MHA uses SSH actively; passphraseless login
- In theory, only require Manager SSH to all nodes
However, remember masterha_secondary_check
masterha_check_ssh --conf=/etc/MHA/app1.cnf
23
Check replication
masterha_check_repl --conf=/etc/MHA/app1.cnf
If you don’t see MySQL Replication Health is OK, MHA will
fail
Common errors? Master binlog in different position, read
privileges on binary/relay log not granted, using multi-master
replication w/o read-only=1 set (only 1 writable master
allowed)
24
MHA Manager
masterha_manager --conf=/etc/MHA/app1.cnf
Logs are printed to stderr by default, set manager_log
Recommended running with nohup, or daemontools
(preferred in production)
http://code.google.com/p/mysql-master-ha/wiki/
Runnning_Background
25
So, the MHA Playbook
Install MHA node, MHA manager
masterha_check_ssh --conf=/etc/app1.cnf
masterha_check_repl --conf=/etc/app1.cnf
masterha_manager --conf=/etc/app1.cnf
That’s it!
26
master_ip_failover_script
Pacemaker can monitor & takeover VIP if required
Can use a catalog database
- map between application name + writer/reader IPs
Shared VIP is easy to implement with minimal changes to
master_ip_failover itself (however, use shutdown_script
to power off machine)
27
master_ip_online_change
Similar to master_ip_failover script, but used for online
maintenance
-masterha_master_switch --master_state=alive
MHA executes FLUSH TABLES WITH READ LOCK after the
writing freeze
28
Test the failover
masterha_check_status --conf=/etc/MHA/
app1.cnf
Kill MySQL (kill -9, shutdown server, kernel panic)
MHA should go thru failover (stderr)
- parse the log as well
Upon completion, it stops running
29
masterha_master_switch
Manual failover
---master_state=dead
Scheduled online master switchover
Great for upgrades to server, etc.
-masterha_master_switch --master_state=alive
--conf=/etc/MHA/app1.cnf --
new_master_host=host2
30
Handling VIPs
my $vip = ‘192.168.0.1/24”;
my $interface = “0”;
my $ssh_start_vip = “sudo /sbin/ifconfig eth0:$key $vip”;
my $ssh_stop_vip = “sudo /sbin/ifconfig eth0:$key down”;
...
sub start_vip() {
`ssh $ssh_user@$new_master_host ” $ssh_start_vip ”`; }
sub stop_vip() {
`ssh $ssh_user@$orig_master_host ” $ssh_stop_vip ”`; }
31
Integration with other HA solutions
Pacemaker
- on RHEL6, you need some HA add-on, just use the CentOS
packages
/etc/ha.d/haresources to configure VIP
-`masterha_master_switch --master_state=dead
--interactive=0 --wait_on_failover_error=0
--dead_master_host=host1 --
new_master_host=host2`
Corosync + Pacemaker works well
32
Solaris 10
MHA tested to work on Linux and Solaris 10 & greater
Use a .pkg MySQL
Solaris 10 needs a compiler (SolarisStudio - register w/Oracle
to download)
CPAN: manual builds of dependencies
Solaris doesn’t have md5sum (call md5)
Solaris ssh isn’t OpenSSH, missing some features like
ConnectionTimeout
33
What about replication delay?
By default, MHA checks to see if slave is behind master. By
more than 100MB, it is never a candidate slave
If you have candidate_master=1 set, consider setting
check_repl_delay=0
You can integrate it with pt-heartbeat from Percona
Toolkit
- http://www.percona.com/doc/percona-toolkit/2.1/pt-
heartbeat.html
34
MHA deployment tips
 You really should install this as root
 SSH needs to work across all hosts
 If you don’t want plaintext passwords in config files, use
init_conf_load_script
 Each monitor can monitor multiple MHA pairs (hence app1, app2, etc.)
 You can have a standby master, make sure its read-only
 By default, master1->master2->slave3 doesn’t work
- MHA manages master1->master2 without issue
- Use multi_tier_slave=1 option
 Make sure replication user exists on candidate master too!
35
Alternate solutions
Heartbeat + DRBD
- Costs $$$ -> passive master
- innodb_flush_log_at_trx_commit=1, sync_binlog=1 is
expensive in MySQL
- http://www.mysqlperformanceblog.com/2008/06/02/how-much-overhead-drdb-could-
cause/
- MariaDB 5.3/5.5 or Percona Server 5.5 or MySQL 5.6
mitigate this by having group commit in the binary log
36
Alternate solutions II
MySQL NDB Cluster
Galera Cluster
- MariaDB Galera Cluster, Percona XtraDB Cluster
Percona Replication Manager (PRM)
- https://raw.github.com/y-trudeau/resource-agents-prm/
master/heartbeat/mysql
Tungsten Replicator
mysqlfailover from mysql-utilities using GTID’s in 5.6
37
Where is MHA used
DeNA
Premaccess (Swiss HA hosting company)
Ireland’s national TV & radio service
Jetair Belgium (MHA + MariaDB!)
Samsung
SK Group
DAPA
Facebook
38
MHA 0.55, 0.56 comes soon
MHA 0.55 is released December 18 2012
- It is OK for node to be 0.54 in this situation, due to bug in
manager only
http://code.google.com/p/mysql-master-ha/wiki/ReleaseNotes
0.56 coming with:
- MySQL 5.6 support for checksum in binlog events
- mysqlbinlog and mysql in custom locations (configurable
clients)
- MHA resource agent shipped
39
Is fully automated failover a good idea?
 False alarms
- Can cause short downtime, restarting all write connections
 Repeated failover
- Problem not fixed? Master overloaded?
- MHA ensures a failover doesn’t happen within 8h, unless --
last_failover_minute=n is set
 Data loss
- id=103 is latest, relay logs are at id=101, loss
- group commit means sync_binlog=1, innodb_flush_log_at_trx_commit=1 can
be enabled! (just wait for master to recover)
 Split brain
- sometimes poweroff takes a long time
40
Support
SkySQL: www.skysql.com
Percona: www.percona.com
PalominoDB: www.palominodb.com
AccelerationDB: www.accelerationdb.com
- training provider as well
41
Automated tools to play with
4-host Vagrant setup for MySQL MHA
- https://github.com/hholzgra/vagrant-mysql-mha
Palomino Cluster Tool
- https://github.com/time-palominodb/PalominoClusterTool
- Ansible playbooks for MHA
4-host Vagrant setup for MHA
- https://github.com/lefred/vagrant-mha
42
Video resources
Yoshinori Matsunobu talking about High Availability & MHA at
Oracle MySQL day
- http://www.youtube.com/watch?v=CNCALAw3VpU
Alex Alexander (AccelerationDB) talks about MHA, with an
example of failover, and how it compares to Tungsten
- http://www.youtube.com/watch?v=M9vVZ7jWTgw
43
References
Design document
- http://www.slideshare.net/matsunobu/automated-master-
failover
Configuration parameters
- http://code.google.com/p/mysql-master-ha/wiki/Parameters
JetAir MHA use case
- http://www.percona.com/live/mysql-conference-2012/sessions/
case-study-jetair-dramatically-increasing-uptime-mha
MySQL binary log
- http://dev.mysql.com/doc/internals/en/binary-log.html
44
Thank you - Q&A
Colin Charles, Monty Program Ab SkySQL Ab
colin@mariadb.org | byte@bytebot.net
http://skysql.com/ | http://mariadb.org/
http://bytebot.net/blog/ | @bytebot on Twitter
Velocity 2013, Santa Clara, CA, USA
20 June 2013
45

More Related Content

More from Colin Charles

What is MariaDB Server 10.3?
What is MariaDB Server 10.3?What is MariaDB Server 10.3?
What is MariaDB Server 10.3?Colin Charles
 
Databases in the hosted cloud
Databases in the hosted cloud Databases in the hosted cloud
Databases in the hosted cloud Colin Charles
 
MySQL features missing in MariaDB Server
MySQL features missing in MariaDB ServerMySQL features missing in MariaDB Server
MySQL features missing in MariaDB ServerColin Charles
 
The MySQL ecosystem - understanding it, not running away from it!
The MySQL ecosystem - understanding it, not running away from it! The MySQL ecosystem - understanding it, not running away from it!
The MySQL ecosystem - understanding it, not running away from it! Colin Charles
 
Databases in the Hosted Cloud
Databases in the Hosted CloudDatabases in the Hosted Cloud
Databases in the Hosted CloudColin Charles
 
Best practices for MySQL High Availability Tutorial
Best practices for MySQL High Availability TutorialBest practices for MySQL High Availability Tutorial
Best practices for MySQL High Availability TutorialColin Charles
 
Percona ServerをMySQL 5.6と5.7用に作るエンジニアリング(そしてMongoDBのヒント)
Percona ServerをMySQL 5.6と5.7用に作るエンジニアリング(そしてMongoDBのヒント)Percona ServerをMySQL 5.6と5.7用に作るエンジニアリング(そしてMongoDBのヒント)
Percona ServerをMySQL 5.6と5.7用に作るエンジニアリング(そしてMongoDBのヒント)Colin Charles
 
Capacity planning for your data stores
Capacity planning for your data storesCapacity planning for your data stores
Capacity planning for your data storesColin Charles
 
The Proxy Wars - MySQL Router, ProxySQL, MariaDB MaxScale
The Proxy Wars - MySQL Router, ProxySQL, MariaDB MaxScaleThe Proxy Wars - MySQL Router, ProxySQL, MariaDB MaxScale
The Proxy Wars - MySQL Router, ProxySQL, MariaDB MaxScaleColin Charles
 
Lessons from {distributed,remote,virtual} communities and companies
Lessons from {distributed,remote,virtual} communities and companiesLessons from {distributed,remote,virtual} communities and companies
Lessons from {distributed,remote,virtual} communities and companiesColin Charles
 
Forking Successfully - or is a branch better?
Forking Successfully - or is a branch better?Forking Successfully - or is a branch better?
Forking Successfully - or is a branch better?Colin Charles
 
MariaDB Server Compatibility with MySQL
MariaDB Server Compatibility with MySQLMariaDB Server Compatibility with MySQL
MariaDB Server Compatibility with MySQLColin Charles
 
Securing your MySQL / MariaDB Server data
Securing your MySQL / MariaDB Server dataSecuring your MySQL / MariaDB Server data
Securing your MySQL / MariaDB Server dataColin Charles
 
The MySQL Server Ecosystem in 2016
The MySQL Server Ecosystem in 2016The MySQL Server Ecosystem in 2016
The MySQL Server Ecosystem in 2016Colin Charles
 
The Complete MariaDB Server tutorial
The Complete MariaDB Server tutorialThe Complete MariaDB Server tutorial
The Complete MariaDB Server tutorialColin Charles
 
Best practices for MySQL/MariaDB Server/Percona Server High Availability
Best practices for MySQL/MariaDB Server/Percona Server High AvailabilityBest practices for MySQL/MariaDB Server/Percona Server High Availability
Best practices for MySQL/MariaDB Server/Percona Server High AvailabilityColin Charles
 
Lessons from database failures
Lessons from database failures Lessons from database failures
Lessons from database failures Colin Charles
 
Lessons from database failures
Lessons from database failuresLessons from database failures
Lessons from database failuresColin Charles
 
Lessons from database failures
Lessons from database failuresLessons from database failures
Lessons from database failuresColin Charles
 
MariaDB 10.1 what's new and what's coming in 10.2 - Tokyo MariaDB Meetup
MariaDB 10.1   what's new and what's coming in 10.2 - Tokyo MariaDB MeetupMariaDB 10.1   what's new and what's coming in 10.2 - Tokyo MariaDB Meetup
MariaDB 10.1 what's new and what's coming in 10.2 - Tokyo MariaDB MeetupColin Charles
 

More from Colin Charles (20)

What is MariaDB Server 10.3?
What is MariaDB Server 10.3?What is MariaDB Server 10.3?
What is MariaDB Server 10.3?
 
Databases in the hosted cloud
Databases in the hosted cloud Databases in the hosted cloud
Databases in the hosted cloud
 
MySQL features missing in MariaDB Server
MySQL features missing in MariaDB ServerMySQL features missing in MariaDB Server
MySQL features missing in MariaDB Server
 
The MySQL ecosystem - understanding it, not running away from it!
The MySQL ecosystem - understanding it, not running away from it! The MySQL ecosystem - understanding it, not running away from it!
The MySQL ecosystem - understanding it, not running away from it!
 
Databases in the Hosted Cloud
Databases in the Hosted CloudDatabases in the Hosted Cloud
Databases in the Hosted Cloud
 
Best practices for MySQL High Availability Tutorial
Best practices for MySQL High Availability TutorialBest practices for MySQL High Availability Tutorial
Best practices for MySQL High Availability Tutorial
 
Percona ServerをMySQL 5.6と5.7用に作るエンジニアリング(そしてMongoDBのヒント)
Percona ServerをMySQL 5.6と5.7用に作るエンジニアリング(そしてMongoDBのヒント)Percona ServerをMySQL 5.6と5.7用に作るエンジニアリング(そしてMongoDBのヒント)
Percona ServerをMySQL 5.6と5.7用に作るエンジニアリング(そしてMongoDBのヒント)
 
Capacity planning for your data stores
Capacity planning for your data storesCapacity planning for your data stores
Capacity planning for your data stores
 
The Proxy Wars - MySQL Router, ProxySQL, MariaDB MaxScale
The Proxy Wars - MySQL Router, ProxySQL, MariaDB MaxScaleThe Proxy Wars - MySQL Router, ProxySQL, MariaDB MaxScale
The Proxy Wars - MySQL Router, ProxySQL, MariaDB MaxScale
 
Lessons from {distributed,remote,virtual} communities and companies
Lessons from {distributed,remote,virtual} communities and companiesLessons from {distributed,remote,virtual} communities and companies
Lessons from {distributed,remote,virtual} communities and companies
 
Forking Successfully - or is a branch better?
Forking Successfully - or is a branch better?Forking Successfully - or is a branch better?
Forking Successfully - or is a branch better?
 
MariaDB Server Compatibility with MySQL
MariaDB Server Compatibility with MySQLMariaDB Server Compatibility with MySQL
MariaDB Server Compatibility with MySQL
 
Securing your MySQL / MariaDB Server data
Securing your MySQL / MariaDB Server dataSecuring your MySQL / MariaDB Server data
Securing your MySQL / MariaDB Server data
 
The MySQL Server Ecosystem in 2016
The MySQL Server Ecosystem in 2016The MySQL Server Ecosystem in 2016
The MySQL Server Ecosystem in 2016
 
The Complete MariaDB Server tutorial
The Complete MariaDB Server tutorialThe Complete MariaDB Server tutorial
The Complete MariaDB Server tutorial
 
Best practices for MySQL/MariaDB Server/Percona Server High Availability
Best practices for MySQL/MariaDB Server/Percona Server High AvailabilityBest practices for MySQL/MariaDB Server/Percona Server High Availability
Best practices for MySQL/MariaDB Server/Percona Server High Availability
 
Lessons from database failures
Lessons from database failures Lessons from database failures
Lessons from database failures
 
Lessons from database failures
Lessons from database failuresLessons from database failures
Lessons from database failures
 
Lessons from database failures
Lessons from database failuresLessons from database failures
Lessons from database failures
 
MariaDB 10.1 what's new and what's coming in 10.2 - Tokyo MariaDB Meetup
MariaDB 10.1   what's new and what's coming in 10.2 - Tokyo MariaDB MeetupMariaDB 10.1   what's new and what's coming in 10.2 - Tokyo MariaDB Meetup
MariaDB 10.1 what's new and what's coming in 10.2 - Tokyo MariaDB Meetup
 

Recently uploaded

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 

Recently uploaded (20)

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 

Automated MySQL failover with MHA: Getting started and moving past its quirks

  • 1. Automated MySQL Failover With MHA: Getting Started And Moving Past Its Quirks Colin Charles, Monty Program Ab SkySQL Ab colin@mariadb.org | byte@bytebot.net http://skysql.com/ | http://mariadb.org/ http://bytebot.net/blog/ | @bytebot on Twitter Velocity 2013, Santa Clara, CA, USA 20 June 2013 1
  • 2. whoami Work on MariaDB at Monty Program Ab - Merging with SkySQL Ab Formerly MySQL AB (exit: Sun Microsystems) Past lives include Fedora Project (FESCO), OpenOffice.org MHA experience - since November 2011 (MHA 0.52, 0.53) - NRE work to make it run in a Solaris 10 environment... with no Internet access! - Continued deployment advice + work for data centre use - Much thanks to SkySQL for the experience 2
  • 3. Aims Why MHA? What does MHA do? How does MHA do it? Running MHA, VIP failover, integration, etc Who uses MHA? Is fully automated failover a good idea? 3
  • 4. Why this talk High Performance MySQL, 3rd Edition Published: March 16 2012 4
  • 5. But first... MySQL replication Single master, multiple slave architecture - When master is unavailable, writes stop being accepted Promoting a new master is not that easy - New master needs to wait to apply all relay log events - Slaves need to be consistent - MySQL clients need to reconnect to new master - All slaves need to start replication from proper binlog position 5
  • 6. Where did MHA come from? DeNA won 2011 MySQL Community Contributor of the Year (April 2011) MHA came in about 3Q/2011 Written by Yoshinori Matsunobu, Oracle ACE Director 6
  • 7. What is MHA? MHA for MySQL: Master High Availability Manager tools for MySQL Goal: automating master failover & slave promotion with minimal downtime Set of Perl scripts http://code.google.com/p/mysql-master-ha/ 7
  • 8. Why MHA? Automating monitoring of your replication topology for master failover Scheduled online master switching to a different host for online maintenance - Switch back after OPTIMIZE/ALTER table, software or hardware upgrade - Schema changes without stopping services - pt-online-schema-change, oak-online-alter-table, Facebook OSC Interactive/non-interactive master failover (just for failover, with detection of master failure + VIP takeover to Pacemaker) 8
  • 9. Why is master failover hard? When master fails, no more writes till failover complete MySQL replication is asynchronous (MHA works with async + semi-sync replication) slave2 is latest, slave1+3 have missing events, MHA does: copy id=10 from master if possible apply all missing events 9
  • 10. Semi-sync replication If master crashes (or is shutdown :P), MHA cannot save binlogs, latest data is lost Minimises risk of binlog event existing only on crashed master Guarantees at least one slave receives binlog events at commit http://code.google.com/p/mysql-master-ha/wiki/ UseCases#Using_together_with_Semi- Synchronous_Replication 10
  • 11. MHA: Typical scenario Monitor replication topology If failure detected on master, immediately switch to a candidate master or the most current slave to become new master - MHA must fail to connect to master server three times CHANGE MASTER for all slaves to new master Print (stderr)/email report, stop monitoring 11
  • 12. So really, what does MHA do? 12
  • 13. Typical timeline Usually no more than 10-30 seconds 0-10s: Master failover detected in around 10 seconds - (optional) check connectivity via secondary network (optional) 10-20s: 10 seconds to power off master 10-20s: apply differential relay logs to new master Practice: 4s @ DeNA, usually less than 10s 13
  • 14. How does MHA work? Save binlog events from crashed master Identify latest slave Apply differential relay log to other slaves Apply saved binlog events from master Promote a slave to new master Make other slaves replicate from new master 14
  • 15. Getting Started MHA requires no changes to your application You are of course to write to a virtual IP (VIP) for your master MHA does not build replication environments for you - that’s DIY 15
  • 16. MHA Node Download mha4mysql-node & install this on all machines: master, slaves, monitor Packages (DEB, RPM) available Manually, make sure you have DBD::mysql & ensure it knows the path of your MySQL 16
  • 17. What’s in MHA node save_binary_logs - save & copy master’s binlogs apply_diff_relay_logs - find differential relay log events & apply missing events purge_relay_logs - purge relay log files filter_mysqlbinlog - OBSOLETE + not used by MHA (trims ROLLBACK statements & equivalent binlog events added by mysqlbinlog) 17
  • 18. MHA Manager server Monitor server doesn’t have to be powerful at all, just remain up This is a single-point-of-failure so monitor the manager server where MHA Manager gets installed If MHA Manager isn’t running, your app still runs, but automated failover is now disabled 18
  • 19. MHA Manager You must install mha4mysql-node then mha4mysql- manager Manager server has many Perl dependencies: DBD::mysql, Config::Tiny, Log::Dispatch, Parallel::ForkManager, Time::HiRes Package management fixes dependencies, else use CPAN 19
  • 20. Configuring MHA Application configuration file: see samples/conf/ app1.cnf - Place this in /etc/MHA/app1.cnf Global configuration file: see /etc/MHA/ masterha_default.cnf (see samples/conf/ masterha_default.cnf) 20
  • 21. app1.cnf [server default] manager_workdir=/var/log/masterha/app1 manager_log=/var/log/masterha/app1/manager.log [server1] hostname=host1 [server2] hostname=host2 candidate_master=1 [server3] hostname=host3 [server4] hostname=host4 no_master=1 21 no need to specify master as MHA auto-detects this sets priority, but doesn’t necessarily mean it gets promoted as a default (say its too far behind replication). But maybe this is a more powerful box, or has a better setup will never be the master. RAID0 instead of RAID1+0? Slave is in another data centre?
  • 22. masterha_default.cnf [server default] user=root password=rootpass ssh_user=root master_binlog_dir= /var/lib/mysql,/var/log/mysql remote_workdir=/data/log/masterha ping_interval=3 # secondary_check_script=masterha_secondary_check -s remote_host1 -s remote_host2 # master_ip_failover_script= /script/masterha/master_ip_failover # shutdown_script= /script/masterha/power_manager # report_script= /script/masterha/send_report # master_ip_online_change_script= /script/masterha/master_ip_online_change 22 check master activity from manager->remote_hostN-> master (multiple hosts to ensure its not a network issue) STONITH
  • 23. MHA uses SSH MHA uses SSH actively; passphraseless login - In theory, only require Manager SSH to all nodes However, remember masterha_secondary_check masterha_check_ssh --conf=/etc/MHA/app1.cnf 23
  • 24. Check replication masterha_check_repl --conf=/etc/MHA/app1.cnf If you don’t see MySQL Replication Health is OK, MHA will fail Common errors? Master binlog in different position, read privileges on binary/relay log not granted, using multi-master replication w/o read-only=1 set (only 1 writable master allowed) 24
  • 25. MHA Manager masterha_manager --conf=/etc/MHA/app1.cnf Logs are printed to stderr by default, set manager_log Recommended running with nohup, or daemontools (preferred in production) http://code.google.com/p/mysql-master-ha/wiki/ Runnning_Background 25
  • 26. So, the MHA Playbook Install MHA node, MHA manager masterha_check_ssh --conf=/etc/app1.cnf masterha_check_repl --conf=/etc/app1.cnf masterha_manager --conf=/etc/app1.cnf That’s it! 26
  • 27. master_ip_failover_script Pacemaker can monitor & takeover VIP if required Can use a catalog database - map between application name + writer/reader IPs Shared VIP is easy to implement with minimal changes to master_ip_failover itself (however, use shutdown_script to power off machine) 27
  • 28. master_ip_online_change Similar to master_ip_failover script, but used for online maintenance -masterha_master_switch --master_state=alive MHA executes FLUSH TABLES WITH READ LOCK after the writing freeze 28
  • 29. Test the failover masterha_check_status --conf=/etc/MHA/ app1.cnf Kill MySQL (kill -9, shutdown server, kernel panic) MHA should go thru failover (stderr) - parse the log as well Upon completion, it stops running 29
  • 30. masterha_master_switch Manual failover ---master_state=dead Scheduled online master switchover Great for upgrades to server, etc. -masterha_master_switch --master_state=alive --conf=/etc/MHA/app1.cnf -- new_master_host=host2 30
  • 31. Handling VIPs my $vip = ‘192.168.0.1/24”; my $interface = “0”; my $ssh_start_vip = “sudo /sbin/ifconfig eth0:$key $vip”; my $ssh_stop_vip = “sudo /sbin/ifconfig eth0:$key down”; ... sub start_vip() { `ssh $ssh_user@$new_master_host ” $ssh_start_vip ”`; } sub stop_vip() { `ssh $ssh_user@$orig_master_host ” $ssh_stop_vip ”`; } 31
  • 32. Integration with other HA solutions Pacemaker - on RHEL6, you need some HA add-on, just use the CentOS packages /etc/ha.d/haresources to configure VIP -`masterha_master_switch --master_state=dead --interactive=0 --wait_on_failover_error=0 --dead_master_host=host1 -- new_master_host=host2` Corosync + Pacemaker works well 32
  • 33. Solaris 10 MHA tested to work on Linux and Solaris 10 & greater Use a .pkg MySQL Solaris 10 needs a compiler (SolarisStudio - register w/Oracle to download) CPAN: manual builds of dependencies Solaris doesn’t have md5sum (call md5) Solaris ssh isn’t OpenSSH, missing some features like ConnectionTimeout 33
  • 34. What about replication delay? By default, MHA checks to see if slave is behind master. By more than 100MB, it is never a candidate slave If you have candidate_master=1 set, consider setting check_repl_delay=0 You can integrate it with pt-heartbeat from Percona Toolkit - http://www.percona.com/doc/percona-toolkit/2.1/pt- heartbeat.html 34
  • 35. MHA deployment tips  You really should install this as root  SSH needs to work across all hosts  If you don’t want plaintext passwords in config files, use init_conf_load_script  Each monitor can monitor multiple MHA pairs (hence app1, app2, etc.)  You can have a standby master, make sure its read-only  By default, master1->master2->slave3 doesn’t work - MHA manages master1->master2 without issue - Use multi_tier_slave=1 option  Make sure replication user exists on candidate master too! 35
  • 36. Alternate solutions Heartbeat + DRBD - Costs $$$ -> passive master - innodb_flush_log_at_trx_commit=1, sync_binlog=1 is expensive in MySQL - http://www.mysqlperformanceblog.com/2008/06/02/how-much-overhead-drdb-could- cause/ - MariaDB 5.3/5.5 or Percona Server 5.5 or MySQL 5.6 mitigate this by having group commit in the binary log 36
  • 37. Alternate solutions II MySQL NDB Cluster Galera Cluster - MariaDB Galera Cluster, Percona XtraDB Cluster Percona Replication Manager (PRM) - https://raw.github.com/y-trudeau/resource-agents-prm/ master/heartbeat/mysql Tungsten Replicator mysqlfailover from mysql-utilities using GTID’s in 5.6 37
  • 38. Where is MHA used DeNA Premaccess (Swiss HA hosting company) Ireland’s national TV & radio service Jetair Belgium (MHA + MariaDB!) Samsung SK Group DAPA Facebook 38
  • 39. MHA 0.55, 0.56 comes soon MHA 0.55 is released December 18 2012 - It is OK for node to be 0.54 in this situation, due to bug in manager only http://code.google.com/p/mysql-master-ha/wiki/ReleaseNotes 0.56 coming with: - MySQL 5.6 support for checksum in binlog events - mysqlbinlog and mysql in custom locations (configurable clients) - MHA resource agent shipped 39
  • 40. Is fully automated failover a good idea?  False alarms - Can cause short downtime, restarting all write connections  Repeated failover - Problem not fixed? Master overloaded? - MHA ensures a failover doesn’t happen within 8h, unless -- last_failover_minute=n is set  Data loss - id=103 is latest, relay logs are at id=101, loss - group commit means sync_binlog=1, innodb_flush_log_at_trx_commit=1 can be enabled! (just wait for master to recover)  Split brain - sometimes poweroff takes a long time 40
  • 41. Support SkySQL: www.skysql.com Percona: www.percona.com PalominoDB: www.palominodb.com AccelerationDB: www.accelerationdb.com - training provider as well 41
  • 42. Automated tools to play with 4-host Vagrant setup for MySQL MHA - https://github.com/hholzgra/vagrant-mysql-mha Palomino Cluster Tool - https://github.com/time-palominodb/PalominoClusterTool - Ansible playbooks for MHA 4-host Vagrant setup for MHA - https://github.com/lefred/vagrant-mha 42
  • 43. Video resources Yoshinori Matsunobu talking about High Availability & MHA at Oracle MySQL day - http://www.youtube.com/watch?v=CNCALAw3VpU Alex Alexander (AccelerationDB) talks about MHA, with an example of failover, and how it compares to Tungsten - http://www.youtube.com/watch?v=M9vVZ7jWTgw 43
  • 44. References Design document - http://www.slideshare.net/matsunobu/automated-master- failover Configuration parameters - http://code.google.com/p/mysql-master-ha/wiki/Parameters JetAir MHA use case - http://www.percona.com/live/mysql-conference-2012/sessions/ case-study-jetair-dramatically-increasing-uptime-mha MySQL binary log - http://dev.mysql.com/doc/internals/en/binary-log.html 44
  • 45. Thank you - Q&A Colin Charles, Monty Program Ab SkySQL Ab colin@mariadb.org | byte@bytebot.net http://skysql.com/ | http://mariadb.org/ http://bytebot.net/blog/ | @bytebot on Twitter Velocity 2013, Santa Clara, CA, USA 20 June 2013 45