A method and system for hot-cutting a MySql database in a production environment
By providing a method and system for eager database libraries in a production environment, switching the data source of MySQL database without shutting down is achieved, solving the downtime problem during database switching in the prior art, and improving user experience and data security.
Patent Information
- Application Number
- CN202410783088.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-18
- Publication Date
- 2025-04-29
- Estimated Expiration
- 2044-06-18
AI Technical Summary
The lack of a method of actively switching MySQL databases without shutting down is the lack of a method in the prior art, which usually requires switching between the master and slave databases after the database or server is shut down during the actual production process, which affects the user's user experience and may cause data loss.
Provide a method and system for eager databases of MySql databases in production environments. When the application service does not shut down, switch the data source of the MySql database. By obtaining database switching instructions, confirm that the second database is synchronizing the data of the first database and is not read-only, modify the configuration information of the configuration center, and make each application service switch the database according to the configuration information.
It realizes switching the data source of the MySQL database without shutting down, ensuring data security and improving user experience, and solving the problem of data source inconsistency during database switching.
Smart Images

Figure CN119003539B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to computer technology and database technology, and in particular to a method and system for hot-cutting a MySql database in a production environment. Background Art
[0002] MySQL database is a small and medium-sized relational database management system, which is currently widely used in large, medium and small websites on the Internet. Due to its small size, fast speed, low total cost of ownership, and especially the open source feature, MySQL database is increasingly widely used. Many large, medium and small websites choose MySQL as their website database in order to reduce the total cost of ownership of the website. With the development of the Internet and big data, people pay more and more attention to the security and reliability of databases in the process of using databases to store data. Therefore, a single database architecture can no longer meet the actual production and application needs. The existing database solution usually builds a master-slave synchronization database architecture to further ensure the security and reliability of the database. However, under the MySQL master-slave replication architecture with multiple application instances, the prior art lacks a method for actively switching MySQL databases without downtime. In the actual production process, it is usually necessary to shut down the database or server before switching the master-slave database. Since the database or server is usually in a downtime state during the database switching process, it means that the business or services related to the database are all stopped. Therefore, this shutdown database switching solution greatly affects the user experience and may also cause data loss.
[0003] In addition, when shutting down the MySQL database server for maintenance, reinstallation, migration, etc., it is inevitable that the database server will shut down and data will be lost. Currently, most database migrations in the industry are based on MHA, KeepAlived virtual IP, etc., which will also cause the problem that multiple application instances cannot take effect at the same time during the migration process. Summary of the invention
[0004] In response to the above technical problems, the present application provides a method and system for hot-cutting a MySql database in a production environment, which switches the data source of the MySql database without shutting down the application service, implements hot-cutting a MySql database, and improves the user experience while ensuring data security.
[0005] In a first aspect, the present application provides a method for hot-cutting a MySql database in a production environment, comprising:
[0006] Get the database switching instruction;
[0007] When the second database is synchronizing data of the first database and the second database is not in read-only state, modify the configuration information of the configuration center, and switch the database currently corresponding to the MySql database from the first database to the second database; wherein the first database is the database currently corresponding to the MySql database, and the second database is the database to be switched;
[0008] A response is initiated to each application service of the MySql database, so that each application service switches the currently corresponding database according to the configuration information, wherein each application service preloads the data source information of the first database and the second database when starting.
[0009] The embodiment of the present application provides a method for MySql database hot-cut library in a production environment. After obtaining the database switching instruction, the synchronization status of the second database is first confirmed to ensure that the second database is synchronizing the data of the first database and is not in a read-only state, thereby ensuring the consistency of the data in the two databases and the availability of the second database, so that each application service can still run normally and stably after the database is switched. When the second database is synchronizing the data of the first database, and the second database is not in a read-only state, the embodiment of the present application modifies the configuration information of the configuration center and sends the configuration information to each application service in the form of a response, so that each application service can switch the current corresponding database according to the configuration information, and switch the data source of the MySql database without shutting down the application service, thereby realizing the MySql database hot-cut library, and improving the user experience while ensuring data security. In the embodiment of the present application, the problem of inconsistent data sources at the moment of database switching is solved:
[0010] (1) The database to which the data was written at the last moment is the first database. At this time, the application thread context is still using the first database, so it does not affect data writing, and the data will be synchronized to the second database normally.
[0011] (2) The database to which the data is currently written is the second database, and the data can be stored normally.
[0012] (3) After the database instance is migrated, the read and write data sources of all application services are the second database, and data migration is completed smoothly.
[0013] In a possible implementation manner, the method further includes confirming that the second database is synchronizing data of the first database and confirming that the second database is not in a read-only state, wherein confirming that the second database is synchronizing data of the first database includes:
[0014] Acquiring synchronization status information of the second database through program instructions;
[0015] Checking multiple preset fields in the synchronization status information to confirm whether the preset synchronization thread is running normally, including: confirming whether the replication data thread is running normally, confirming whether the replication I / O thread is running normally, and confirming whether the replication SQL thread is running normally;
[0016] It is confirmed whether the time for data synchronization of the second database to lag behind that of the first database is within a preset range.
[0017] In the embodiment of the present application, the synchronization status information of the second database is used to confirm whether the second database is synchronizing the data of the first database. First, multiple preset fields in the synchronization status information are checked to confirm whether multiple synchronization threads are running normally. At the same time, a synchronization time difference will inevitably occur during the data synchronization process, that is, the data synchronization of the second database lags behind the time of the first database. If the synchronization time difference exceeds the preset range, it means that although the second database is synchronizing the data of the first database at this time, the delay is too high. At this time, switching the database will increase the user's waiting time and may have the risk of data errors. Therefore, it is necessary to confirm that the synchronization time difference of the second database is within the preset range to ensure data consistency and security.
[0018] Further, the confirming that the second database is not in a read-only state includes:
[0019] Query the current status of the second database to confirm whether the second database is in read-only status;
[0020] When it is confirmed that the second database is in a read-only state, a corresponding configuration of the second database is modified so that the second database is not in a read-only state.
[0021] In the embodiment of the present application, the specific steps for confirming that the second database is not in a read-only state are limited. After the database switching is implemented, each application service will use the second database for read and write operations accordingly. Therefore, in order to ensure the normal operation of each application service, the second database cannot be in a read-only state. This embodiment queries the current state of the database before switching the database, and can quickly confirm whether the second database is in a read-only state. Furthermore, when it is confirmed that the second database is in a read-only state, the current state of the second database can be changed by modifying the configuration so that the database switching can proceed normally.
[0022] In a possible implementation, each application service switches a currently corresponding database according to the configuration information, including:
[0023] Each application service establishes a communication connection with the configuration center in a long polling mode;
[0024] Each application service continuously obtains response information from the configuration center;
[0025] Each application service determines whether the configuration information has changed according to the response information;
[0026] When the configuration information changes, each of the application services switches the currently corresponding database according to the configuration information.
[0027] The embodiment of the present application provides a method for each application service to switch databases. In this embodiment, each application service establishes a communication connection with the configuration center in a long polling mode. The long polling mode means that in each request round, the application service initiates a long polling request to the configuration center, but at this time the configuration center will not return the request result immediately, but will suspend the request until there is a configuration change or the request times out. The application service will initiate another long polling request to the configuration center after receiving the response, and the request will be repeated. Therefore, when the configuration information of the configuration center changes, each application service can obtain the response information of the configuration center in time, and switch the current corresponding database according to the response information, so as to realize the hot database of the MySql database.
[0028] Further, when the MySql database is running normally, if the database currently corresponding to the MySql database is the first database, the second database performs data synchronization according to the binlog log of the first database;
[0029] If the database currently corresponding to the MySql database is the second database, the first database performs data synchronization according to the binlog log of the second database.
[0030] In the second aspect, accordingly, the embodiment of the present application provides a system for hot-cutting a MySql database in a production environment, including an acquisition module, a cutting library module and a response module;
[0031] Wherein, the acquisition module is used to acquire a database switching instruction;
[0032] The database switching module is used to modify the configuration information of the configuration center and switch the database currently corresponding to the MySql database from the first database to the second database when the second database is synchronizing the data of the first database and the second database is not in a read-only state; wherein the first database is the database currently corresponding to the MySql database, and the second database is the database to be switched;
[0033] The response module is used to initiate a response to each application service of the MySql database, so that each application service switches the currently corresponding database according to the configuration information, wherein each application service preloads the data source information of the first database and the second database when starting.
[0034] In a possible implementation, the system further includes a first confirmation module and a second confirmation module, the first confirmation module is used to confirm that the second database is synchronizing the data of the first database, and the second confirmation module is used to confirm that the second database is not in a read-only state, wherein the first confirmation module includes a synchronization information acquisition unit, a synchronization state checking unit, and a synchronization time difference confirmation unit;
[0035] Wherein, the synchronization information acquisition unit is used to acquire the synchronization status information of the second database through program instructions;
[0036] The synchronization status checking unit is used to check multiple preset fields in the synchronization status information to confirm whether the preset synchronization thread is running normally, including: confirming whether the replication data thread is running normally, confirming whether the replication I / O thread is running normally, and confirming whether the replication SQL thread is running normally;
[0037] The synchronization time difference confirmation unit is used to confirm whether the time by which the data synchronization of the second database lags behind that of the first database is within a preset range.
[0038] Further, the second confirmation module includes a status query unit and a configuration modification unit;
[0039] The state query unit is used to query the current state of the second database to confirm whether the second database is in a read-only state;
[0040] The configuration modification unit is used to modify the corresponding configuration of the second database when it is confirmed that the second database is in the read-only state, so that the second database is not in the read-only state.
[0041] In a possible implementation, each application service switches a currently corresponding database according to the configuration information, including:
[0042] Each application service establishes a communication connection with the configuration center in a long polling mode;
[0043] Each application service continuously obtains response information from the configuration center;
[0044] Each application service determines whether the configuration information has changed according to the response information;
[0045] When the configuration information changes, each of the application services switches the currently corresponding database according to the configuration information.
[0046] Further, when the MySql database is running normally, if the database currently corresponding to the MySql database is the first database, the second database performs data synchronization according to the binlog log of the first database;
[0047] If the database currently corresponding to the MySql database is the second database, the first database performs data synchronization according to the binlog log of the second database. BRIEF DESCRIPTION OF THE DRAWINGS
[0048] Figure 1 : A flow chart of an embodiment of a method for hot-cutting a MySql database in a production environment provided by the present application.
[0049] Figure 2 : A long polling mode flow chart of an embodiment of a method for hot-cutting a MySql database in a production environment provided by the present application.
[0050] Figure 3 : A structural diagram of an embodiment of a MySql database hot-cut library system in a production environment provided by the present application.
[0051] Figure 4 : A schematic diagram of the first confirmation module structure of an embodiment of a MySql database hot-cut library system in a production environment provided by the present application.
[0052] Figure 5 : A schematic diagram of the second confirmation module structure of an embodiment of a MySql database hot-cut library system in a production environment provided by the present application. DETAILED DESCRIPTION
[0053] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0054] It should be noted that the step numbers in the text are only for the convenience of explanation of the specific embodiments and do not serve to limit the order in which the steps are executed. In the description of this application, the terms "first" and "second" are only used for descriptive purposes and cannot be understood as indicating or implying relative importance or implicitly indicating the number of the indicated technical features. Therefore, the features defined as "first" and "second" may explicitly or implicitly include one or more of the features.
[0055] Embodiment 1:
[0056] like Figure 1 As shown, embodiment 1 provides a method for hot-cutting a MySql database in a production environment, including steps S1-S3:
[0057] Step S1, obtaining a database switching instruction;
[0058] Step S2: when the second database is synchronizing data of the first database and the second database is not in read-only state, modify the configuration information of the configuration center, and switch the database currently corresponding to the MySql database from the first database to the second database; wherein the first database is the database currently corresponding to the MySql database, and the second database is the database to be switched;
[0059] Step S3: Initiate a response to each application service of the MySql database, so that each application service switches the currently corresponding database according to the configuration information, wherein each application service preloads the data source information of the first database and the second database when starting.
[0060] The embodiment of the present application provides a method for MySql database hot-cut library in a production environment. After obtaining the database switching instruction, the synchronization status of the second database is first confirmed to ensure that the second database is synchronizing the data of the first database and is not in a read-only state, thereby ensuring the consistency of the data in the two databases and the availability of the second database, so that each application service can still run normally and stably after the database is switched. When the second database is synchronizing the data of the first database, and the second database is not in a read-only state, the embodiment of the present application modifies the configuration information of the configuration center and sends the configuration information to each application service in the form of a response, so that each application service can switch the current corresponding database according to the configuration information, and switch the data source of the MySql database without shutting down the application service, thereby realizing the MySql database hot-cut library, and improving the user experience while ensuring data security. In the embodiment of the present application, the problem of inconsistent data sources at the moment of database switching is solved:
[0061] (1) The database to which the data was written at the last moment is the first database. At this time, the application thread context is still using the first database, so it does not affect data writing, and the data will be synchronized to the second database normally.
[0062] (2) The database to which the data is currently written is the second database, and the data can be stored normally.
[0063] (3) After the database instance is migrated, the read and write data sources of all application services are the second database, and data migration is completed smoothly.
[0064] In a possible implementation manner, the method further includes confirming that the second database is synchronizing data of the first database and confirming that the second database is not in a read-only state, wherein confirming that the second database is synchronizing data of the first database includes:
[0065] Acquiring synchronization status information of the second database through program instructions;
[0066] Checking multiple preset fields in the synchronization status information to confirm whether the preset synchronization thread is running normally, including: confirming whether the replication data thread is running normally, confirming whether the replication I / O thread is running normally, and confirming whether the replication SQL thread is running normally;
[0067] It is confirmed whether the time for data synchronization of the second database to lag behind that of the first database is within a preset range.
[0068] In a preferred embodiment, the command SHOW SLAVE STATUS\G is used on the second database to obtain the synchronization status information of the second database, and the following fields in the synchronization status information are checked:
[0069] Slave_IO_State: If this field displays "Waiting for master to send event" or "Connecting to master", it means that the replication data thread is running normally.
[0070] Slave_IO_Running: If this field displays "Yes", it means that the replication I / O thread is running normally.
[0071] Slave_SQL_Running: If this field displays "Yes", it means that the replication SQL thread is running normally.
[0072] Seconds_Behind_Master: If the value of this field is greater than 0, it indicates the time (in seconds) that the slave server lags behind the master server. The smaller the value, the better. Confirm that the value is within the preset range.
[0073] In the embodiment of the present application, the synchronization status information of the second database is used to confirm whether the second database is synchronizing the data of the first database. First, multiple preset fields in the synchronization status information are checked to confirm whether multiple synchronization threads are running normally. At the same time, a synchronization time difference will inevitably occur during the data synchronization process, that is, the data synchronization of the second database lags behind the time of the first database. If the synchronization time difference exceeds the preset range, it means that although the second database is synchronizing the data of the first database at this time, the delay is too high. At this time, switching the database will increase the user's waiting time and may have the risk of data errors. Therefore, it is necessary to confirm that the synchronization time difference of the second database is within the preset range to ensure data consistency and security.
[0074] Further, the confirming that the second database is not in a read-only state includes:
[0075] Query the current status of the second database to confirm whether the second database is in read-only status;
[0076] When it is confirmed that the second database is in a read-only state, a corresponding configuration of the second database is modified so that the second database is not in a read-only state.
[0077] In a preferred embodiment, the current status information of the second database instance is queried through the command line "SHOW GLOBAL VARIABLES LIKE'READ_ONLY'". If the value of the READ_ONLY variable is ON, it means that the second database instance is running in read-only mode. At this time, the status of the database is modified through the command line "SET GLOBAL read_only=0". In addition, when configuring the second database instance, the embodiment of the present application will set read_only=0 in the my.cnf configuration, so that the initial state of the second database instance is not read-only. At the same time, when the second database instance is connected to the MySQL instance, set global read_only=off will be executed, so that the second database instance is also not in read-only state after connecting to the MySQL instance.
[0078] In the embodiment of the present application, the specific steps for confirming that the second database is not in a read-only state are limited. After the database switching is implemented, each application service will use the second database for read and write operations accordingly. Therefore, in order to ensure the normal operation of each application service, the second database cannot be in a read-only state. This embodiment queries the current state of the database before switching the database, and can quickly confirm whether the second database is in a read-only state. Furthermore, when it is confirmed that the second database is in a read-only state, the current state of the second database can be changed by modifying the configuration so that the database switching can proceed normally.
[0079] In a preferred embodiment, in step S2, the configuration information of the configuration center is modified as follows:
[0080] ①Configuration center configuration setting SWITCH_DATABASE=true
[0081] ②Configuration center configuration setting SWITCH_DATABASE_GROUP = true
[0082] In a possible implementation, in step S3, each application service switches the currently corresponding database according to the configuration information, including:
[0083] Each application service establishes a communication connection with the configuration center in a long polling mode;
[0084] Each application service continuously obtains response information from the configuration center;
[0085] Each application service determines whether the configuration information has changed according to the response information;
[0086] When the configuration information changes, each of the application services switches the currently corresponding database according to the configuration information.
[0087] The embodiment of the present application provides a method for each application service to switch databases. In this embodiment, each application service establishes a communication connection with the configuration center in a long polling mode. The long polling mode means that in each request round, the application service initiates a long polling request to the configuration center, but at this time the configuration center will not return the request result immediately, but will suspend the request until there is a configuration change or the request times out. The application service will initiate another long polling request to the configuration center after receiving the response, and the request will be repeated. Therefore, when the configuration information of the configuration center changes, each application service can obtain the response information of the configuration center in time, and switch the current corresponding database according to the response information, so as to realize the hot database of the MySql database.
[0088] In a preferred embodiment, each of the application services will preload the data source information of the first database and the second database when it is started, specifically:
[0089] ① You need to configure two sets of data sources in advance in the environment variables configured locally in the application service, namely data source DataSourceA and data source DataSourceB.
[0090] ②When the application service is started, single-database data sources and multi-database data sources have different loading methods. The following are different ways to load data sources:
[0091] Single database data source loading method:
[0092] 1) When the application service starts, two sets of data sources and an implementation class MainRoutingDataSource of the data source routing AbstractRoutingDataSource abstract base class are loaded into memory through Spring Bean injection.
[0093] How to load multiple database data sources:
[0094] When the Spring container is initialized, the init() method is executed to loop and load 32 database data sources dbA (0-31) and 32 data sources dbB (0-31) and load a data source routing AbstractRoutingDataSource abstract base class implementation class ShardingDataSource into memory.
[0095] The difference between the multi-library loading method and the single-library loading method is that the single-library needs to create two data source beans and a data routing bean during initialization, while the multi-library uses the @PostConstruct annotation to execute the init() method when the Spring container is initialized, and cyclically loads 32 data sources dbA (0-31) and 32 data sources dbB (0-31) of the library. The reason for using the encoding form to dynamically register the data source is to avoid manually writing 32+32 database beans, so that different numbers of sub-library data sources can be registered by changing the number of loaded databases (32) to other numbers.
[0096] After the application service is started and initialized, the data source read is dataSourceA or dbA (0-31).
[0097] During the normal operation of the MySql database, such as Figure 2 As shown, each application service establishes a communication connection with the configuration center in long polling mode. During the long polling process, the application service initiates a long polling request to the configuration center. However, the configuration center does not return the request result immediately at this time, but suspends the request until there is a configuration change or the request times out. The application service will respond to the application service only after receiving the response. It will initiate another long polling request to the configuration center. This repeated communication connection mode is the long polling mode.
[0098] Furthermore, when the application service perceives the configuration change, before obtaining the DataSource data source, it will determine the configuration of the configuration center through the Aop aspect method to determine which data source the current thread should use getDataSource(). Similarly, this is also divided into single-library data source and multi-library data source, specifically:
[0099] ① The Aop aspect method of a single database data source determines the data source used by the thread
[0100] 1) The data source routing implementation class MainRoutingDataSource inherits from the AbstractRoutingDataSource abstract base class provided by the Spring framework to implement the data source routing function.
[0101] 2) Override the determineCurrentLookupKey() method in the base class AbstractRoutingDataSource, where the implementation in the method switches the data source according to the configuration of the configuration center.
[0102] 3) When LOAD_SWITCH_DATABASE=true and SWITCH_DATABASE=true, dataSourceB data source will be returned. When either LOAD_SWITCH_DATABASE or SWITCH_DATABASE is not true, dataSourceA data source will be returned.
[0103] ②The Aop aspect method of the multi-database data source determines the data source used by the thread
[0104] 1) Override the determineCurrentLookupKey() method in the base class AbstractRoutingDataSource, where the implementation in the method switches the data source according to the configuration of the configuration center.
[0105] 2) When LOAD_SWITCH_DATABASE_GROUP = true and SWITCH_DATABASE = true, the dbB (0-31) data source will be returned. When either LOAD_SWITCH_DATABASE_GROUP or SWITCH_DATABASE is not true, the dbA (0-31) data source will be returned.
[0106] The embodiment of the present application implements the MySQL hot-cut library through the AbstractRoutingDataSource data source routing provided by the Spring framework, and determines which data source the application service uses through the configuration of the configuration center and the defined routing rules. It allows the same application service to dynamically select different data sources for read and write operations during operation. At the same time, it also greatly simplifies data configuration, making it easier to configure multiple data sources, and realizing dynamic switching of data sources without sacrificing performance. At the same time, centralized management of multiple data source configurations through the configuration center reduces the possibility of configuration errors and makes the configuration clearer.
[0107] Further, when the MySql database is running normally, if the database currently corresponding to the MySql database is the first database, the second database performs data synchronization according to the binlog log of the first database;
[0108] If the database currently corresponding to the MySql database is the second database, the first database performs data synchronization according to the binlog log of the second database.
[0109] Embodiment 2:
[0110] Accordingly, the second embodiment provides a system for hot-cutting a MySql database in a production environment, such as Figure 3 As shown, it includes an acquisition module 10, a library cutting module 20 and a response module 30;
[0111] Wherein, the acquisition module 10 is used to acquire a database switching instruction;
[0112] The database switching module 20 is used to modify the configuration information of the configuration center and switch the database currently corresponding to the MySql database from the first database to the second database when the second database is synchronizing the data of the first database and the second database is not in a read-only state; wherein the first database is the database currently corresponding to the MySql database, and the second database is the database to be switched;
[0113] The response module 30 is used to initiate a response to each application service of the MySql database, so that each application service switches the currently corresponding database according to the configuration information, wherein each application service preloads the data source information of the first database and the second database when starting.
[0114] In one possible implementation, Figure 4As shown, the system further includes a first confirmation module 40 and a second confirmation module 50, the first confirmation module 40 is used to confirm that the second database is synchronizing the data of the first database, and the second confirmation module 50 is used to confirm that the second database is not in a read-only state, wherein the first confirmation module 40 includes a synchronization information acquisition unit 401, a synchronization state checking unit 402 and a synchronization time difference confirmation unit 403;
[0115] The synchronization information acquisition unit 401 is used to acquire the synchronization status information of the second database through program instructions;
[0116] The synchronization status checking unit 402 is used to check multiple preset fields in the synchronization status information to confirm whether the preset synchronization thread is running normally, including: confirming whether the replication data thread is running normally, confirming whether the replication I / O thread is running normally, and confirming whether the replication SQL thread is running normally;
[0117] The synchronization time difference confirmation unit 403 is used to confirm whether the time when the data synchronization of the second database lags behind that of the first database is within a preset range.
[0118] Further, such as Figure 5 As shown, the second confirmation module 50 includes a status query unit 501 and a configuration modification unit 502;
[0119] The state query unit 501 is used to query the current state of the second database to confirm whether the second database is in a read-only state;
[0120] The configuration modification unit 502 is used to modify the corresponding configuration of the second database when it is confirmed that the second database is in the read-only state, so that the second database is not in the read-only state.
[0121] In a possible implementation, each application service switches a currently corresponding database according to the configuration information, including:
[0122] Each application service establishes a communication connection with the configuration center in a long polling mode;
[0123] Each application service continuously obtains response information from the configuration center;
[0124] Each application service determines whether the configuration information has changed according to the response information;
[0125] When the configuration information changes, each of the application services switches the currently corresponding database according to the configuration information.
[0126] Further, when the MySql database is running normally, if the database currently corresponding to the MySql database is the first database, the second database performs data synchronization according to the binlog log of the first database;
[0127] If the database currently corresponding to the MySql database is the second database, the first database performs data synchronization according to the binlog log of the second database.
[0128] The embodiment of the present application provides a system for MySql database hot-cut library in a production environment. After obtaining the database switching instruction, the synchronization status of the second database is first confirmed to ensure that the second database is synchronizing the data of the first database and is not in a read-only state, thereby ensuring the consistency of the data in the two databases and the availability of the second database, so that each application service can still run normally and stably after the database is switched. When the second database is synchronizing the data of the first database, and the second database is not in a read-only state, the embodiment of the present application modifies the configuration information of the configuration center and sends the configuration information to each application service in the form of a response, so that each application service can switch the current corresponding database according to the configuration information, and switch the data source of the MySql database without shutting down the application service, thereby realizing the MySql database hot-cut library, and improving the user experience while ensuring data security. In the embodiment of the present application, the problem of inconsistent data sources at the moment of database switching is solved:
[0129] (1) The database to which the data was written at the last moment is the first database. At this time, the application thread context is still using the first database, so it does not affect data writing, and the data will be synchronized to the second database normally.
[0130] (2) The database to which the data is currently written is the second database, and the data can be stored normally.
[0131] (3) After the database instance is migrated, the read and write data sources of all application services are the second database, and data migration is completed smoothly.
[0132] The more detailed working principle and step flow of this embodiment can refer to, but are not limited to, the relevant records of Embodiment 1.
[0133] The specific embodiments described above further describe the purpose, technical solutions and beneficial effects of the present application in detail. It should be understood that the above description is only a specific embodiment of the present application and is not intended to limit the scope of protection of the present application. It is particularly pointed out that for those skilled in the art, any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application should be included in the scope of protection of the present application.
Claims
1. A method for hot-cutting a MySql database in a production environment, characterized in that: include: Get the database switching instruction; When the second database is synchronizing data of the first database and the second database is not in read-only state, modify the configuration information of the configuration center, and switch the database currently corresponding to the MySql database from the first database to the second database; wherein the first database is the database currently corresponding to the MySql database, and the second database is the database to be switched; Initiating a response to each application service of the MySql database so that each application service switches the currently corresponding database according to the configuration information, wherein each application service preloads the data source information of the first database and the second database when starting, and each application service switches the currently corresponding database according to the configuration information, including: each application service establishes a communication connection with the configuration center in a long polling mode; each application service continuously obtains response information from the configuration center; each application service determines whether the configuration information has changed according to the response information; when the configuration information has changed, each application service switches the currently corresponding database according to the configuration information.
2. A method for hot-cutting a MySql database in a production environment as described in claim 1, characterized in that: The method further includes confirming that the second database is synchronizing data of the first database and confirming that the second database is not in a read-only state, wherein confirming that the second database is synchronizing data of the first database includes: Acquiring synchronization status information of the second database through program instructions; Checking multiple preset fields in the synchronization status information to confirm whether the preset synchronization thread is running normally, including: confirming whether the replication data thread is running normally, confirming whether the replication I / O thread is running normally, and confirming whether the replication SQL thread is running normally; It is confirmed whether the time for data synchronization of the second database to lag behind that of the first database is within a preset range.
3. A method for hot-cutting a MySql database in a production environment as described in claim 2, characterized in that: The confirming that the second database is not in a read-only state includes: Query the current status of the second database to confirm whether the second database is in read-only status; When it is confirmed that the second database is in a read-only state, a corresponding configuration of the second database is modified so that the second database is not in a read-only state.
4. A method for hot-cutting a MySql database in a production environment as described in claim 1, characterized in that: When the MySql database runs normally, if the database currently corresponding to the MySql database is the first database, the second database performs data synchronization according to the binlog log of the first database; If the database currently corresponding to the MySql database is the second database, the first database performs data synchronization according to the binlog log of the second database.
5. A MySql database hot-cut library system in a production environment, characterized in that: It includes acquisition module, library cutting module and response module; Wherein, the acquisition module is used to acquire a database switching instruction; The database switching module is used to modify the configuration information of the configuration center and switch the database currently corresponding to the MySql database from the first database to the second database when the second database is synchronizing the data of the first database and the second database is not in a read-only state; wherein the first database is the database currently corresponding to the MySql database, and the second database is the database to be switched; The response module is used to initiate a response to each application service of the MySql database, so that each application service switches the currently corresponding database according to the configuration information, wherein each application service preloads the data source information of the first database and the second database when starting, and each application service switches the currently corresponding database according to the configuration information, including: each application service establishes a communication connection with the configuration center in a long polling mode; each application service continuously obtains response information from the configuration center; each application service determines whether the configuration information has changed according to the response information; when the configuration information changes, each application service switches the currently corresponding database according to the configuration information.
6. A MySql database hot-cut library system in a production environment as described in claim 5, characterized in that: The system further includes a first confirmation module and a second confirmation module, wherein the first confirmation module is used to confirm that the second database is synchronizing data of the first database, and the second confirmation module is used to confirm that the second database is not in a read-only state, wherein the first confirmation module includes a synchronization information acquisition unit, a synchronization state checking unit, and a synchronization time difference confirmation unit; Wherein, the synchronization information acquisition unit is used to acquire the synchronization status information of the second database through program instructions; The synchronization status checking unit is used to check multiple preset fields in the synchronization status information to confirm whether the preset synchronization thread is running normally, including: confirming whether the replication data thread is running normally, confirming whether the replication I / O thread is running normally, and confirming whether the replication SQL thread is running normally; The synchronization time difference confirmation unit is used to confirm whether the time by which the data synchronization of the second database lags behind that of the first database is within a preset range.
7. A MySql database hot-cut library system in a production environment as described in claim 6, characterized in that: The second confirmation module includes a status query unit and a configuration modification unit; The state query unit is used to query the current state of the second database to confirm whether the second database is in a read-only state; The configuration modification unit is used to modify the corresponding configuration of the second database when it is confirmed that the second database is in the read-only state, so that the second database is not in the read-only state.
8. A MySql database hot-cut library system in a production environment as claimed in claim 5, characterized in that: When the MySql database runs normally, if the database currently corresponding to the MySql database is the first database, the second database performs data synchronization according to the binlog log of the first database; If the database currently corresponding to the MySql database is the second database, the first database performs data synchronization according to the binlog log of the second database.
Citation Information
Patent Citations
Disaster recovery switching method and device, equipment and storage medium
CN117931523A