CN106202075B - Method and device for switching between main database and standby database - Google Patents

Method and device for switching between main database and standby database Download PDF

Info

Publication number
CN106202075B
CN106202075B CN201510213694.1A CN201510213694A CN106202075B CN 106202075 B CN106202075 B CN 106202075B CN 201510213694 A CN201510213694 A CN 201510213694A CN 106202075 B CN106202075 B CN 106202075B
Authority
CN
China
Prior art keywords
database
data
switching
current
main
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Active
Application number
CN201510213694.1A
Other languages
Chinese (zh)
Other versions
CN106202075A (en
Inventor
郭龙波
陈河堆
白涛
张宗禹
汪彦舒
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Jinzhuan Xinke Co Ltd
Original Assignee
ZTE Corp
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by ZTE Corp filed Critical ZTE Corp
Priority to CN201510213694.1A priority Critical patent/CN106202075B/en
Priority to PCT/CN2015/089553 priority patent/WO2016173179A1/en
Publication of CN106202075A publication Critical patent/CN106202075A/en
Application granted granted Critical
Publication of CN106202075B publication Critical patent/CN106202075B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor

Abstract

The embodiment of the invention provides a method and a device for switching between main and standby databases, wherein the method comprises the following steps: monitoring the operation of a first database serving as a current main database, and sending a standby switching main request to a second database serving as a current standby database when the abnormal operation of the first database is detected; acquiring feedback data reported when the second database detects that the data in the second database is consistent with the data in the first database and the second database is successfully switched to the current master database, and acquiring data switching information of the second database which is finally synchronized with the first database according to the feedback data; and sending a main switching request containing the data switching information to the first database, so that the first database is switched to the current standby database when judging that the data synchronized to the second database is not lost according to the data switching information.

Description

Method and device for switching between main database and standby database
Technical Field
The present invention relates to the field of databases, and in particular, to a method and an apparatus for switching between a master database and a slave database.
Background
In the application of the database, the database has high concurrency and high data growth speed, which are two major characteristics of the database business at present. In a short time, there may be a large amount of data additions or modifications. With the increasing magnitude of data, from bytes, kilobytes and megabytes to ten-megabytes at present, the distributed database based on the database is widely applied in combination with the good transverse expansion characteristic of the database. Meanwhile, in order to improve disaster tolerance of the database, each database provider provides corresponding strategies, wherein a master-slave replication strategy has the core idea that a master database provides service to the outside, a binary log generated by the operation of the master database is read from the database at intervals, and relevant actions in the binary log are executed, so that the data consistency of the master database and the slave database is ensured. When a database host is down and needs to be restored in a short time, a slave needs to be used as a master database (i.e., master-slave switching), how to judge whether the data of the slave database and the data of the host database are consistent or not and how to restore the data of the master database and the slave database to be consistent in a short time become a technical problem in the industry.
The master-slave copy is that the backup machine copies the binary log of the host machine, and the master-slave log is necessarily asynchronous due to the factors such as the log writing delay of the host machine, the network communication delay and the like. However, in the current industry, there is no tool for automatically comparing the data consistency of the main and standby databases, and it is only possible to know whether the data of the main and standby databases are consistent by manually executing database statements, and when the main and standby databases are down and the main and standby databases need to be switched, it is only possible to determine the difference condition of the main and standby binary logs by checking the binary logs of the databases, and manually extract the corresponding logs from the main computer, restore the corresponding logs into corresponding database statements and insert the corresponding database statements into the standby computer for execution, so as to implement the data synchronization of the main and standby computers. The process is very complicated, the labor cost is high, the reaction speed is slow, and the error rate is high.
Disclosure of Invention
The invention provides a method and a device for switching between a main database and a standby database, and aims to solve the problems of complex process, high labor cost, low reaction speed and high error rate of switching between the main database and the standby database.
In order to achieve the above object, an embodiment of the present invention provides a method for switching between active and standby databases, which is applied to a management server, and includes:
monitoring the operation of a first database serving as a current main database, and sending a standby switching main request to a second database serving as a current standby database when the abnormal operation of the first database is detected;
acquiring feedback data reported when the second database detects that the data in the second database is consistent with the data in the first database and the second database is successfully switched to the current master database, and acquiring data switching information of the second database which is finally synchronized with the first database according to the feedback data;
and sending a main switching request containing the data switching information to the first database, so that the first database is switched to the current standby database when judging that the data synchronized to the second database is not lost according to the data switching information.
Further, the step of monitoring the operation of the first database as the current master database comprises:
and acquiring heartbeat information periodically reported by the first database, and determining that the first database runs abnormally when the heartbeat information is not received at preset time intervals.
Further, after the step of detecting when the first database operates abnormally, the method further comprises:
acquiring a first position of a current binary log position used for recording data execution actions in the first database according to the heartbeat information reported by the first database for the last time;
and sending the current binary log position first position of the first database to the second database while sending the backup main request to the second database.
Further, in the step of obtaining the data switching information that the second database is synchronized with the first database at last according to the feedback data:
acquiring a second position of a binary log position of the second database synchronized with the first data finally according to the feedback data;
and sending the second database to the first database from the second position of the binary log position synchronized with the first database at the last time when the master-cut-off request is sent to the first database.
Further, the step of obtaining, according to the heartbeat information reported by the first database for the last time, a first position of a current binary log position in the first database for recording a data execution action specifically includes:
acquiring a third position of a binary log position for recording data execution actions in the heartbeat information for the last time;
detecting whether a link with the first database exists:
if the binary log exists, sending a request for acquiring the current binary log position to the first database, and acquiring the first position of the current binary log position from the feedback message of the first database; if the heartbeat information does not exist, marking the third position of the binary log position of the recorded data execution action in the heartbeat information at the last time as the first position of the current binary log position.
The embodiment of the invention also provides a method for switching between main and standby databases, which is applied to a first database serving as a current main database and comprises the following steps:
receiving a main switching request sent by a management server when the first database is abnormally operated;
acquiring data switching information synchronized from the first database when a second database serving as a current standby database is switched to a main database from the main switching request;
judging whether the data synchronized to the second database is lost or not according to the data switching information, and switching to a current standby database when the data is judged not to be lost; and when the data is judged to be lost, prompting that the switching fails.
Further, before the step of receiving a master-standby request sent by a management server when the first database is abnormally operated, the method further includes:
and reporting heartbeat information to the management server periodically, so that the management server detects whether the first database operates abnormally according to the heartbeat information.
Further, when the second database serving as the current standby database is switched to the master database from the master switching request, the step of finally obtaining the data switching information synchronized with the first database includes:
and acquiring a second position of the binary log position of the second database synchronized from the first database finally from the main backup request.
Further, the step of determining whether the data synchronized to the second database is lost according to the data switching information includes: acquiring a fourth position of the binary log after the first database recovers normal heartbeat;
comparing the fourth position with the second position, and if the fourth position is less than or equal to the second position, determining that the data synchronized to the second database is not lost, and switching to the current standby database; and if the fourth position is larger than the second position, determining that the data synchronized to the second database is lost and has a difference with the data of the first database, and prompting that the switching fails.
Further, after the step of prompting the handover failure when it is determined that the data is lost, the method further includes:
detecting a start file of a differentiated SQL script between the first database and the second database;
and traversing from the second position of the binary log position in the data of the first database until all log files are obtained, generating SQL sentences of all transactions, and redirecting to a target text file.
The embodiment of the invention also provides a method for switching the main database and the standby database, which is applied to a second database serving as the current standby database and comprises the following steps:
acquiring a backup switching main request sent by a management server when monitoring that a first database serving as a current main database is abnormal;
acquiring the current data information of the first database from the backup main request, comparing the current data information with the current data information of the second database, and judging whether the data are consistent; and when the data are consistent, switching to the current master database, and when the data are inconsistent, prompting that the switching to the current master database fails.
Further, the step of obtaining the current data information of the first database from the backup main request and comparing the current data information of the first database with the current data information of the second database includes:
acquiring a first position of a current binary log position of the first database from the backup main request;
acquiring a fifth position of a current binary log position of the data copied from the first database by the second database;
comparing the first position with the fifth position, if the first position is equal to the fifth position, determining that the current data information of the first database is consistent with the current data information of the second database, and switching to a current main database; and if the first position is smaller than the fifth position, determining that the current data information of the first database is inconsistent with the current data information of the second database, and prompting that the switching to the current main database fails.
The embodiment of the invention also provides a device for switching the main database and the standby database, which is applied to a management server, and the device comprises:
the system comprises a monitoring module, a backup switching module and a backup switching module, wherein the monitoring module is used for monitoring the operation of a first database serving as a current master database, and sending a backup switching master request to a second database serving as a current backup database when detecting that the operation of the first database is abnormal;
a first obtaining module, configured to obtain feedback data reported when the second database is successfully switched to the current master database when the second database detects that data in the second database is consistent with data in the first database, and obtain, according to the feedback data, data switching information that is synchronized with the first database at last by the second database;
and the first data sending module is used for sending a main switching request containing the data switching information to the first database, so that the first database is switched to the current standby database when the first database judges that the data synchronized to the second database is not lost according to the data switching information.
Further, the monitoring module includes:
and the first monitoring submodule is used for acquiring heartbeat information reported by the first database regularly, and determining that the first database runs abnormally when the heartbeat information is not received at preset time intervals.
Further, the apparatus further comprises:
the first log obtaining module is used for obtaining a first position of a current binary log position used for recording data execution actions in the first database according to the heartbeat information reported by the first database at the last time;
the monitoring module sends a standby main request to the second database and sends a first position of a current binary log position of the first database to the second database.
Further, the first obtaining module comprises:
the first log obtaining unit is used for obtaining a second position of a binary log position of the second database which is synchronized with the first data from the first database at last according to the feedback data;
the first data sending module sends a master switching request to the first database, and sends the second database to the first database from the second position of the binary log position synchronized with the first database at the last time.
Further, the first log obtaining module further comprises:
the second log obtaining unit is used for obtaining a third position of a binary log position of a recorded data execution action in the heartbeat information at the last time;
a first detection unit for detecting whether a link with the first database exists:
if the binary log exists, sending a request for acquiring the current binary log position to the first database, and acquiring the first position of the current binary log position from the feedback message of the first database; if the heartbeat information does not exist, marking the third position of the binary log position of the recorded data execution action in the heartbeat information at the last time as the first position of the current binary log position.
Further, the apparatus is applied to a first database as a current master database, and comprises:
the first receiving module is used for receiving a main backup switching request sent by a management server when the first database is detected to be abnormal in operation;
a second obtaining module, configured to obtain, from the master-slave switching request, data switching information synchronized with the first database when a second database serving as a current slave database is switched to the master database;
the first judging module is used for judging whether the data synchronized to the second database is lost or not according to the data switching information, and switching to the current standby database when the data is judged not to be lost; and when the data is judged to be lost, prompting that the switching fails.
Further, the first receiving module comprises:
and the first reporting submodule is used for reporting heartbeat information to the management server periodically so that the management server detects whether the first database operates abnormally according to the heartbeat information.
Further, the second obtaining module includes:
and the first obtaining submodule is used for obtaining a second position of a binary log position of the second database which is synchronized with the first database from the main switching request.
Further, the first determination module includes:
the second obtaining submodule is used for obtaining a fourth position of the binary log after the normal heartbeat of the first database is recovered;
the first judgment submodule is used for comparing the fourth position with the second position, and if the fourth position is less than or equal to the second position, determining that data synchronized to the second database is not lost, and switching to the current standby database; and if the fourth position is larger than the second position, determining that the data synchronized to the second database is lost and has a difference with the data of the first database, and prompting that the switching fails.
Further, the apparatus further comprises:
the first detection module is used for detecting a start file of a differentiated SQL script between the first database and the second database;
and the first generation module is used for traversing from the second position of the binary log position in the data of the first database to all log files, generating SQL sentences of all transactions and redirecting to a target text file.
The embodiment of the invention provides a device for switching between a main database and a standby database, which is applied to a second database serving as a current standby database, and comprises the following components:
the third acquisition module is used for acquiring a backup switching main request sent by the management server when monitoring that the first database serving as the current main database is abnormal;
the second judging module is used for acquiring the current data information of the first database from the backup main request, comparing the current data information with the current data information of the second database and judging whether the data are consistent; and when the data are consistent, switching to the current master database, and when the data are inconsistent, prompting that the switching to the current master database fails.
Further, the second judging module includes:
a third obtaining submodule, configured to obtain a first position of a current binary log position of the first database from the backup main request;
a fourth obtaining submodule, configured to obtain a fifth position of a current binary log position of data copied from the first database by the second database;
a second judging sub-module, configured to compare the first location with the fifth location, and if the first location is equal to the fifth location, determine that data of current data information of the first database is consistent with data of current data information of the second database, and switch the current data information to a current master database; and if the first position is smaller than the fifth position, determining that the current data information of the first database is inconsistent with the current data information of the second database, and prompting that the switching to the current main database fails.
The scheme of the invention at least comprises the following beneficial effects:
the method and the device for switching the main database and the standby database can effectively identify the inconsistency of the main database and the standby database and carry out the main-standby switching; the binary logs are continuously synchronized to the standby database through the host database, so that the data consistency of the main database and the standby database and the switching accuracy in the main-standby switching process can be effectively ensured; the whole switching process does not need manual participation, the labor cost is saved, the reaction speed is high, and the error rate is low.
Drawings
Fig. 1 is an architecture diagram of main/standby switching according to an embodiment of the present invention;
fig. 2 is a flowchart of main/standby switching according to an embodiment of the present invention;
fig. 3 is a flowchart of a method for switching between active and standby application management servers according to an embodiment of the present invention;
fig. 4 is a schematic diagram of a device for switching between active and standby management servers according to an embodiment of the present invention;
fig. 5 is a flowchart of a main/standby switching method applied to a first database currently serving as a main database according to an embodiment of the present invention;
fig. 6 is a schematic diagram of a device for switching between main and standby databases of a first database currently serving as a main database according to an embodiment of the present invention;
fig. 7 is a flowchart of a main/standby switching method applied to a second database currently serving as a standby database according to an embodiment of the present invention;
fig. 8 is a schematic diagram of a device for switching between active and standby databases applied to a second database currently serving as a standby database according to an embodiment of the present invention.
Detailed Description
In order to make the technical problems, technical solutions and advantages of the present invention more apparent, the following detailed description is given with reference to the accompanying drawings and specific embodiments.
Referring to fig. 1, the present invention provides a method and a device for switching between a master database and a slave database, and aims to solve the problems of complicated process, high labor cost, low reaction speed and high error rate of switching between the master database and the slave database, wherein a management server interacts with a first database and a second database through a control flow, the first database and the second database report heartbeat messages to the management server, and the management server sends a master-slave switching request to the first database and the second database; the first database and the second database interact through data streams, and the second database synchronizes heartbeat messages of the first database.
Referring to fig. 3, an embodiment of the present invention provides a method for switching between active and standby databases, which is applied to a management server, and includes:
step 11, monitoring the operation of a first database as a current master database, and when detecting that the operation of the first database is abnormal, sending a backup switching master request to a second database as a current backup database;
further, the step of monitoring the operation of the first database as the current master database in step 11 includes:
acquiring heartbeat information reported by the first database regularly, and determining that the first database runs abnormally when the heartbeat information is not received at preset time intervals;
after the step of detecting that the first database runs abnormally, acquiring a first position of a current binary log position for recording data execution actions in the first database according to the heartbeat information reported by the first database for the last time;
and sending the current binary log position first position of the first database to the second database while sending the backup main request to the second database.
In a specific embodiment of the present invention, the step of obtaining, according to the heartbeat information reported by the first database for the last time, a first position of a current binary log position in the first database for recording a data execution action specifically includes:
acquiring a third position of a binary log position for recording data execution actions in the heartbeat information for the last time;
detecting whether a link with the first database exists:
if the binary log exists, sending a request for acquiring the current binary log position to the first database, and acquiring the first position of the current binary log position from the feedback message of the first database; if the heartbeat information does not exist, marking the third position of the binary log position of the recorded data execution action in the heartbeat information at the last time as the first position of the current binary log position.
Step 12, obtaining feedback data reported when the second database detects that the data in the second database is consistent with the data in the first database and the second database is successfully switched to the current master database, and obtaining data switching information of the second database which is finally synchronized with the first database according to the feedback data;
in a specific embodiment of the present invention, in the step 12, in the step of obtaining, according to the feedback data, data switching information that is synchronized by the second database from the first database at last:
acquiring a second position of a binary log position of the second database synchronized with the first data finally according to the feedback data;
and sending the second database to the first database from the second position of the binary log position synchronized with the first database at the last time when the master-cut-off request is sent to the first database.
And step 13, sending a main switching request containing the data switching information to the first database, so that the first database is switched to the current standby database when judging that no data loss exists in the data synchronized to the second database according to the data switching information.
Referring to fig. 2, in an embodiment of the present invention, the first database currently serving as the master database reports a heartbeat message to the management server periodically, the management server obtains a first location of a binary log location of the first database from the heartbeat message, and when the management server does not receive the heartbeat message at a predetermined time interval, it is determined that the first database is abnormal in operation;
the management server obtains the binary log position of the first database: acquiring a third position of a binary log position for recording data execution actions in the heartbeat information for the last time;
detecting whether a link with the first database exists: if yes, obtaining a first position of the current binary log position from the feedback message of the first database; if the heartbeat information does not exist, marking the third position of the binary log position of the recorded data execution action in the heartbeat information at the last time as the first position of the current binary log position.
The management server sends a standby switch main request to a second database serving as a current standby database, and after the second database receives the standby switch main request, a first position of a current binary log position of the first database is obtained from the standby switch main request; acquiring a fifth position of a current binary log position of the data copied from the first database by the second database; comparing the first position with the fifth position, if the first position is equal to the fifth position, determining that the current data information of the first database is consistent with the current data information of the second database, switching to a current master database, reporting a switching result to the management server, and reporting a second position of a binary log position synchronized with the first database to the management server;
and if the first position is smaller than the fifth position, determining that the current data information of the first database is inconsistent with the current data information of the second database, and prompting that the switching to the current main database fails.
When the management server receives the normal heartbeat of the first database again, the second database acquires a fourth position of the binary log position after the first database recovers the normal heartbeat, compares the fourth position with the second position, and determines that the data synchronized to the second database is not lost if the fourth position is less than or equal to the second position, and switches to the current standby database;
if the fourth position is larger than the second position, determining that the data synchronized to the second database is lost and has a difference with the data of the first database, and prompting that the switching fails; in a specific embodiment of the present invention, after the step of prompting the failure of the switching, a start file of a differentiated SQL script between the first database and the second database is detected;
traversing from the second position of the binary log position in the data of the first database until all log files generate SQL sentences of all transactions, redirecting to a target text file, executing the SQL sentences of the target text file by the second database, and switching to the current standby database; if the target text file cannot be oriented, indicating that data difference exists between the first database and the second database, the data consistency cannot be recovered, and failing to switch; according to the invention, the binary logs are continuously synchronized to the standby database by the host database, so that the data consistency and switching accuracy of the main database and the standby database in the main-standby switching process can be effectively ensured; the whole switching process does not need manual participation, the labor cost is saved, the reaction speed is high, and the error rate is low.
Referring to fig. 4, an embodiment of the present invention further provides a device for switching between active and standby databases, which is applied to a management server 1, and the device includes:
the system comprises a monitoring module 11, a backup switching module and a backup switching module, wherein the monitoring module is used for monitoring the operation of a first database serving as a current master database, and sending a backup switching master request to a second database serving as a current backup database when detecting that the operation of the first database is abnormal;
a first obtaining module 12, configured to obtain feedback data reported when the second database is successfully switched to the current master database when the second database detects that data in the second database is consistent with data in the first database, and obtain, according to the feedback data, data switching information that is synchronized with the first database at last by the second database;
a first data sending module 13, configured to send a master-backup request including the data switching information to the first database, so that the first database is switched to the current backup database when it is determined, according to the data switching information, that there is no data loss in the data synchronized to the second database.
Further, the monitoring module 11 includes:
the first monitoring submodule 111 is configured to acquire heartbeat information periodically reported by the first database, and determine that the first database is abnormal in operation when the heartbeat information is not received at a preset time interval.
Further, the apparatus further comprises:
a first log obtaining module 14, configured to obtain, according to the heartbeat information reported by the first database at the last time, a first position of a current binary log position in the first database, where the first position is used for recording a data execution action;
the monitoring module 11 sends the standby master request to the second database, and sends the first position of the current binary log position of the first database to the second database.
Further, the first obtaining module 12 includes:
a first log obtaining unit 121, configured to obtain, according to the feedback data, a second location of a binary log location at which the second database is synchronized with the first data last;
the first data sending module sends a master switching request to the first database, and sends the second database to the first database from the second position of the binary log position synchronized with the first database at the last time.
Further, the first log obtaining module 14 further includes:
a second log obtaining unit 141, configured to obtain a third position of a binary log position where a data execution action is recorded in the heartbeat information for the last time;
a first detection unit for detecting whether a link with the first database exists:
if the binary log exists, sending a request for acquiring the current binary log position to the first database, and acquiring the first position of the current binary log position from the feedback message of the first database; if the heartbeat information does not exist, marking the third position of the binary log position of the recorded data execution action in the heartbeat information at the last time as the first position of the current binary log position.
It should be noted that the device for switching between active and standby databases in the management server according to the embodiments of the present invention is a device that applies the method for switching between active and standby databases in the management server, that is, all embodiments of the method are applicable to the device, and all embodiments can achieve the same or similar beneficial effects.
Referring to fig. 5, an embodiment of the present invention further provides a method for switching between main and standby databases, which is applied to a first database serving as a current main database, and includes:
step 21, receiving a master switching request sent by a management server when the first database is detected to be abnormal in operation; further, before step 21, the method further comprises:
and reporting heartbeat information to the management server periodically, so that the management server detects whether the first database operates abnormally according to the heartbeat information.
Step 22, obtaining data switching information synchronized from the first database when the second database serving as the current standby database is switched to the main database from the main switching request; further, step 22 includes:
and acquiring a second position of the binary log position of the second database synchronized from the first database finally from the main backup request.
Further, the step of determining whether the data synchronized to the second database is lost according to the data switching information includes: acquiring a fourth position of the binary log after the first database recovers normal heartbeat;
comparing the fourth position with the second position, and if the fourth position is less than or equal to the second position, determining that the data synchronized to the second database is not lost, and switching to the current standby database; and if the fourth position is larger than the second position, determining that the data synchronized to the second database is lost and has a difference with the data of the first database, and prompting that the switching fails.
Step 23, judging whether the data synchronized to the second database is lost or not according to the data switching information, and switching to the current standby database when the data is judged not to be lost; and when the data is judged to be lost, prompting that the switching fails.
Further, in step 23, after the step of prompting the handover failure when it is determined that the data is lost, the method further includes:
detecting a start file of a differentiated SQL script between the first database and the second database;
and traversing from the second position of the binary log position in the data of the first database until all log files are obtained, generating SQL sentences of all transactions, and redirecting to a target text file.
Referring to fig. 6, an embodiment of the present invention further provides a device for switching between main and standby databases, where the device is applied to a first database serving as a current main database, and the device includes:
a first receiving module 21, configured to receive a master-backup request sent by a management server when the first database is detected to be abnormal in operation; further, the first receiving module 21 includes:
the first reporting sub-module 211 is configured to report heartbeat information to the management server periodically, so that the management server detects whether the first database operates abnormally according to the heartbeat information.
A second obtaining module 22, configured to obtain, from the master-slave switching request, data switching information synchronized from the first database when a second database serving as a current slave database is switched to a master database; further, the second obtaining module 22 includes: the first obtaining sub-module 221 is configured to obtain, from the master prepare request, a second location of a binary log location where the second database is synchronized with the first database at last.
The first judging module 23 is configured to judge whether data synchronized to the second database is lost according to the data switching information, and switch to the current standby database when it is judged that the data is not lost; when the data are judged to be lost, the switching failure is prompted; further, the first determining module 23 includes:
the second obtaining submodule 231 is configured to obtain a fourth position of the binary log position after the first database recovers a normal heartbeat;
the first determining submodule 232 is configured to compare the fourth position with the second position, and if the fourth position is less than or equal to the second position, determine that data synchronized to the second database is not lost, and switch to the current backup database; and if the fourth position is larger than the second position, determining that the data synchronized to the second database is lost and has a difference with the data of the first database, and prompting that the switching fails.
Further, the apparatus further comprises:
a first detection module 24, configured to detect a start file of a differentiated SQL script between the first database and the second database;
and the first generating module 25 is configured to traverse from the second position of the binary log position to all log files in the data of the first database, generate an SQL statement for each transaction, and redirect the SQL statement to a target text file.
It should be noted that the device for switching between main and standby databases applied to the first database currently serving as the main database provided in the embodiment of the present invention is a device for applying the method for switching between main and standby databases currently serving as the main database, that is, all embodiments of the method are applicable to the device, and can achieve the same or similar beneficial effects.
Referring to fig. 7, an embodiment of the present invention further provides a method for switching between active and standby databases, which is applied to a second database serving as a current standby database, and includes:
step 31, acquiring a backup switching main request sent by a management server when monitoring that a first database serving as a current main database is abnormal;
step 32, obtaining the current data information of the first database from the backup main request, comparing the current data information with the current data information of the second database, and judging whether the data are consistent; and when the data are consistent, switching to the current master database, and when the data are inconsistent, prompting that the switching to the current master database fails.
Further, in step 32, the step of obtaining the current data information of the first database from the backup main request, and comparing the current data information of the first database with the current data information of the second database includes:
acquiring a first position of a current binary log position of the first database from the backup main request;
acquiring a fifth position of a current binary log position of the data copied from the first database by the second database;
comparing the first position with the fifth position, if the first position is equal to the fifth position, determining that the current data information of the first database is consistent with the current data information of the second database, and switching to a current main database; and if the first position is smaller than the fifth position, determining that the current data information of the first database is inconsistent with the current data information of the second database, and prompting that the switching to the current main database fails.
Referring to fig. 6, an embodiment of the present invention provides a device for switching between active and standby databases, which is applied to a second database 3 as a current standby database, and the device includes:
a third obtaining module 31, configured to obtain a backup master request sent by the management server when the first database serving as the current master database is monitored to be abnormal;
a second judging module 32, configured to obtain current data information of the first database from the backup main request, compare the current data information with current data information of the second database, and judge whether data are consistent; and when the data are consistent, switching to the current master database, and when the data are inconsistent, prompting that the switching to the current master database fails.
Further, the second judging module includes:
a third obtaining submodule 321, configured to obtain a first position of a current binary log position of the first database from the backup main request;
a fourth obtaining sub-module 322, configured to obtain a fifth current binary log location of the data copied from the first database by the second database;
a second determining sub-module 323, configured to compare the first location with the fifth location, and if the first location is equal to the fifth location, determine that the current data information of the first database is consistent with the current data information of the second database, and switch the current database to a current master database; and if the first position is smaller than the fifth position, determining that the current data information of the first database is inconsistent with the current data information of the second database, and prompting that the switching to the current main database fails.
It should be noted that the device for switching between active and standby databases applied to the second database currently serving as the standby database provided in the embodiment of the present invention is a device for applying the method for switching between active and standby databases applied to the second database currently serving as the standby database, that is, all embodiments of the method are applicable to the device, and can achieve the same or similar beneficial effects.
While the foregoing is directed to the preferred embodiment of the present invention, it will be understood by those skilled in the art that various changes and modifications may be made without departing from the spirit and scope of the invention as defined in the appended claims.

Claims (16)

1. A method for switching between main database and standby database is applied to a management server, and is characterized by comprising the following steps:
acquiring heartbeat information periodically reported by a first database serving as a current main database, and determining that the first database is abnormal in operation when the heartbeat information is not received at preset time intervals;
acquiring a first position of a current binary log position used for recording data execution actions in the first database according to the heartbeat information reported by the first database for the last time;
sending a backup switching main request to a second database serving as a current backup database, and simultaneously sending a first position of a current binary log position of the first database to the second database;
acquiring feedback data reported when the second database detects that the data in the second database is consistent with the data in the first database and the second database is successfully switched to the current master database, and acquiring data switching information of the second database which is finally synchronized with the first database according to the feedback data;
and sending a main switching request containing the data switching information to the first database, so that the first database is switched to the current standby database when judging that the data synchronized to the second database is not lost according to the data switching information.
2. The method for switching between main and standby databases according to claim 1, wherein in the step of obtaining the data switching information of the second database that is synchronized with the first database at last according to the feedback data:
acquiring a second position of a binary log position of the second database synchronized with the first data finally according to the feedback data;
and sending the second database to the first database from the second position of the binary log position synchronized with the first database at the last time when the master-cut-off request is sent to the first database.
3. The method according to claim 1, wherein the step of obtaining a first position of a current binary log position used for recording data execution actions in the first database according to the heartbeat information reported by the first database for the last time specifically comprises:
acquiring a third position of a binary log position for recording data execution actions in the heartbeat information for the last time;
detecting whether a link with the first database exists:
if the binary log exists, sending a request for acquiring the current binary log position to the first database, and acquiring the first position of the current binary log position from the feedback message of the first database; if the heartbeat information does not exist, marking the third position of the binary log position of the recorded data execution action in the heartbeat information at the last time as the first position of the current binary log position.
4. A method for switching between main database and standby database is applied to a first database which is a current main database, and is characterized by comprising the following steps:
receiving a main switching request sent by a management server when the first database is abnormally operated; acquiring data switching information synchronized from the first database when a second database serving as a current standby database is switched to a main database from the main switching request; acquiring a second position of a binary log position of the second database synchronized with the first database from the main switching request;
judging whether the data synchronized to the second database is lost or not according to the data switching information, and switching to a current standby database when the data is judged not to be lost; and when the data is judged to be lost, prompting that the switching fails.
5. The method according to claim 4, wherein before the step of receiving a primary backup request sent by a management server when the first database is abnormally operated, the method further comprises:
and reporting heartbeat information to the management server periodically, so that the management server detects whether the first database operates abnormally according to the heartbeat information.
6. The method according to claim 4, wherein the step of determining whether the data synchronized to the second database is lost according to the data switching information comprises: acquiring a fourth position of the binary log after the first database recovers normal heartbeat;
comparing the fourth position with the second position, and if the fourth position is less than or equal to the second position, determining that the data synchronized to the second database is not lost, and switching to the current standby database; and if the fourth position is larger than the second position, determining that the data synchronized to the second database is lost and has a difference with the data of the first database, and prompting that the switching fails.
7. The method according to claim 6, wherein after the step of prompting the switching failure when the data is determined to be lost, the method further comprises:
detecting a start file of a differentiated SQL script between the first database and the second database; and traversing from the second position of the binary log position in the data of the first database until all log files are obtained, generating SQL sentences of all transactions, and redirecting to a target text file.
8. A method for switching between main and standby databases is applied to a second database serving as a current standby database, and is characterized by comprising the following steps:
acquiring a backup switching main request sent by a management server when monitoring that a first database serving as a current main database is abnormal;
acquiring a first position of a current binary log position of the first database from the backup main request, comparing the first position with a fifth position of the current binary log position of the data copied from the first database by the second database, and judging whether the data are consistent; the comparison process comprises: comparing the first position with the fifth position, if the first position is equal to the fifth position, determining that the current data information of the first database is consistent with the current data information of the second database, and switching to a current main database; if the first position is smaller than the fifth position, determining that the current data information of the first database is inconsistent with the current data information of the second database, and prompting that the switching to the current main database fails;
when the data are consistent, switching to the current master database, reporting feedback data to the management server, and when the data are inconsistent, prompting that the switching to the current master database fails; the feedback data comprises data switching information of the second database which is synchronized with the first database at last, and the data switching information is used for switching the first database into the current standby database when the first database judges that data synchronized to the second database is not lost according to the data switching information after receiving a main switching request.
9. A device for switching between main database and standby database is applied to a management server, and is characterized by comprising the following steps:
the system comprises a monitoring module, a backup switching module and a backup switching module, wherein the monitoring module is used for monitoring the operation of a first database serving as a current master database, and sending a backup switching master request to a second database serving as a current backup database when detecting that the operation of the first database is abnormal; the monitoring module includes: the first monitoring submodule is used for acquiring heartbeat information reported by the first database regularly, and determining that the first database runs abnormally when the heartbeat information is not received at preset time intervals;
the system comprises a first log acquisition module, a first database management module and a second log acquisition module, wherein the first log acquisition module is used for acquiring a first position of a current binary log position used for recording data execution actions in the first database according to heartbeat information reported by the first database at the last time, and the monitoring module sends the first position of the current binary log position of the first database to the second database while sending a standby switching main request to the second database;
a first obtaining module, configured to obtain feedback data reported when the second database is successfully switched to the current master database when the second database detects that data in the second database is consistent with data in the first database, and obtain, according to the feedback data, data switching information that is synchronized with the first database at last by the second database;
and the first data sending module is used for sending a main switching request containing the data switching information to the first database, so that the first database is switched to the current standby database when the first database judges that the data synchronized to the second database is not lost according to the data switching information.
10. The apparatus for switching between active and standby databases according to claim 9, wherein the first obtaining module comprises:
the first log obtaining unit is used for obtaining a second position of a binary log position of the second database which is synchronized with the first data from the first database at last according to the feedback data;
the first data sending module sends a master switching request to the first database, and sends the second database to the first database from the second position of the binary log position synchronized with the first database at the last time.
11. The apparatus for switching between active and standby databases according to claim 9, wherein the first log obtaining module further comprises:
the second log obtaining unit is used for obtaining a third position of a binary log position of a recorded data execution action in the heartbeat information at the last time;
a first detection unit for detecting whether a link with the first database exists:
if the binary log exists, sending a request for acquiring the current binary log position to the first database, and acquiring the first position of the current binary log position from the feedback message of the first database; if the heartbeat information does not exist, marking the third position of the binary log position of the recorded data execution action in the heartbeat information at the last time as the first position of the current binary log position.
12. A device for switching between main database and standby database is applied to a first database as a current main database, and is characterized in that the device comprises:
the first receiving module is used for receiving a main backup switching request sent by a management server when the first database is detected to be abnormal in operation;
a second obtaining module, configured to obtain, from the master-slave switching request, data switching information synchronized with the first database when a second database serving as a current slave database is switched to the master database; the second acquisition module includes: the first obtaining submodule is used for obtaining a second position of a binary log position of the second database which is synchronized with the first database from the main switching request;
the first judging module is used for judging whether the data synchronized to the second database is lost or not according to the data switching information, and switching to the current standby database when the data is judged not to be lost; and when the data is judged to be lost, prompting that the switching fails.
13. The apparatus for switching between active and standby databases according to claim 12, wherein the first receiving module comprises:
and the first reporting submodule is used for reporting heartbeat information to the management server periodically so that the management server detects whether the first database operates abnormally according to the heartbeat information.
14. The apparatus for switching between active and standby databases according to claim 12, wherein the first determining module comprises:
the second obtaining submodule is used for obtaining a fourth position of the binary log after the normal heartbeat of the first database is recovered;
the first judgment submodule is used for comparing the fourth position with the second position, and if the fourth position is less than or equal to the second position, determining that data synchronized to the second database is not lost, and switching to the current standby database; and if the fourth position is larger than the second position, determining that the data synchronized to the second database is lost and has a difference with the data of the first database, and prompting that the switching fails.
15. The apparatus for switching between active and standby databases according to claim 14, further comprising:
the first detection module is used for detecting a start file of a differentiated SQL script between the first database and the second database;
and the first generation module is used for traversing from the second position of the binary log position in the data of the first database to all log files, generating SQL sentences of all transactions and redirecting to a target text file.
16. A device for switching between main and standby databases is applied to a second database as a current standby database, and is characterized by comprising:
the third acquisition module is used for acquiring a backup switching main request sent by the management server when monitoring that the first database serving as the current main database is abnormal;
the second judging module is used for acquiring a first position of a current binary log position of the first database from the backup main request, comparing the first position with a fifth position of the current binary log position of data copied from the first database by the second database, and judging whether the data are consistent; when the data are consistent, switching to the current master database, and when the data are inconsistent, prompting that the switching to the current master database fails;
the second judging module includes: a second judging sub-module, configured to compare the first location with the fifth location, and if the first location is equal to the fifth location, determine that data of current data information of the first database is consistent with data of current data information of the second database, and switch the current data information to a current master database; and if the first position is smaller than the fifth position, determining that the current data information of the first database is inconsistent with the current data information of the second database, and prompting that the switching to the current main database fails.
CN201510213694.1A 2015-04-29 2015-04-29 Method and device for switching between main database and standby database Active CN106202075B (en)

Priority Applications (2)

Application Number Priority Date Filing Date Title
CN201510213694.1A CN106202075B (en) 2015-04-29 2015-04-29 Method and device for switching between main database and standby database
PCT/CN2015/089553 WO2016173179A1 (en) 2015-04-29 2015-09-14 Method and device for switching primary database and secondary database

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201510213694.1A CN106202075B (en) 2015-04-29 2015-04-29 Method and device for switching between main database and standby database

Publications (2)

Publication Number Publication Date
CN106202075A CN106202075A (en) 2016-12-07
CN106202075B true CN106202075B (en) 2021-02-19

Family

ID=57198042

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201510213694.1A Active CN106202075B (en) 2015-04-29 2015-04-29 Method and device for switching between main database and standby database

Country Status (2)

Country Link
CN (1) CN106202075B (en)
WO (1) WO2016173179A1 (en)

Families Citing this family (21)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN107066480B (en) * 2016-12-20 2020-08-11 创新先进技术有限公司 Method, system and equipment for managing main and standby databases
CN106815097A (en) * 2017-01-18 2017-06-09 北京许继电气有限公司 Database disaster tolerance system and method
CN108984569A (en) * 2017-06-05 2018-12-11 中兴通讯股份有限公司 Database switching method, system and computer readable storage medium
CN107506263A (en) * 2017-08-24 2017-12-22 深圳互联先锋科技有限公司 A kind of fault testing method and device
CN107632906A (en) * 2017-09-30 2018-01-26 北京九桥同步软件有限公司 Database data backup method and device
CN110377570B (en) * 2017-10-12 2021-06-11 腾讯科技(深圳)有限公司 Node switching method and device, computer equipment and storage medium
CN108897658B (en) * 2018-05-31 2023-03-21 康键信息技术(深圳)有限公司 Method and device for monitoring master database, computer equipment and storage medium
CN108834179B (en) * 2018-05-31 2021-07-09 深圳市酷达通讯有限公司 Method, server and client for adjusting heartbeat cycle
CN109086292B (en) * 2018-06-11 2023-11-28 平安科技(深圳)有限公司 Database switching method and system
CN109947772A (en) * 2018-09-07 2019-06-28 网联清算有限公司 Database automatic switching method, device, storage medium and computer equipment
CN109388630B (en) * 2018-09-29 2020-09-29 京东数字科技控股有限公司 Database switching method, system, electronic device and computer readable medium
CN110166524B (en) * 2019-04-12 2023-04-07 未鲲(上海)科技服务有限公司 Data center switching method, device, equipment and storage medium
CN110109934B (en) * 2019-05-08 2021-07-30 上海达梦数据库有限公司 Database management method, device, server and storage medium
CN110209554B (en) * 2019-06-14 2023-08-11 上海中通吉网络技术有限公司 Database log distribution method, device and equipment
CN110909171B (en) * 2019-10-16 2022-07-12 珠海格力电器股份有限公司 Data scheduling method, device, terminal and storage medium
CN111258823A (en) * 2020-01-17 2020-06-09 青梧桐有限责任公司 Method and system for switching master server and slave server
CN113297002B (en) * 2020-02-21 2023-12-05 北京京东振世信息技术有限公司 Database working mode switching method and device
CN111460029B (en) * 2020-03-11 2024-04-19 中移动信息技术有限公司 Data synchronization method and device
CN112363865A (en) * 2020-10-19 2021-02-12 深圳云天励飞技术股份有限公司 Database fault recovery method and device and face image search system
CN114679399A (en) * 2022-03-17 2022-06-28 浙江大华技术股份有限公司 Host switching method and device, storage medium and electronic device
CN115361269B (en) * 2022-07-08 2023-05-30 福建新大陆通信科技股份有限公司 Hot backup method for emergency broadcasting equipment

Citations (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
JPH08115323A (en) * 1994-10-18 1996-05-07 Toppan Printing Co Ltd Automatic page-up rule generation device
US7043504B1 (en) * 2000-04-10 2006-05-09 International Business Machines Corporation System and method for parallel primary and secondary backup reading in recovery of multiple shared database data sets
CN101751394A (en) * 2008-12-16 2010-06-23 青岛海信传媒网络技术有限公司 Method and system for synchronizing data
CN102810111A (en) * 2012-05-07 2012-12-05 互动在线(北京)科技有限公司 Implementation method and system for keeping high availability of Oracle database service
CN102968457A (en) * 2012-10-30 2013-03-13 北京奇虎科技有限公司 Switching method and system between databases
CN103220183A (en) * 2013-05-02 2013-07-24 杭州电子科技大学 Implement method of Hadoop high-availability system based on double-main-engine warm backup

Family Cites Families (8)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN101212458A (en) * 2006-12-25 2008-07-02 中兴通讯股份有限公司 Database system backup method
CN101729290A (en) * 2009-11-04 2010-06-09 中兴通讯股份有限公司 Method and device for realizing business system protection
US8442962B2 (en) * 2010-12-28 2013-05-14 Sap Ag Distributed transaction management using two-phase commit optimization
CN103064860A (en) * 2011-10-21 2013-04-24 阿里巴巴集团控股有限公司 Database high availability implementation method and device
CN103780638B (en) * 2012-10-18 2019-02-19 腾讯科技(深圳)有限公司 Method of data synchronization and system
CN102945195B (en) * 2012-11-26 2015-11-18 国电南瑞科技股份有限公司 A kind of Master-standby redundancy replication method based on SQLite database
CN103019875B (en) * 2012-12-19 2015-12-09 北京世纪家天下科技发展有限公司 The method of the two main transformation of a kind of fulfillment database and device
CN103226502B (en) * 2013-05-21 2015-08-19 中国工商银行股份有限公司 A kind of data calamity is for control system and data reconstruction method

Patent Citations (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
JPH08115323A (en) * 1994-10-18 1996-05-07 Toppan Printing Co Ltd Automatic page-up rule generation device
US7043504B1 (en) * 2000-04-10 2006-05-09 International Business Machines Corporation System and method for parallel primary and secondary backup reading in recovery of multiple shared database data sets
CN101751394A (en) * 2008-12-16 2010-06-23 青岛海信传媒网络技术有限公司 Method and system for synchronizing data
CN102810111A (en) * 2012-05-07 2012-12-05 互动在线(北京)科技有限公司 Implementation method and system for keeping high availability of Oracle database service
CN102968457A (en) * 2012-10-30 2013-03-13 北京奇虎科技有限公司 Switching method and system between databases
CN103220183A (en) * 2013-05-02 2013-07-24 杭州电子科技大学 Implement method of Hadoop high-availability system based on double-main-engine warm backup

Non-Patent Citations (1)

* Cited by examiner, † Cited by third party
Title
从双机到四机中MySQL数据库高可用性实现方法;金紫蘅;《中国科技信息》;20110815;第91页,第96页 *

Also Published As

Publication number Publication date
WO2016173179A1 (en) 2016-11-03
CN106202075A (en) 2016-12-07

Similar Documents

Publication Publication Date Title
CN106202075B (en) Method and device for switching between main database and standby database
CN107291787B (en) Main and standby database switching method and device
CN105589961B (en) A kind of detection method and system of database real-time synchronization system data consistency
CN103077242B (en) The method of a kind of fulfillment database server two-node cluster hot backup
CN108847982B (en) Distributed storage cluster and node fault switching method and device thereof
CN109032849B (en) Hot backup system, hot backup method and computer equipment
US7996718B1 (en) Techniques for continuous data protection
CN101706795A (en) Method for synchronizing data of database in active/standby server
CN105159795A (en) Data synchronization method, apparatus and system
CN100394394C (en) Fault tolerant duplex computer system and its control method
CN112506702B (en) Disaster recovery method, device, equipment and storage medium for data center
CN107688547B (en) Method and system for switching between main controller and standby controller
WO2022088861A1 (en) Database fault handling method and apparatus
CN110635950A (en) Double-data-center disaster recovery system
WO2012068787A1 (en) Data synchronization method and device
CN106815094B (en) Method and equipment for realizing transaction submission in master-slave synchronization mode
CN109165112B (en) Fault recovery method, system and related components of metadata cluster
CN112153134A (en) Disaster tolerance drilling method, device, equipment and storage medium of disaster tolerance cloud host
CN106294795A (en) A kind of data base's changing method and system
CN110011853B (en) Cross fault troubleshooting method and device for multiple platforms and clusters
CN113535665B (en) Method and device for synchronizing log files between main database and standby database
CN108009045B (en) Method and device for processing faults of main and standby databases
CN106682141B (en) Data synchronization method based on service operation log
CN116737462A (en) Data processing method, system, device and medium
CN109005246B (en) Data synchronization method, device and system

Legal Events

Date Code Title Description
C06 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant
GR01 Patent grant
TR01 Transfer of patent right
TR01 Transfer of patent right

Effective date of registration: 20220107

Address after: 100176 602, floor 6, building 6, courtyard 10, KEGU 1st Street, Beijing Economic and Technological Development Zone, Daxing District, Beijing (Yizhuang group, high-end industrial area of Beijing Pilot Free Trade Zone)

Patentee after: Jinzhuan Xinke Co.,Ltd.

Address before: 518057 Ministry of justice, Zhongxing building, South Science and technology road, Nanshan District hi tech Industrial Park, Shenzhen, Guangdong

Patentee before: ZTE Corp.