Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of MHA Source Code in MySQL

2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

Editor to share with you a sample analysis of the MHA source code in MySQL, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

There are many high-availability solutions for MySQL, and MHA is one of the most popular ones. At present, the latest version is 0.57. it is divided into two parts, one is Water himanager, the other is node.

Learning about an open source project, reading the source code is a good start, so MHA has become a key content for me to learn. Let's start with manager, of course, because this open source project is based on perl development, we can check it in the development IDE to make it more efficient, such as downloading a plug-in using eclipse.

The situation of the whole project is as follows: under the bin directory is the executable perl script, and the referenced package logic is in the. pm file under lib.

For node nodes, it is a similar way, except that the logical content of node nodes is much less, mainly focused on the processing of binlog.

With MHA, starting and stopping manager is a basic entry, so we can look at it in masterha_manager. You will find that the script is actually divided into two main parts, masterMonitor and MasterFailover. We will gradually understand it through the main method.

Let's switch to MasterMonitor and look at the contents of the main method.

If it is in IDE, it is easy to see a structural overview of the corresponding .pm file MasterMonitor.pm, starting with the main method.

The core logic of the whole Monitor is the following method. This logic is a mechanism for detecting heartbeats in a loop. Among them, wait_until_master_is_dead is an important method. You can continue to look in this direction.

Another important script is masterha_master_switch, which mainly completes the switching. If you look at the basic structure of the script, you will find that by detecting the status of the master node, you will call MasterFailover and MasterRotate to deal with it.

The logic of failover is as follows. We can look at the corresponding pm file MasterFailover.pm to see the logic. The overall logic, as shown in the following figure, is divided into several stages. There is an overall verification step for each stage.

As you can see from the above section, during the whole switching process, the backend will perform a series of logical checks, such as finding the latest slave node and how to complete the log information (in essence, it is processed through mysqlbinlog based on timestamp)

On this basis, through the source code, we can see a lot of details that we have not paid attention to, such as if we switch again within 8 hours, it will be thrown wrong directly.

The above is all the content of this article "sample Analysis of MHA Source Code in MySQL". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report