Main and standby database switching method and device, equipment, storage medium and product
Based on the scheduling platform to judge and switch the main library according to the database operation information, the main spare library is used to write and read the data alternately, solving the problem of losing the reading function when the existing technology reserve library is lost when the main library fails, and improved The availability of the database architecture.
Patent Information
- Application Number
- CN202411335092.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-24
- Publication Date
- 2025-01-28
AI Technical Summary
In the existing master-standby database switching technology, the main library is fixed for writing data, and the backup library is fixed for reading data, resulting in the backup library losing the data reading function when the main library fails, which reduces the availability of the database architecture.
Determine whether the main database cluster meets the preset database switching conditions through the assignment information of the first database and the second database. Realize data synchronization and data reading at the application level.
Even when the main library fails, the preparation library can provide complete data writing and data reading function, which improves the availability of the database architecture.
Smart Images

Figure CN119377316A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a method, device, equipment, storage medium and product for switching between a primary and a secondary database. Background Art
[0002] At present, in database management systems, in order to ensure high data availability and disaster recovery capabilities, active-standby switching technology has become a key component. Among them, active-standby switching technology is to automatically or manually switch the database service to the standby database when the primary database fails to ensure business continuity and data integrity.
[0003] In the related technology, the MHA (Master High Availability) tool is usually used to monitor the health status of the master database. When the master database fails, MHA can automatically switch between the master and the standby database to achieve the master-standby switch of the MySQL database. However, in this method, the master database is fixed for writing data, and the standby database is fixed for reading data. When the master database fails, the standby database takes over the writing function of the master database, which makes the standby database lose its data reading function in the database cluster, resulting in low availability of the database architecture.
[0004] The above contents are only used to assist in understanding the technical solution of the present application and do not constitute an admission that the above contents are prior art. Summary of the invention
[0005] The main purpose of this application is to provide a method for switching between a primary and a standby database, aiming to solve the technical problem of low availability of the database architecture.
[0006] To achieve the above object, the present application proposes a method for switching between a primary and a standby database, the method comprising:
[0007] In response to a data write instruction, first operation information of the first database and second operation information of the second database are acquired, wherein both the first database and the second database have data write and data read capabilities, the first database is a primary database, and the second database is a secondary database; or, the first database is a secondary database, and the second database is a primary database; the primary database is used for data write, and the secondary database is used for data read;
[0008] Based on the first job information and the second job information, determining whether the active / standby database cluster meets a preset database switching condition;
[0009] If the master-slave database cluster meets the database switching condition, the master-slave database switching is performed on the first database and the second database to obtain the switched first database and the switched second database.
[0010] Optionally, the first database is a primary database, and the second database is a secondary database; the step of judging whether the primary and secondary database clusters meet a preset database switching condition based on the first job information and the second job information includes:
[0011] Determine the number of first jobs and first job running status information of the first database based on the first job information, and determine the number of second jobs of the second database based on the second job information;
[0012] Based on the first job quantity, the second job quantity, and the first job running status information, it is determined whether the active-standby database cluster meets a preset database switching condition.
[0013] Optionally, the step of judging whether the active-standby database cluster meets a preset database switching condition based on the first job quantity, the second job quantity and the first job running status information includes:
[0014] Determine whether the number of the first jobs is greater than the number of the second jobs, and determine whether the running state information of the first job is in an idle state;
[0015] If the number of the first jobs is greater than the number of the second jobs, and the running status information of the first job is in an idle state, the active-standby database cluster meets the preset database switching condition;
[0016] If the number of the first jobs is equal to the number of the second jobs, or the running status information of the first job is in a running state, the active-standby database cluster does not meet the preset database switching condition.
[0017] Optionally, if the active-standby database cluster meets the database switching condition, the step of performing active-standby database switching on the first database and the second database to obtain the switched first database and the switched second database includes:
[0018] If the active / standby database cluster meets the database switching condition, turning off the data writing signal of the first database to stop data writing of the first database;
[0019] After closing the data writing signal of the first database, obtaining the current running status information of the second job of the first database;
[0020] Determine whether the second job running status information is in an idle state. If the second job running status information is in an idle state, perform a primary-slave database switch on the first database and the second database to obtain a switched first database and a switched second database.
[0021] Optionally, the first database is a primary database, and the second database is a secondary database; the step of performing primary-secondary database switching on the first database and the second database to obtain the switched first database and the switched second database includes:
[0022] Switching the service access process to the first database to obtain the switched first database, wherein the service access process is used to read data from the database;
[0023] The data writing signal of the second database is turned on to obtain the switched second database to start writing data into the second database.
[0024] Optionally, the method for switching between a primary and a standby database further includes:
[0025] Acquire first database attribute information of the first database and second database attribute information of the second database;
[0026] In response to a data writing instruction, obtaining a source data file to be written;
[0027] In a case where the first database is a primary database and the second database is a standby database, converting the source data file into a first standard file corresponding to the attribute information of the first database, so that the first database writes data into the first standard file;
[0028] When the first database is a standby database and the second database is a primary database, the source data file is converted into a second standard file corresponding to the attribute information of the second database, so that the second database can write data into the second standard file.
[0029] In addition, to achieve the above-mentioned purpose, the present application also proposes a primary and standby database switching device, the primary and standby database switching device comprising:
[0030] an acquisition module, configured to acquire first operation information of the first database and second operation information of the second database in response to a data write instruction, wherein both the first database and the second database have data write and data read capabilities, the first database is a primary database, and the second database is a standby database; or, the first database is a standby database, and the second database is a primary database; the primary database is used for data write, and the standby database is used for data read;
[0031] A judgment module, used to judge whether the active and standby database clusters meet a preset database switching condition based on the first job information and the second job information;
[0032] The switching module is used to perform a master-slave database switch on the first database and the second database if the master-slave database cluster meets the database switching condition, so as to obtain a switched first database and a switched second database.
[0033] In addition, to achieve the above-mentioned purpose, the present application also proposes a primary-standby database switching device, which includes: a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the computer program is configured to implement the steps of the primary-standby database switching method described above.
[0034] In addition, to achieve the above-mentioned purpose, the present application also proposes a storage medium, which is a computer-readable storage medium, and a computer program is stored on the storage medium. When the computer program is executed by a processor, the steps of the primary and standby database switching method described above are implemented.
[0035] In addition, to achieve the above-mentioned purpose, the present application also provides a computer program product, which includes a computer program. When the computer program is executed by a processor, the steps of the primary and standby database switching method described above are implemented.
[0036] One or more technical solutions proposed in this application have at least the following technical effects:
[0037] Compared with the related art that uses the MHA tool to monitor the health status of the main database, and when the main database fails, MHA can automatically perform the master-slave switch to achieve the master-slave switch of the Mysql database. However, in this method, the main database is fixedly used to write data, and the standby database is fixedly used to read data. When the main database fails, the standby database is used to take over the writing function of the main database, so that the database cluster loses the data reading function of the standby database, resulting in low availability of the database architecture. In this application, the scheduling platform is used to determine whether the master-slave database cluster meets the preset database switching conditions based on the first job information of the first database and the second job information of the second database, wherein both the first database and the second database have the ability to write and read data. If the master-slave database cluster meets the database switching conditions, the master-slave database switch is performed on the first database and the second database to obtain the switched first database and the switched second database. It can be understood that in this application, both the primary and standby databases have the ability to write and read data. The scheduling platform switches the primary and standby databases according to their current operating conditions, so that the primary and standby databases alternately write data and read data for business access, so as to achieve data synchronization between the primary and standby databases and meet the data reading requirements at the application level. Even when the primary database fails, the standby database can provide complete data writing and data reading functions, thereby improving the availability of the database architecture. BRIEF DESCRIPTION OF THE DRAWINGS
[0038] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present application and, together with the description, serve to explain the principles of the present application.
[0039] In order to more clearly illustrate the embodiments of the present application 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, for ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative labor.
[0040] Figure 1 A flowchart of the first embodiment of the method for switching between primary and standby databases provided in this application;
[0041] Figure 2 This is a schematic diagram of the process flow of the primary and standby database switching method for the present application for a database dual loading scenario;
[0042] Figure 3 This is a schematic diagram of the process of switching the master database to the standby database in the master-standby database switching method of this application;
[0043] Figure 4 This is a schematic diagram of the primary database secondary switching process of the primary and standby database switching method of this application;
[0044] Figure 5 This is a schematic diagram of data synchronization design for the master-slave database switching method of this application;
[0045] Figure 6 A schematic diagram of component configuration of the master-slave database switching method of this application;
[0046] Figure 7 A flowchart of the second embodiment of the method for switching between the primary and standby databases of the present application is provided;
[0047] Figure 8 A schematic diagram of system compatibility design in the master-slave database switching method of this application;
[0048] Fig. 9 This is a schematic diagram of the module structure of the master-slave database switching device according to an embodiment of the present application;
[0049] Fig.10 This is a schematic diagram of the device structure of the hardware operating environment involved in the primary and standby database switching method in the embodiment of the present application.
[0050] The purpose, features and advantages of this application will be further described in conjunction with the embodiments and with reference to the accompanying drawings. DETAILED DESCRIPTION
[0051] It should be understood that the specific embodiments described herein are only used to explain the technical solutions of the present application and are not used to limit the present application.
[0052] In order to better understand the technical solution of the present application, a detailed description will be given below in conjunction with the accompanying drawings and specific implementation methods.
[0053] The main solution of the embodiment of the present application is: in response to a data write instruction, first job information of the first database and second job information of the second database are obtained, wherein the first database and the second database both have the ability to write and read data, and the first database is a primary database and the second database is a standby database; or, the first database is a standby database and the second database is a primary database; the primary database is used for data writing and the standby database is used for data reading; based on the first job information and the second job information, whether the primary and standby database clusters meet a preset database switching condition is determined; if the primary and standby database clusters meet the database switching condition, the primary and standby databases are switched to the first and second databases to obtain the switched first and second databases.
[0054] In this embodiment, the scheduling platform is used as the execution entity. For the convenience of description, it will be described as "platform" hereinafter.
[0055] In the related art, the MHA (Master High Availability) tool is usually used to monitor the health status of the master database, and when the master database fails, MHA can automatically perform master-slave switching to achieve master-slave switching of the MySQL database. However, in this method, the master database is fixedly used for writing data, and the standby database is fixedly used for reading data. When the master database fails, the standby database takes over the writing function of the master database, which makes the standby database lose its data reading function in the database cluster, resulting in low availability of the database architecture.
[0056] The present application provides a solution to implement the switching of the primary and standby databases, thereby achieving data synchronization between the primary and standby databases and satisfying data reading at the application level. Even when the primary database fails, the standby database can provide complete data writing and data reading functions, thereby improving the availability of the database architecture.
[0057] It can be seen from the above embodiments that the scheduling platform in this application is used to determine whether the master-slave database cluster meets the preset database switching conditions based on the first job information of the first database and the second job information of the second database, wherein both the first database and the second database have the ability to write and read data. If the master-slave database cluster meets the database switching conditions, the master-slave database is switched on the first database and the second database to obtain the switched first database and the switched second database. It can be understood that in this application, both the master and the standby databases have the ability to write and read data. The scheduling platform switches the master and the standby databases according to the current job status of the master and the standby databases, so that the master and the standby databases alternately write data and read data for business access, so as to achieve data synchronization between the master and the standby databases and meet the data reading at the application level. Even when the master database fails, the standby database can provide complete data writing and data reading functions, thereby improving the availability of the database architecture.
[0058] It should be noted that the execution subject of this embodiment can be a computing service device with data processing, network communication and program running functions, such as a tablet computer, a personal computer, a mobile phone, etc., or an electronic device capable of realizing the above functions, a terminal system, etc. The following takes the active-standby database switching device as an example to illustrate this embodiment and the following embodiments.
[0059] Based on this, the present application embodiment provides a method for switching between a primary and a secondary database. Figure 1 , Figure 1 This is a flow chart of the first embodiment of the method for switching between primary and standby databases of the present application.
[0060] In this embodiment, the master-slave database switching method is applied to a scheduling platform, the scheduling platform is communicatively connected to a master-slave database cluster, the master-slave database cluster includes a first database and a second database, and the master-slave database switching method includes steps S100 to S300:
[0061] Step S100, in response to a data write instruction, obtaining first operation information of the first database and second operation information of the second database, wherein both the first database and the second database have the capability of data writing and data reading, the first database is a primary database, and the second database is a standby database; or, the first database is a standby database, and the second database is a primary database; the primary database is used for data writing, and the standby database is used for data reading;
[0062] It should be noted that the scheduling platform is a system responsible for managing and scheduling tasks or jobs, including allocating resources, monitoring job progress, handling job dependencies, etc. The master-slave database cluster is a database architecture that includes two or more database servers, which work together to provide high data availability and disaster recovery capabilities. Specifically, the scheduling platform establishes a stable communication connection with the database cluster to ensure that data related to scheduling tasks can be effectively stored and retrieved. The scheduling platform will read the job status, update the job progress, or write new job instructions as needed. In general, the scheduling platform relies on a cluster consisting of master and standby databases to perform its core functions, and this database cluster has the ability to fail over to ensure the high availability of the scheduling platform.
[0063] In a specific implementation, the platform performs subsequent operations after receiving a data write request (instruction). Specifically, the platform obtains job information from the first database and the second database of the primary and standby database set, wherein the first database provides information about the first job, and the second database provides information about the second job, and both the first database and the second database have the ability to write and read data (that is, both the primary database and the standby database can receive data write operations and process data read requests). Job information refers to job execution related information of the database, including but not limited to the number of jobs, job running status, etc., wherein the number of jobs refers to the total number of tasks to be processed, being processed, or completed in the database at a specific point in time or time period; the job running status refers to the state of the database when executing a job, including idle state, job running, and jobs with no waiting resources, etc.
[0064] Furthermore, the first database is a main database, and the second database is a standby database; or, the first database is a standby database, and the second database is a main database; from the above, it can be seen that the first database and the second database can be used as a main database or a standby database in different situations. For example, the first database is a main database in an initial state, and the second database is a standby database in an initial state. After executing the main-standby database switching, the first database is a standby database, and the second database is a main database in an initial state.
[0065] It should be emphasized that the primary database is used for data writing, and the backup database is used for data reading. That is, when the database is used as the primary database, only data writing is performed, and when the database is used as the backup database, only data reading is performed. By separating data reading and writing, the load of the primary database is shared and high availability is provided.
[0066] In practice, the above configuration architecture allows the system to switch write operations to the standby database when the primary database fails, thereby maintaining service continuity. At the same time, the standby database can handle read operations while the primary database handles write operations, which can improve the overall performance and responsiveness of the system. This master-standby switch is usually automatic and controlled by the database management system or cluster management software.
[0067] Step S200: judging whether the active / standby database cluster meets a preset database switching condition based on the first job information and the second job information;
[0068] In the specific implementation, the platform will evaluate the current status of the primary and standby database clusters based on the two job information (the first job information and the second job information) and decide whether a database switch is required. Specifically, the platform makes a judgment based on the job information in the two databases (the primary database and the standby database), where the job information includes the job status, progress, error log, resource usage, etc.
[0069] It should be noted that the database switching condition is the pre-set database switching condition information. The platform will evaluate whether the master and standby databases in the current database cluster need to be switched based on some predefined conditions. Specifically, the master and standby database switching conditions include but are not limited to: data consistency: ensuring that the data between the master and standby databases are synchronized and there is no data inconsistency; performance indicators: such as response time, load, resource utilization, etc., to ensure that the database performance is within an acceptable range; fault detection: detecting whether the master database has a fault, such as inaccessibility, performance degradation, service interruption, etc.; standby database preparation status: confirming whether the standby database is ready to take over the responsibilities of the master database, such as whether data synchronization has been completed, and whether there are sufficient resources and configurations to process requests.
[0070] In a specific implementation, the first database is a primary database, the second database is a secondary database, and the platform determines whether the primary and secondary database clusters meet a preset database switching condition based on the first job information and the second job information, including:
[0071] Based on the first job information, determine the first job quantity and first job running status information of the first database, and based on the second job information, determine the second job quantity of the second database; based on the first job quantity, the second job quantity and the first job running status information, determine whether the primary and standby database clusters meet the preset database switching conditions.
[0072] In a specific implementation, when judging whether the master-slave database cluster meets the preset database switching conditions, it is necessary to determine the first job quantity and the first job running status information of the master database (first database), as well as the second job quantity of the standby database (second database). Since the master database is used for data writing, the master database executes data writing instructions and continuously writes new data. After the master database writes new data, the number of jobs increases. Since the standby database is used for data reading and does not write new data, after the data is written, the number of first jobs will be greater than the number of second jobs. On the basis of the number of jobs, the running status of the job of the master database also needs to be considered. It is not suitable to switch the database during the data writing process of the master database (which may easily lead to database errors). Therefore, the present application proposes to judge whether the master-slave database cluster meets the preset database switching conditions based on the number of jobs of the master-slave database and the running status of the job of the master database.
[0073] In a specific implementation, the platform determines whether the active-standby database cluster meets a preset database switching condition based on the first job quantity, the second job quantity, and the first job running status information, including:
[0074] Determine whether the first job number is greater than the second job number, and determine whether the first job running status information is in an idle state; if the first job number is greater than the second job number, and the first job running status information is in an idle state, the primary and standby database clusters meet the preset database switching conditions; if the first job number is equal to the second job number, or the first job running status information is in a running state, the primary and standby database clusters do not meet the preset database switching conditions.
[0075] In a specific implementation, the switching of databases is for data synchronization between the primary and standby databases. The primary database is used for data writing, so the primary database executes data writing instructions and continuously writes new data. After the primary database writes new data, the number of jobs increases. Since the standby database is used for data reading and does not write new data, after the data is written, the first number of jobs will be greater than the second number of jobs, which means that the primary database has executed data writing, and the data of the primary and standby databases are synchronized before the data is written, that is, after the data is written, the standby database still only includes the data before the data of the primary database is written. Therefore, when the standby database data is synchronized with the primary database, after the primary database completes data writing, and there is no running job or job waiting for resources in the primary database, the primary and standby database clusters meet the preset database switching conditions; if the first number of jobs is equal to the second number of jobs (the primary database has not written data), or the first job running status information is in the running state (the primary database is still executing data writing), the primary and standby database clusters do not meet the preset database switching conditions.
[0076] Step S300: If the active-standby database cluster meets the database switching condition, the active-standby database switching is performed on the first database and the second database to obtain the switched first database and the switched second database.
[0077] In a specific implementation, if the master-slave database cluster meets the database switching condition, the platform switches the master and the standby databases of the first database and the second database to obtain the switched first database and the switched second database. The switched first database switches from the master database to the standby database, performs data reading for business access, and the switched second database switches from the standby database to the master database, performs data writing, and is used for data synchronization of the second database. That is, the platform enables its master and standby databases to alternately write data and read data for business access, so as to achieve data synchronization between the master and standby databases and satisfy data reading at the application level. Even when the master database fails, the standby database can provide complete data writing and data reading functions, thereby improving the availability of the database architecture.
[0078] In the specific implementation, refer to Figure 2 After the extraction job is initiated, the data is extracted from the data lake to the exchange platform. The exchange platform will process the original data, generate standard files, and then push the message that the file has been generated to the scheduling platform. The scheduling platform receives the file message sent by the exchange platform and triggers the corresponding loading job. The loading job will load the standard file data of the exchange platform into the primary and standby databases. At the same time, the database switching job configured in advance in the scheduling platform will regularly schedule a database switching script to be responsible for the primary and standby switching of the dual loading databases. After the interface table data in the primary and standby databases are ready, the business program is triggered to process the interface table data into the business table, and then the business application obtains data by accessing the business table.
[0079] In a specific implementation, if the master-slave database cluster meets the database switching condition, the master-slave database switching is performed on the first database and the second database to obtain the switched first database and the switched second database, including:
[0080] If the master-slave database cluster meets the database switching condition, the data write signal of the first database is turned off to stop data writing to the first database; after turning off the data write signal of the first database, the second job running status information of the current first database is obtained; it is determined whether the second job running status information is in an idle state. If the second job running status information is in an idle state, the master-slave database switching is performed on the first database and the second database to obtain the switched first database and the switched second database.
[0081] In a specific implementation, if the master-slave database cluster meets the database switching condition, the platform will switch the master-slave database. Once the platform needs to perform the master-slave database switch, the platform will send a signal to the master database (the first database) to turn off the data writing function. This is to ensure the consistency and integrity of the data during the switching process and prevent data conflicts or damage during the switching.
[0082] Furthermore, after the data write signal of the first database is turned off, the platform obtains the second job running status information of the current first database and checks the current running status of the main database. This is to confirm whether all jobs of the main database have been correctly completed or suspended in the time period before the signal is turned off after the switch is determined, so as to ensure the smooth progress of the switch. It is understandable that the platform performs a secondary main database operation confirmation on the main database to prevent the situation where jobs are executed again in the time period before the signal is turned off after the switch is determined, so as to ensure the smooth progress of the switch.
[0083] The first database is a primary database, the second database is a secondary database, and the platform switches the first database and the second database between the primary and secondary databases to obtain the switched first database and the switched second database, including:
[0084] Switch the business access process to the first database to obtain the switched first database, wherein the business access process is used to read the data of the database; turn on the data write signal of the second database to obtain the switched second database to start writing data to the second database.
[0085] In the specific implementation, after the database switching operation, the access process of the business system or application (that is, the part of the business logic used to interact with the database) needs to be redirected to the new primary database. Specifically, the first database has changed from the original primary database to the backup database, and the second database has changed from the backup database to the new primary database. The business access process usually includes reading data in the database, such as query operations, for use by the business system. After the first database becomes the backup database, it will start to process the read requests of the business access process.
[0086] Furthermore, after the database switch, the role of the second database has changed from the primary database to the backup database, and the second database starts to perform data write operations, wherein turning on the data write signal usually means releasing the write lock that may have been set previously, allowing the database to accept operations such as insert, update or delete.
[0087] In the specific implementation, the above describes how to adjust the business access process and database read and write permissions after the database master-slave switch to ensure that the business system can continue to operate normally and the database can begin to accept new data write operations. This adjustment helps to ensure data continuity and high availability of the system.
[0088] In the specific implementation, refer to Figure 3 , the process is that the A database switches to the B database (i.e. the main database switches to the standby database). During initialization, the A database is the main database, responsible for data loading, and the B database is the standby database, responsible for the read operation of the business application. After the A database switches to the B database, the A database becomes the standby database, responsible for the read operation of the business application, and the B database becomes the main database, responsible for data loading. The specific switching logic is as follows: When the switching program determines whether the A database switches to the B database, it will read the job information in the A database and the B database, including but not limited to the job status and the number of jobs. When the number of jobs completed in the A database on the same day is greater than the number of jobs completed in the B database on the same day, and there are no running jobs or jobs waiting for resources in the A database, the switching program determines that it can switch from A to B. Then the switching program detects in real time whether there are any running jobs in the A database. If there are any running jobs in the A database, the switching cannot be performed, and the switching program exits and waits for the next polling; if there are no running jobs in the A database, the A database signal is turned off, that is, the A database signal state is changed from on to off. Then the switching program does a double-check to determine whether there are any running jobs in library A. If there are still running jobs in library A, the A signal status rolls back, the switching program exits, and this polling ends. If there are still no running jobs in library A, the application accesses the data and switches to library A, turns on the library B signal, and the library B signal changes from off to on, and library B starts running batches.
[0089] In the specific implementation, refer to Figure 4 , the process is to switch from database B to database A (i.e., the standby database after switching performs switching again). Before switching: database B is the main database, responsible for data loading, and database A is the standby database, responsible for business application read operations. After switching: database A is the main database, responsible for data loading, and database B is the standby database, responsible for business application read operations. The specific switching logic is as follows: when the switching program determines that database B switches to database A, it will also read the job information of databases A and B, including but not limited to the job status and total number of jobs. When the switching program determines that the number of completed jobs of database B is greater than or equal to the number of completed jobs of database A, and there are no errors, no running jobs, and no waiting resource jobs in database B, the database can be switched. The switching program will detect whether there are running jobs in database B. If so, the database switching program will exit; if not, the B signal will be turned off and database B will stop loading. Then the switching program will double-check database B and detect whether there are running jobs in database B again. If so, the B signal status will be rolled back and the database switching program will exit; if not, the business access process will switch to database B, turn on the A signal, and start loading database A.
[0090] In the specific implementation, refer to Figure 5Based on the bidirectional synchronization mechanism of A and B libraries and the library signal control mechanism, when the signal of A library is on, the loading job of A library starts working. At this time, data loading will not appear in B library. As long as the signal state does not change, A library will always load data, and there will be no brain split problem. Similarly, when the signal of B library is on, the loading job of B library starts working. At this time, data loading will not appear in A library. Therefore, due to the control of the signal, A and B libraries will not have the inconsistency problem of dual library loading at the same time. During the loading process, if the data volume of A library is greater than the data volume of B library, after A library switches to B library, the data of A library will be synchronized to B library; if the data volume of B library is greater than the data volume of A library, after B library switches to A library, the data of B library will be synchronized to A library. Finally, the data of A and B will be leveled, and there will be no data inconsistency.
[0091] In the specific implementation, refer to Figure 6 , this application deploys a set of dual-loading A and B library configurations under this solution, and only needs to configure the following components on the scheduling platform:
[0092] 1) Switching operation: responsible for the timing and scheduling of switching procedures;
[0093] 2) A-base signal: batch control switch for loading jobs in A-base;
[0094] 3) B library signal: B library loading operation batch control switch;
[0095] 4) Database A loading job: responsible for loading the table data of database A;
[0096] 5) B library loading job: responsible for loading the B library table data.
[0097] It only takes a short time to complete the configuration of the above components, and the deployment cost is extremely low. If the database needs to add or delete tables in the subsequent operation and maintenance phase, it can be achieved by modifying or offline loading operations, reducing the pressure of operation and maintenance.
[0098] In the specific implementation, the solution of the present application supports the switching condition of the data loading progress of the database in the dual loading scenario; the solution can support data synchronization from the primary database to the backup database, and can also support data synchronization from the backup database to the primary database, that is, it supports two-way data synchronization; further, since the solution supports two-way data synchronization, it can well solve the problem of data inconsistency between the primary and backup databases.
[0099] Compared with the related art that uses the MHA tool to monitor the health status of the main database, and when the main database fails, MHA can automatically perform the master-slave switch to achieve the master-slave switch of the Mysql database. However, in this method, the main database is fixedly used to write data, and the standby database is fixedly used to read data. When the main database fails, the standby database is used to take over the writing function of the main database, so that the database cluster loses the data reading function of the standby database, resulting in low availability of the database architecture. In this application, the scheduling platform is used to determine whether the master-slave database cluster meets the preset database switching conditions based on the first job information of the first database and the second job information of the second database, wherein both the first database and the second database have the ability to write and read data. If the master-slave database cluster meets the database switching conditions, the master-slave database switch is performed on the first database and the second database to obtain the switched first database and the switched second database. It can be understood that in this application, both the primary and standby databases have the ability to write and read data. The scheduling platform switches the primary and standby databases according to their current operating conditions, so that the primary and standby databases alternately write data and read data for business access, so as to achieve data synchronization between the primary and standby databases and meet the data reading requirements at the application level. Even when the primary database fails, the standby database can provide complete data writing and data reading functions, thereby improving the availability of the database architecture.
[0100] Based on the above first embodiment, the present application also proposes another embodiment, referring to Figure 7 , the master-slave database switching method includes:
[0101] Step A100, obtaining first database attribute information of the first database and second database attribute information of the second database;
[0102] In a specific implementation, the present application also supports compatibility with databases of different attributes (or categories), for example, the primary and standby databases are MySQL and Oracle Database, respectively. Specifically, database attribute information refers to the attributes of the database, and different database attributes are adapted to different data.
[0103] Step A200, in response to a data writing instruction, obtaining a source data file to be written;
[0104] In the specific implementation, the platform receives a specific instruction or request to write data to a database or other storage system. Before performing the write operation, the platform needs to locate and access the source file containing the data to be written. This source data file may be a CSV, JSON, XML or other format file containing the data to be imported or stored in the database.
[0105] Step A300: when the first database is a primary database and the second database is a standby database, convert the source data file into a first standard file corresponding to the attribute information of the first database, so that the first database writes data into the first standard file;
[0106] In the specific implementation, in the configuration, the first database is the main database, which is responsible for processing all data write operations, such as inserting, updating and deleting data. The second database is the standby database, which is usually used for read operations, such as queries, and synchronizes data from the main database to ensure data consistency. The source data file is converted into a first standard file corresponding to the attribute information of the first database, that is, the platform needs to convert the original data file (source data file) into a format that conforms to the data model and attribute requirements of the first database (main database). This involves operations such as data format conversion, data type matching, and field mapping. The first standard file refers to the converted file, which follows the data format and structure standards of the main database. For example, if the main database requires data to be arranged in a specific order, or requires specific data types and formats, the first standard file needs to meet these requirements.
[0107] In a specific implementation, once the source data file is converted to the first standard file, this file can be used by the first database (main database) to perform data writing operations. This process involves data import tools or scripts that can import the data in the first standard file into the corresponding table in the database. The purpose of this is to ensure data consistency and integrity while optimizing data writing performance. By converting the data file in advance, errors and performance bottlenecks that occur during data writing can be reduced.
[0108] Step A400, when the first database is a standby database and the second database is a primary database, convert the source data file into a second standard file corresponding to the attribute information of the second database, so that the second database can write data to the second standard file.
[0109] In the specific implementation, refer to Figure 8After the platform extracts the source data files from the data lake, it converts the source files into standard files suitable for loading such as Mysql, Oracle, and Postgre through its own platform capabilities, and then loads them into the corresponding Mysql, Oracle, and Postgre databases. Based on this feature, the dual-loaded AB database can be the same type of database or different types of databases. This solution can support switching between databases of the same type such as Mysql, Oracle, and Postgre, as well as switching between databases of different types such as Msql to Oracle, Mysql to Postgre, and Oracle to Postgre.
[0110] It should be noted that the above examples are only used to understand the present application and do not constitute a limitation on the master-slave database switching method of the present application. More simple transformations based on this technical concept are all within the protection scope of the present application.
[0111] This application also provides a primary and standby database switching device, please refer to Fig. 9 , the master-slave database switching device comprises:
[0112] The acquisition module 10 is used to acquire first operation information of the first database and second operation information of the second database in response to a data writing instruction, wherein the first database and the second database both have data writing and data reading capabilities, the first database is a primary database, and the second database is a standby database; or, the first database is a standby database, and the second database is a primary database; the primary database is used for data writing, and the standby database is used for data reading;
[0113] A judgment module 20, configured to judge whether the active / standby database cluster meets a preset database switching condition based on the first job information and the second job information;
[0114] The switching module 30 is used to perform a master-slave database switch on the first database and the second database if the master-slave database cluster meets the database switching condition, so as to obtain a switched first database and a switched second database.
[0115] Optionally, the judging module 20 includes:
[0116] a determination module, configured to determine the number of first jobs and first job running status information of the first database based on the first job information, and determine the number of second jobs of the second database based on the second job information;
[0117] The condition judgment module is used to judge whether the active-standby database cluster meets a preset database switching condition based on the first job quantity, the second job quantity and the first job running status information.
[0118] Optionally, the condition judgment module includes:
[0119] An information determination module, used to determine whether the number of the first jobs is greater than the number of the second jobs, and to determine whether the running state information of the first job is in an idle state;
[0120] A condition satisfaction module, configured to determine that if the number of the first jobs is greater than the number of the second jobs and the running state information of the first job is in an idle state, the primary and standby database clusters meet a preset database switching condition;
[0121] The condition not satisfied module is used for, if the first job quantity is equal to the second job quantity, or the first job running status information is in the running state, then the primary and standby database clusters do not satisfy the preset database switching condition.
[0122] Optionally, the switching module 30 includes:
[0123] A closing module, configured to close the data writing signal of the first database to stop data writing to the first database if the active and standby database clusters meet the database switching condition;
[0124] A status acquisition module, used for acquiring the current running status information of the second job of the first database after closing the data writing signal of the first database;
[0125] The database switching module is used to determine whether the second job running status information is in an idle state. If the second job running status information is in an idle state, the first database and the second database are switched between the primary and secondary databases to obtain the switched first database and the switched second database.
[0126] Optionally, the switching module 30 further includes:
[0127] A process switching module, used to switch the service access process to the first database to obtain the switched first database, wherein the service access process is used to read data from the database;
[0128] The start module is used to start the data writing signal of the second database to obtain the switched second database to start the data writing of the second database.
[0129] Optionally, the primary / standby database switching device further comprises:
[0130] An attribute information acquisition module, used to acquire first database attribute information of the first database and second database attribute information of the second database;
[0131] A file acquisition module, used for acquiring a source data file to be written in response to a data writing instruction;
[0132] A first conversion module is used to convert the source data file into a first standard file corresponding to the attribute information of the first database when the first database is a main database and the second database is a standby database, so that the first database can write data into the first standard file;
[0133] The second conversion module is used to convert the source data file into a second standard file corresponding to the attribute information of the second database when the first database is a standby database and the second database is a primary database, so that the second database can write data to the second standard file.
[0134] The master-standby database switching device provided by the present application adopts the master-standby database switching method in the above embodiment, and can solve the technical problem of master-standby database switching. Compared with the prior art, the beneficial effects of the master-standby database switching device provided by the present application are the same as the beneficial effects of the master-standby database switching method provided by the above embodiment, and other technical features in the master-standby database switching device are the same as the features disclosed in the above embodiment method, which will not be repeated here.
[0135] The present application provides a master-standby database switching device, which includes: at least one processor; and a memory connected to the at least one processor in communication; wherein the memory stores instructions that can be executed by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can execute the master-standby database switching method in the above-mentioned embodiment 1.
[0136] Reference below Fig.10 , which shows a schematic diagram of the structure of a master-standby database switching device suitable for implementing the embodiment of the present application. The master-standby database switching device in the embodiment of the present application may include but is not limited to mobile terminals such as mobile phones, laptop computers, digital broadcast receivers, PDAs (Personal Digital Assistants), PADs (Portable Application Descriptions), PMPs (Portable Media Players), vehicle-mounted terminals (such as vehicle-mounted navigation terminals), etc., and fixed terminals such as digital TVs, desktop computers, etc. Fig.10The primary-standby database switching device shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present application.
[0137] like Fig.10 As shown, the master-standby database switching device may include a processing device 1001 (e.g., a central processing unit, a graphics processor, etc.), which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM: Read Only Memory) 1002 or a program loaded from a storage device 1003 to a random access memory (RAM: Random Access Memory) 1004. In RAM1004, various programs and data required for the operation of the master-standby database switching device are also stored. The processing device 1001, ROM1002, and RAM1004 are connected to each other through a bus 1005. An input / output (I / O) interface 1006 is also connected to the bus. Generally, the following systems can be connected to the I / O interface 1006: an input device 1007 including, for example, a touch screen, a touch pad, a keyboard, a mouse, an image sensor, a microphone, an accelerometer, a gyroscope, etc.; an output device 1008 including, for example, a liquid crystal display (LCD: Liquid Crystal Display), a speaker, a vibrator, etc.; a storage device 1003 including, for example, a magnetic tape, a hard disk, etc.; and a communication device 1009. The communication device 1009 can allow the master-standby database switching device to communicate with other devices wirelessly or by wire to exchange data. Although the figure shows a master-standby database switching device with various systems, it should be understood that it is not required to implement or have all the systems shown. More or fewer systems can be implemented or provided instead.
[0138] In particular, according to the embodiments disclosed in the present application, the process described above with reference to the flowchart can be implemented as a computer software program. For example, the embodiments disclosed in the present application include a computer program product, which includes a computer program carried on a computer-readable medium, and the computer program includes a program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from a network through a communication device, or installed from a storage device 1003, or installed from a ROM 1002. When the computer program is executed by the processing device 1001, the above-mentioned functions defined in the method of the embodiment disclosed in the present application are executed.
[0139] The master-standby database switching device provided by the present application adopts the master-standby database switching method in the above embodiment, which can solve the technical problem of master-standby database switching. Compared with the prior art, the beneficial effects of the master-standby database switching device provided by the present application are the same as the beneficial effects of the master-standby database switching method provided by the above embodiment, and other technical features in the master-standby database switching device are the same as the features disclosed in the method of the previous embodiment, which will not be repeated here.
[0140] It should be understood that the various parts disclosed in this application can be implemented by hardware, software, firmware or a combination thereof. In the description of the above embodiments, specific features, structures, materials or characteristics can be combined in any one or more embodiments or examples in a suitable manner.
[0141] The above is only a specific implementation of the present application, but the protection scope of the present application is not limited thereto. Any person skilled in the art who is familiar with the present technical field can easily think of changes or substitutions within the technical scope disclosed in the present application, which should be included in the protection scope of the present application. Therefore, the protection scope of the present application should be based on the protection scope of the claims.
[0142] The present application provides a computer-readable storage medium having computer-readable program instructions (ie, computer programs) stored thereon, and the computer-readable program instructions are used to execute the primary-standby database switching method in the above-mentioned embodiment.
[0143] The computer-readable storage medium provided in the present application may be, for example, a USB flash drive, but is not limited to electrical, magnetic, optical, electromagnetic, infrared, or semiconductor systems, systems or devices, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In this embodiment, the computer-readable storage medium may be any tangible medium containing or storing a program that can be used by or in combination with an instruction execution system, system or device. The program code contained on the computer-readable storage medium may be transmitted using any appropriate medium, including but not limited to: wires, optical cables, RF (Radio Frequency), etc., or any suitable combination of the above.
[0144] The computer-readable storage medium may be included in the primary-standby database switching device; or may exist independently without being assembled into the primary-standby database switching device.
[0145] The computer-readable storage medium carries one or more programs. When the one or more programs are executed by the master-standby database switching device, the master-standby database switching device performs master-standby database switching.
[0146] Computer program code for performing the operations of the present application may be written in one or more programming languages or a combination thereof, including object-oriented programming languages such as Java, Smalltalk, C++, and conventional procedural programming languages such as "C" or similar programming languages. The program code may be executed entirely on the user's computer, partially on the user's computer, as a separate software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In the case of a remote computer, the remote computer may be connected to the user's computer through any type of network, including a local area network (LAN) or a wide area network (WAN), or may be connected to an external computer (e.g., via the Internet using an Internet service provider).
[0147] The flow chart and block diagram in the accompanying drawings illustrate the possible architecture, function and operation of the system, method and computer program product according to various embodiments of the present application. In this regard, each square box in the flow chart or block diagram can represent a module, a program segment or a part of a code, and the module, the program segment or a part of the code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the square box can also occur in a sequence different from that marked in the accompanying drawings. For example, two square boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each square box in the block diagram and / or flow chart, and the combination of the square boxes in the block diagram and / or flow chart can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.
[0148] The modules involved in the embodiments described in this application may be implemented by software or hardware, wherein the name of the module does not constitute a limitation on the unit itself in some cases.
[0149] The readable storage medium provided in the present application is a computer-readable storage medium, which stores computer-readable program instructions (i.e., computer programs) for executing the above-mentioned primary and standby database switching method, and can solve the technical problem of primary and standby database switching. Compared with the prior art, the beneficial effects of the computer-readable storage medium provided in the present application are the same as the beneficial effects of the primary and standby database switching method provided in the above-mentioned embodiment, and will not be repeated here.
[0150] The present application also provides a computer program product, including a computer program, which implements the steps of the above-mentioned primary and standby database switching method when executed by a processor.
[0151] The computer program product provided by the present application can solve the technical problem of master-slave database switching. Compared with the prior art, the beneficial effects of the computer program product provided by the present application are the same as the beneficial effects of the master-slave database switching method provided by the above embodiment, which will not be repeated here.
[0152] The above descriptions are only some embodiments of the present application, and are not intended to limit the patent scope of the present application. All equivalent structural changes made using the contents of the present application specification and drawings under the technical concept of the present application, or direct / indirect applications in other related technical fields are included in the patent protection scope of the present application.
Claims
1. A method for switching between a primary and a standby database, characterized in that: Applied to a scheduling platform, the scheduling platform is communicatively connected with a master-slave database cluster, the master-slave database cluster includes a first database and a second database, and the method for switching the master-slave database includes: In response to a data write instruction, first operation information of the first database and second operation information of the second database are acquired, wherein both the first database and the second database have data write and data read capabilities, the first database is a primary database, and the second database is a secondary database; or, the first database is a secondary database, and the second database is a primary database; the primary database is used for data write, and the secondary database is used for data read; Based on the first job information and the second job information, determining whether the active / standby database cluster meets a preset database switching condition; If the master-slave database cluster meets the database switching condition, the master-slave database switching is performed on the first database and the second database to obtain the switched first database and the switched second database.
2. The method for switching between the primary and standby databases according to claim 1, characterized in that: The first database is a primary database, and the second database is a secondary database; the step of judging whether the primary and secondary database clusters meet a preset database switching condition based on the first job information and the second job information includes: Determine the number of first jobs and first job running status information of the first database based on the first job information, and determine the number of second jobs of the second database based on the second job information; Based on the first job quantity, the second job quantity, and the first job running status information, it is determined whether the active-standby database cluster meets a preset database switching condition.
3. The method for switching between the primary and standby databases according to claim 2, wherein: The step of judging whether the active / standby database cluster meets a preset database switching condition based on the first job quantity, the second job quantity, and the first job running status information includes: Determine whether the number of the first jobs is greater than the number of the second jobs, and determine whether the running state information of the first job is in an idle state; If the number of the first jobs is greater than the number of the second jobs, and the running status information of the first job is in an idle state, the active-standby database cluster meets the preset database switching condition; If the number of the first jobs is equal to the number of the second jobs, or the running status information of the first job is in a running state, the active-standby database cluster does not meet the preset database switching condition.
4. The method for switching between the primary and standby databases according to claim 3, characterized in that: If the active / standby database cluster meets the database switching condition, the step of performing active / standby database switching on the first database and the second database to obtain the switched first database and the switched second database includes: If the active / standby database cluster meets the database switching condition, turning off the data writing signal of the first database to stop data writing of the first database; After closing the data writing signal of the first database, obtaining the current running status information of the second job of the first database; Determine whether the second job running status information is in an idle state. If the second job running status information is in an idle state, perform a primary-slave database switch on the first database and the second database to obtain a switched first database and a switched second database.
5. The method for switching between the primary and standby databases according to claim 1, wherein: The first database is a primary database, and the second database is a secondary database; the step of switching the first database and the second database between the primary and secondary databases to obtain the switched first database and the switched second database includes: Switching the service access process to the first database to obtain the switched first database, wherein the service access process is used to read data from the database; The data writing signal of the second database is turned on to obtain the switched second database to start writing data into the second database.
6. The method for switching between a primary and a standby database according to any one of claims 1 to 5, characterized in that: The method for switching the primary and standby databases also includes: Acquire first database attribute information of the first database and second database attribute information of the second database; In response to a data writing instruction, obtaining a source data file to be written; In a case where the first database is a primary database and the second database is a standby database, converting the source data file into a first standard file corresponding to the attribute information of the first database, so that the first database writes data into the first standard file; When the first database is a standby database and the second database is a primary database, the source data file is converted into a second standard file corresponding to the attribute information of the second database, so that the second database can write data into the second standard file.
7. A master-slave database switching device, characterized in that: The device comprises: an acquisition module, configured to acquire first operation information of the first database and second operation information of the second database in response to a data write instruction, wherein both the first database and the second database have data write and data read capabilities, the first database is a primary database, and the second database is a standby database; or, the first database is a standby database, and the second database is a primary database; the primary database is used for data write, and the standby database is used for data read; A judgment module, used to judge whether the active and standby database clusters meet a preset database switching condition based on the first job information and the second job information; The switching module is used to perform a master-slave database switch on the first database and the second database if the master-slave database cluster meets the database switching condition, so as to obtain a switched first database and a switched second database.
8. A master-slave database switching device, characterized in that: The device comprises: a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the computer program is configured to implement the steps of the primary-standby database switching method according to any one of claims 1 to 6.
9. A storage medium, characterized in that: The storage medium is a computer-readable storage medium, and a computer program is stored on the storage medium. When the computer program is executed by a processor, the steps of the primary-standby database switching method according to any one of claims 1 to 6 are implemented.
10. A computer program product, characterized in that The computer program product comprises a computer program, and when the computer program is executed by a processor, the steps of the method for switching between a primary and a standby database according to any one of claims 1 to 6 are implemented.