A MySQL fault switching method and device based on sentinel mechanism
The MySQL slave database node is selected as the new master database node through sentinel mechanism monitoring and scoring, which solves the problems of simple MySQL failover logic and insufficient metadata update in the existing technology, achieves high-quality service switching and metadata update, and ensures that MySQL Stability and availability of the Service.
Patent Information
- Application Number
- CN202010582351.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-06-23
- Publication Date
- 2024-10-29
- Estimated Expiration
- 2040-06-23
AI Technical Summary
The existing MySQL failover method cannot modify metadata and does not support semi-synchronous replication mode. The logic of selecting a new master node is too simple and cannot be selected based on the load, memory and disk type of the slave database, resulting in unguaranteed service quality. , and failover is not possible when the network is partitioned.
The MySQL main database node is regularly monitored through the sentinel mechanism, and the slave database node with the fastest replication speed and optimal resources is selected as the new main database node based on the resource information scores of multiple sentinel nodes, and metadata is updated to ensure service quality and faults. The switch went smoothly.
Select the most suitable new main database node through comprehensive judgment to ensure service quality, and update metadata in a timely manner during failover to avoid service interruptions caused by machine reasons.
Smart Images

Figure CN111831489B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of data processing, and in particular to a MySQL fault switching method and device based on a sentinel mechanism. Background Art
[0002] MHA (Master High Availability) is a MySQL failover solution written by MySQL experts in Perl to ensure high availability of the database. Its function is to achieve master MySQL failover within 10-30 seconds.
[0003] MHA failover can help us solve the consistency problem of slave database data after the master node goes down, and maximize the recovery of data after the failure. There are two roles in MHA, one is the node node and the other is the manager node. To implement this MHA, at least three database servers are required, one master and multiple backups, that is, one serves as the master node, one serves as the backup machine of the master node, and the other is a slave machine. It should be noted that once the master server goes down, the backup machine will start to serve as the master node. If the master server is online, it will no longer be the master node, because if so, the consistency of the database will be changed.
[0004] In the process of implementing the present invention, the inventors found that there are at least the following problems in the prior art:
[0005] 1. The existing MySQL failover cannot modify metadata, does not support failover in semi-synchronous replication mode, and the logic of selecting a new master node in failover is too simple and cannot be selected based on the load, memory, and disk type of the machine where the slave database is located. As a result, the newly selected master node may still not meet our service standards due to machine reasons, resulting in our inability to guarantee the quality of MySQL service.
[0006] 2. When a network partition occurs, the existing MHA only has one manager node. If this manager node is in the same partition as the MySQL master node, no failover will be performed. This will cause other partitions to be unable to access the MySQL service.
[0007] 3. When it is known in advance that the MySQL master database node will be unable to provide services, such as due to adjustments to the computer room or replacement of switches, the MySQL nodes need to be manually switched to other unaffected machines in advance. However, these MySQL nodes cannot be switched without metadata information. Summary of the invention
[0008] The embodiment of the present invention provides a MySQL fault switching method and device based on a sentinel mechanism. Through the sentinel mechanism, a comprehensive judgment is made on the information collected by the machine information, and a MySQL node that is most suitable to become a new master node is selected.
[0009] To achieve the above object, on the one hand, an embodiment of the present invention provides a MySQL failover method based on a sentinel mechanism, the method comprising:
[0010] Regularly monitor the MySQL master database node through the sentinel node. When a sentinel node finds that the MySQL master database node has failed, it instructs other sentinel nodes to determine whether the MySQL master database node has failed;
[0011] If more than a set number of sentinel nodes determine that the MySQL master database node fails, obtain the resource information score value provided by each sentinel node based on the resource information of its corresponding MySQL cluster database node;
[0012] Determine the MySQL cluster database node with the fastest replication speed, and copy the difference logs between the replication point of the MySQL cluster database node with the fastest replication speed and the corresponding replication points of each other MySQL cluster database node to each corresponding MySQL cluster database node and execute;
[0013] The MySQL cluster library node with the largest resource information score value is determined to be the new MySQL master library node, and based on the difference log of the new MySQL master library node, other MySQL cluster library nodes are pointed to the new MySQL master library node, and the metadata information corresponding to the MySQL master library node switching is modified.
[0014] On the other hand, an embodiment of the present invention provides a MySQL fault switching device based on a sentinel mechanism, the device comprising:
[0015] A judgment unit, used to regularly monitor the MySQL master database node through a sentinel node, and when a sentinel node finds that the MySQL master database node fails, instructs other sentinel nodes to judge whether the MySQL master database node fails;
[0016] A scoring value acquisition unit is used to obtain a resource information scoring value provided by each sentinel node according to the resource information of the corresponding MySQL cluster node if more than a set number of sentinel nodes determine that the MySQL master database node fails;
[0017] The synchronous replication unit is used to determine the MySQL cluster database node with the fastest replication speed, and to copy the difference logs between the replication point of the MySQL cluster database node with the fastest replication speed and the corresponding replication points of each other MySQL cluster database node to each corresponding MySQL cluster database node and execute them;
[0018] The switching unit is used to determine that the MySQL cluster library node with the largest resource information score value is the new MySQL master library node, and point other MySQL cluster library nodes to the new MySQL master library node according to the difference log of the new MySQL master library node, and modify the metadata information corresponding to the MySQL master library node when switching.
[0019] The above technical solution has the following beneficial effects:
[0020] The technical solution of the present invention uses a MySQL fault switching method based on a sentinel mechanism. By making a comprehensive judgment on the information collected from the machine information, the MySQL node that is most suitable to become the new master library node is selected to ensure that the new MySQL master library node will not still fail to meet our service standards due to machine reasons, thereby ensuring the service quality; and through the sentinel mechanism, when the MySQL master library node is switched due to a fault, the metadata information is updated in time to ensure the smooth switching of the MySQL master library node. BRIEF DESCRIPTION OF THE DRAWINGS
[0021] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0022] Figure 1 It is a flow chart of a MySQL fault switching method based on a sentinel mechanism according to an embodiment of the present invention;
[0023] Figure 2 It is a structural diagram of a MySQL fault switching device based on a sentinel mechanism according to an embodiment of the present invention. DETAILED DESCRIPTION
[0024] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0025] like Figure 1 FIG. 1 is a flowchart of a MySQL fault switching method based on a sentinel mechanism according to an embodiment of the present invention, wherein the method comprises:
[0026] S101: Regularly monitor the MySQL master database node through the sentinel node. When a sentinel node finds that the MySQL master database node fails, it instructs other sentinel nodes to determine whether the MySQL master database node fails.
[0027] Prior to this, further, it also includes that in the network architecture of the computer rooms in two locations and three centers, more than three sentinel nodes are deployed in each computer room to ensure normal switching when network partition occurs; and all MySQL node information is configured in the sentinel node and metadata information.
[0028] Preferably, the periodic monitoring of the MySQL master database node by the sentinel node includes: periodically sending a heartbeat PING to the MySQL master database node by the sentinel node, and determining whether the MySQL master database node fails according to the response of the MySQL master database node to the heartbeat PING. When a sentinel finds that a MySQL master database node fails, it starts to ask other sentinel nodes to determine whether the MySQL master database node fails.
[0029] Further preferably, judging whether the MySQL master library node fails based on the response of the MySQL master library node to the heartbeat PING includes: when the response of the MySQL master library node to the heartbeat PING is an error, or the response of the MySQL master library node to the heartbeat PING is not received within a set time range, then judging that the MySQL master library node fails.
[0030] S102: If more than a set number of sentinel nodes determine that the MySQL master database node fails, obtain a resource information score value provided by each sentinel node based on the resource information of its corresponding MySQL cluster database node. Preferably, the resource information includes: CPU load, memory usage, disk capacity, disk type, and disk IO load usage.
[0031] Specifically, for example, if more than half of the sentinel nodes believe that the MySQL master node is faulty, they will start preparing for the switch. The sentinel will score multiple MySQL slave nodes by collecting the CPU load, memory usage, disk capacity, disk type, and disk IO load usage of each MySQL slave node, and the MySQL slave node with the highest score will be selected as the new MySQL master node.
[0032] S103: Determine the MySQL cluster library node with the fastest replication speed, and copy the difference log between the replication point of the MySQL cluster library node with the fastest replication speed and the corresponding replication point of each other MySQL cluster library node to each corresponding MySQL cluster library node and execute.
[0033] Because the new MySQL master node may not be the fastest replicating node, we need to obtain the fastest replicating MySQL slave node, obtain its replication point, and then compare the difference between these replication points and the replication points of other slave nodes to obtain the corresponding difference logs. Copy these difference logs to each corresponding MySQL slave node. After executing these difference logs, the replication status of all MySQL slave nodes will be consistent. Specifically, by executing the show slave status command on each slave, the values of the two variables Master_Log_File and Read_Master_Log_Pos are obtained. By comparing the sizes of these two values, the MySQL slave node with the fastest replication speed is selected. Then, by comparing the difference between Master_Log_File and Read_Master_Log_Pos of other slaves and the slave with the fastest replication speed, the corresponding relay log (relay log) or difference log is generated from the Relay_Log_File of the slave with the fastest replication speed. Then, copy these logs to the corresponding MySQL slave nodes for execution, and the replication differences between each slave can be completed.
[0034] S104: Determine the MySQL cluster library node with the largest resource information score value as the new MySQL master library node, and point other MySQL cluster library nodes to the new MySQL master library node based on the difference log of the new MySQL master library node, and modify the metadata information corresponding to the MySQL master library node switch.
[0035] Specifically, according to the replication information of the new MySQL master library node, other MySQL slave library nodes are pointed to the new MySQL master library node to form a new topology structure; according to the new topology structure, the metadata information is modified.
[0036] Corresponding to the above method, such as Figure 2 FIG. 1 is a schematic diagram of a structure of a MySQL fault switching device based on a sentinel mechanism according to an embodiment of the present invention, wherein the device comprises:
[0037] The judging unit 21 is used to regularly monitor the MySQL master database node through the sentinel node. When a sentinel node finds that the MySQL master database node fails, it instructs other sentinel nodes to judge whether the MySQL master database node fails;
[0038] The scoring value acquisition unit 22 is used to obtain the resource information scoring value provided by each sentinel node according to the resource information of the corresponding MySQL cluster node if more than a set number of sentinel nodes determine that the MySQL master database node fails;
[0039] The synchronous replication unit 23 is used to determine the MySQL cluster database node with the fastest replication speed, and to copy the difference logs between the replication point of the MySQL cluster database node with the fastest replication speed and the corresponding replication points of each other MySQL cluster database node to each corresponding MySQL cluster database node and execute them;
[0040] The switching unit 24 is used to determine that the MySQL cluster library node with the largest resource information score value is the new MySQL master library node, and point other MySQL cluster library nodes to the new MySQL master library node according to the difference log of the new MySQL master library node, and modify the metadata information corresponding to the MySQL master library node when switching.
[0041] Furthermore, it also includes a sentinel setting unit, which is used to deploy more than three sentinel nodes in each computer room in the computer room network architecture of two locations and three centers, and configure all MySQL node information in the sentinel nodes and metadata information.
[0042] Preferably, the judgment unit 21 includes a monitoring module, which is used to periodically send a heartbeat PING to the MySQL master database node through a sentinel node, and judge whether the MySQL master database node fails according to the response of the MySQL master database node to the heartbeat PING.
[0043] Further preferably, the monitoring module is specifically used to: when the response of the MySQL master library node to the heartbeat PING is erroneous, or the response of the MySQL master library node to the heartbeat PING is not received within a set time range, it is determined that the MySQL master library node has failed.
[0044] Preferably, the resource information includes: CPU load, memory usage, disk capacity, disk type and disk IO load usage.
[0045] The present invention deploys sentinel nodes in multiple central computer rooms. When each sentinel node finds that a MySQL master database node fails, if most of the sentinels believe that the MySQL master database node has failed, a failover will be started.
[0046] After confirming the failure of the MySQL master node, in order to ensure that the new MySQL master node can provide high-quality services and ensure that the utilization rate of all servers is above an average level, we will score multiple MySQL slave nodes based on the CPU load, memory usage, disk capacity, disk type, and disk IO load usage of all the machines where the MySQL slave nodes are located, and select the most suitable MySQL slave node for failover.
[0047] Through the sentinel mechanism, a sentinel is deployed in each center of the computer room network architecture with two locations and three centers, preventing the MySQL service from being unable to fail over in the event of a network partition.
[0048] By collecting the machine's CPU load, memory usage, disk capacity, disk type, and disk IO load usage, multiple slave nodes are scored and the most suitable slave node to become the new master node is selected.
[0049] Subscribe to the Sentinel's switching information to maintain the metadata of the existing system, so that the DBA can maintain the MySQL service in advance when adjusting the computer room and the switch without being affected.
[0050] It should be understood that the specific order or hierarchy of steps in the disclosed process is an example of an exemplary method. Based on design preferences, it should be understood that the specific order or hierarchy of steps in the process can be rearranged without departing from the scope of protection of the present disclosure. The attached method claims present the elements of the various steps in an exemplary order and are not intended to be limited to the specific order or hierarchy described.
[0051] In the above detailed description, various features are grouped together in a single embodiment to simplify the disclosure. This method of disclosure should not be interpreted as reflecting an intention that the embodiments of the claimed subject matter require more features than are clearly stated in each claim. On the contrary, as reflected in the appended claims, the invention is in a state of having less than all the features of the disclosed individual embodiments. Therefore, the appended claims are hereby expressly incorporated into the detailed description, with each claim standing on its own as a separate preferred embodiment of the invention.
[0052] The disclosed embodiments are described above to enable any person skilled in the art to implement or use the present invention. Various modifications of these embodiments are obvious to those skilled in the art, and the general principles defined herein may also be applied to other embodiments without departing from the spirit and scope of the present disclosure. Therefore, the present disclosure is not limited to the embodiments given herein, but is consistent with the broadest scope of the principles and novel features disclosed in this application.
[0053] The above description includes examples of one or more embodiments. Of course, it is impossible to describe all possible combinations of components or methods for the purpose of describing the above embodiments, but it should be recognized by those skilled in the art that the various embodiments may be further combined and arranged. Therefore, the embodiments described herein are intended to cover all such changes, modifications and variations that fall within the scope of protection of the appended claims. In addition, with respect to the term "comprising" used in the specification or claims, the word is covered in a manner similar to the term "including", just as "including," is explained as a transitional word in the claims. In addition, any term "or" used in the specification of the claims is intended to mean "non-exclusive or".
[0054] The specific implementation methods described above further illustrate the objectives, technical solutions and beneficial effects of the present invention in detail. It should be understood that the above description is only a specific implementation method of the present invention and is not intended to limit the scope of protection of the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present invention should be included in the scope of protection of the present invention.
Claims
1. A MySQL failover method based on a sentinel mechanism, characterized in that: include: Regularly monitor the MySQL master database node through the sentinel node. When a sentinel node finds that the MySQL master database node has failed, it instructs other sentinel nodes to determine whether the MySQL master database node has failed; If more than a set number of sentinel nodes determine that the MySQL master database node fails, obtain the resource information score value provided by each sentinel node based on the resource information of its corresponding MySQL cluster database node; Determine the MySQL cluster database node with the fastest replication speed, and copy the difference logs between the replication point of the MySQL cluster database node with the fastest replication speed and the corresponding replication points of each other MySQL cluster database node to each corresponding MySQL cluster database node and execute; Determine the MySQL cluster library node with the largest resource information score value as the new MySQL master library node, and point other MySQL cluster library nodes to the new MySQL master library node according to the difference log of the new MySQL master library node, and modify the metadata information corresponding to the MySQL master library node switching; The resource information includes: CPU load, memory usage, disk capacity, disk type, and disk IO load usage.
2. The MySQL failover method based on the sentinel mechanism as claimed in claim 1, characterized in that: It also includes that in the network architecture of the computer rooms in two locations and three centers, more than three Sentinel nodes are deployed in each computer room, and all MySQL node information is configured in the Sentinel nodes and metadata information.
3. The MySQL failover method based on the sentinel mechanism as claimed in claim 2, characterized in that: The sentinel node is used to regularly monitor the MySQL master database node, including: The sentinel node regularly sends a heartbeat PING to the MySQL master database node, and determines whether the MySQL master database node fails based on the response of the MySQL master database node to the heartbeat PING.
4. The MySQL failover method based on the sentinel mechanism as claimed in claim 3, characterized in that: The determining whether the MySQL master database node fails according to the response of the MySQL master database node to the heartbeat PING includes: When the response of the MySQL master database node to the heartbeat PING is an error, or the response of the MySQL master database node to the heartbeat PING is not received within a set time range, it is determined that the MySQL master database node has failed.
5. A MySQL fault switching device based on a sentinel mechanism, characterized in that: include: A judgment unit, used to regularly monitor the MySQL master database node through a sentinel node, and when a sentinel node finds that the MySQL master database node fails, instructs other sentinel nodes to judge whether the MySQL master database node fails; A scoring value acquisition unit is used to obtain a resource information scoring value provided by each sentinel node according to the resource information of the corresponding MySQL cluster node if more than a set number of sentinel nodes determine that the MySQL master database node fails; The synchronous replication unit is used to determine the MySQL cluster database node with the fastest replication speed, and to copy the difference logs between the replication point of the MySQL cluster database node with the fastest replication speed and the corresponding replication points of each other MySQL cluster database node to each corresponding MySQL cluster database node and execute them; A switching unit is used to determine the MySQL cluster library node with the largest resource information score value as the new MySQL master library node, and point other MySQL cluster library nodes to the new MySQL master library node according to the difference log of the new MySQL master library node, and modify the metadata information corresponding to the MySQL master library node when switching; The resource information includes: CPU load, memory usage, disk capacity, disk type, and disk IO load usage.
6. The MySQL fault switching device based on the sentinel mechanism as claimed in claim 5, characterized in that: It also includes a sentinel setting unit, which is used to deploy more than three sentinel nodes in each computer room in the network architecture of the computer rooms in two locations and three centers, and configure all MySQL node information in the sentinel nodes and metadata information.
7. The MySQL fault switching device based on the sentinel mechanism as claimed in claim 6, characterized in that: The judgment unit includes a monitoring module, which is used to periodically send a heartbeat PING to the MySQL master database node through a sentinel node, and judge whether the MySQL master database node fails according to the response of the MySQL master database node to the heartbeat PING.
8. The MySQL fault switching device based on the sentinel mechanism as claimed in claim 7, characterized in that: The monitoring module is specifically used to determine that the MySQL master database node has failed when the response of the MySQL master database node to the heartbeat PING is an error, or the response of the MySQL master database node to the heartbeat PING is not received within a set time range.
Citation Information
Patent Citations
Fault processing method and device of distributed cache
CN106209447A
Hierarchical-database high-availability system based on guard model
CN107016087A
