Data synchronization method and device for master and slave databases, electronic equipment and storage medium
Interceptors intercept and analyze SQL statements, and use message queues to achieve master-slave data synchronization in the MySQL database. This solves the problems of low synchronization efficiency and narrow scope of application in the existing technology, and achieves efficient cross-database synchronization.
Patent Information
- Application Number
- CN202410674253.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-28
- Publication Date
- 2024-10-08
AI Technical Summary
In the existing technology, the master-slave synchronization mechanism of the MySQL database achieves data synchronization by reading binlog files, which results in low synchronization efficiency and is only applicable to the MySQL master-slave synchronization mechanism and not to other master-slave synchronization mechanisms.
Intercept the SQL statement through the interceptor, determine its category, and issue an alarm when the security conditions are met; for the SQL statement of the update type, determine the data identifier to be operated, and if synchronization is required, write it into the message queue and pass the message The queue implements data synchronization, avoids reading binlog files, and is suitable for various relational databases.
It improves database performance, improves data synchronization efficiency, expands the scope of application, and can be applied to other relational databases except MySQL.
Smart Images

Figure CN118747199A_ABST
Abstract
Description
Technical Field
[0001] The embodiments of the present application relate to the field of database technology, and in particular, to a method, device, electronic device and storage medium for synchronizing data between a master and a slave database. Background Art
[0002] In the tide of the information age, database synchronization and data security are particularly important. Especially in the big data environment, ensuring the real-time, consistency and security of data has become a key link in the construction of enterprise informatization.
[0003] In the prior art, for synchronization of MySQL databases, the master-slave synchronization mechanism of MySQL itself is generally used to achieve data synchronization between master loads. Figure 1 As shown, the master database updates the binlog file when data changes occur. The master server reads the binlog file, obtains SQL (Structured Query Language) statements, and filters the SQL statements to filter out the SQL statements that do not need to be synchronized, and sends the SQL statements that need to be synchronized to the load server, which writes them to the slave database.
[0004] The existing technology uses binlog files to achieve synchronization. Reading binlog files affects the performance of the database, resulting in low synchronization efficiency. Moreover, it is only applicable to servers with MySQL master-slave synchronization mechanism, and is not applicable to master-slave synchronization implemented by other mechanisms. Summary of the invention
[0005] The embodiments of the present application provide a data synchronization method, device, electronic device and storage medium for a master-slave database, which are helpful to improve data synchronization efficiency and expand the scope of application.
[0006] In order to solve the above problems, in a first aspect, an embodiment of the present application provides a data synchronization method of a master-slave database, which is applied to a master server and includes:
[0007] intercepting a first SQL statement by an interceptor, and determining a first category of the first SQL statement, where the first category includes a query category or an update category;
[0008] When the first SQL statement meets the security condition, an alarm is issued for the first SQL statement;
[0009] When the first category is an update category, determining a first data identifier to be operated by the first SQL statement, wherein the first data identifier includes a first data table identifier, or the first data identifier includes a first data table identifier and a first field identifier;
[0010] When the first data identifier is a data identifier to be synchronized in the configuration information, the first SQL statement is written into a message queue, and the SQL statement in the message queue is used to synchronize to a slave database of the load server;
[0011] Execute the first SQL statement to operate on the primary database of the primary server.
[0012] In a second aspect, an embodiment of the present application provides a data synchronization method for a master-slave database, which is applied to a load server, including:
[0013] Obtain a first SQL statement from a message queue, the message queue being used to store SQL statements that the master server needs to synchronize to the load server, wherein the first category of the first SQL statement is an update category and the first data identifier to be operated by the first SQL statement is a data identifier to be synchronized in the configuration information; wherein the first data identifier includes a first data table identifier, or the first data identifier includes a first data table identifier and a first field identifier;
[0014] Execute the first SQL statement to operate on the slave database of the load server.
[0015] In a third aspect, an embodiment of the present application provides a data synchronization device for a master-slave database, which is applied to a master server and includes:
[0016] A first category determination module, configured to intercept a first SQL statement through an interceptor and determine a first category of the first SQL statement, wherein the first category includes a query category or an update category;
[0017] An alarm module, configured to generate an alarm for the first SQL statement when the first SQL statement meets a security condition;
[0018] A first identifier determination module, used for determining a first data identifier to be operated by the first SQL statement when the first category is an update category, wherein the first data identifier includes a first data table identifier, or the first data identifier includes a first data table identifier and a first field identifier;
[0019] A first SQL synchronization module, used for writing the first SQL statement into a message queue when the first data identifier is a data identifier to be synchronized in the configuration information, and the SQL statement in the message queue is used for synchronization to a slave database of the load server;
[0020] The first SQL execution module is used to execute the operation of the first SQL statement on the primary database of the primary server.
[0021] In a fourth aspect, an embodiment of the present application provides a data synchronization device for a master-slave database, which is applied to a load server, including:
[0022] A first SQL acquisition module is used to acquire a first SQL statement from a message queue, wherein the message queue is used to store SQL statements that the master server needs to synchronize to the load server, wherein the first category of the first SQL statement is an update category and the first data identifier to be operated by the first SQL statement is a data identifier to be synchronized in the configuration information; wherein the first data identifier includes a first data table identifier, or the first data identifier includes a first data table identifier and a first field identifier;
[0023] The first SQL execution module is used to execute the operation of the first SQL statement on the slave database of the load server.
[0024] In the fifth aspect, an embodiment of the present application also provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the computer program, the master-slave database data synchronization method described in the first aspect or the second aspect is implemented.
[0025] In a sixth aspect, an embodiment of the present application provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the steps of the master-slave database data synchronization method described in the first aspect or the second aspect.
[0026] The master-slave database data synchronization method, device, electronic device and storage medium provided in the embodiment of the present application can perform security analysis on the first SQL statement after intercepting the first SQL statement through the interceptor, and issue an alarm for the first SQL statement that meets the security conditions, which can solve the problem that the master-slave synchronization mechanism of MySQL itself cannot perform security analysis, and can improve the performance of the database. By determining the first category of the first SQL statement, when the first category is an update category, determining the first data identifier to be operated by the first SQL statement, when the first data identifier is a data identifier to be synchronized in the configuration information, the first SQL statement is written into the message queue, and the first SQL statement is executed to operate on the master database of the master server, and the data synchronization in the master-slave database is realized through the message queue, without reading the binlog file, which can avoid the impact on the database performance and improve the data synchronization efficiency. Moreover, the data synchronization method can be applied to various relational databases, which broadens the scope of application. BRIEF DESCRIPTION OF THE DRAWINGS
[0027] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative labor.
[0028] Figure 1 It is a flow chart of realizing synchronization by using MySQL master-slave synchronization mechanism in the prior art;
[0029] Figure 2 It is a flow chart of a data synchronization method of a master-slave database provided in an embodiment of the present application;
[0030] Figure 3 It is a flow chart of a data synchronization method of a master-slave database provided in an embodiment of the present application;
[0031] Figure 4 It is a flow chart of a data synchronization method of a master-slave database provided in an embodiment of the present application;
[0032] Figure 5 It is a flow chart of a data synchronization method of a master-slave database provided in an embodiment of the present application;
[0033] Figure 6 It is a flow chart of a data synchronization method of a master-slave database provided in an embodiment of the present application;
[0034] Figure 7 It is a structural diagram of a data synchronization device for a master-slave database provided in an embodiment of the present application;
[0035] Figure 8 It is a structural diagram of a data synchronization device for a master-slave database provided in an embodiment of the present application. DETAILED DESCRIPTION
[0036] 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 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.
[0037] Figure 2 is a flow chart of a data synchronization method for a master-slave database provided in an embodiment of the present application. The data synchronization method for a master-slave database is applied to a master server, such as Figure 2 As shown, the method includes: step 210 to step 250.
[0038] Step 210: intercept a first SQL statement through an interceptor, and determine a first category of the first SQL statement, where the first category includes a query category or an update category.
[0039] Among them, the interceptor can be the Interceptor of the MyBatis framework, or it can be the interceptor in the Hibernate framework. MyBatis is an ORM (Object Relational Mapping) framework that supports customized SQL, stored procedures, and advanced mapping. MyBatis avoids almost all JDBC code and manual parameter setting and result set acquisition. MyBatis can use simple XML or annotations to configure and map primitive types, interfaces, and JavaPOJO (Plain Old Java Objects) to records in the database. MyBatis has the main features of lightweight, flexibility, dynamic SQL, prevention of SQL injection, integration with Spring, stored procedure support, and advanced mapping. It is a powerful, flexible and easy-to-use persistence layer framework that can help developers efficiently handle database operations and improve the performance and security of applications.
[0040] Before the primary server executes the first SQL statement, the first SQL statement is intercepted by an interceptor in the primary server to perform security analysis and data synchronization monitoring on the intercepted first SQL statement. The first SQL statement is analyzed to determine a first category of the first SQL statement, where the first category may be a query category or an update category, and the update category may include operations such as adding, deleting, or modifying. The first SQL statement is a SQL statement received by the primary server.
[0041] Step 220: When the first SQL statement meets the security condition, an alarm is issued for the first SQL statement.
[0042] Regardless of whether the first category of the first SQL statement is a query category or an update category, a security analysis needs to be performed on the first SQL statement.
[0043] A security analysis is performed on the first SQL statement to determine whether the first SQL statement meets the security condition. When the first SQL statement meets the security condition, it is determined that the first SQL statement has a security risk, and an alarm is issued for the first SQL statement. This process does not interrupt the execution of the first SQL statement. Through the alarm, the operation and maintenance personnel can be notified to optimize the first SQL statement.
[0044] The security condition includes at least one of the following:
[0045] The number of nested relationships is greater than the preset number of levels;
[0046] Full table scan;
[0047] Indexes are not fully utilized;
[0048] When the first category is a query category, the number of multi-table associations is greater than or equal to a preset number;
[0049] When the first category is a query category, no paging parameter is set when the amount of data queried is greater than a preset amount of data;
[0050] When the first category is the query category, the paging parameter is greater than the paging threshold when the amount of data queried is greater than the preset data amount.
[0051] When performing security analysis on the first SQL statement, it can be analyzed whether the first SQL statement is too long or contains a complex nested relationship. When the number of nested relationship layers is greater than a preset number of layers, it is determined that the first SQL statement contains a complex nested relationship.
[0052] When the first category is the query category, it is also necessary to determine whether the number of multi-table associations is greater than or equal to a preset number. When the number of multi-table associations is greater than or equal to the preset number, it is determined that the first SQL statement has a security risk.
[0053] When the first category is the query type, it is also necessary to determine whether to set a paging parameter when the amount of queried data is greater than a preset amount of data. If the paging parameter is not set, it is determined that the first SQL statement has a security risk.
[0054] When the first category is the query type, it is also necessary to determine whether the paging parameter settings are reasonable when the amount of data queried is greater than the preset amount of data and the paging parameters have been set. If the paging parameters in the first SQL statement are greater than the paging threshold, it is considered that the paging parameter settings are unreasonable, and it is determined that the first SQL statement has security risks. Among them, the paging threshold is the upper limit of the amount of data queried in a single time, which is also the amount of data displayed on one page.
[0055] In the above judgment of paging query, the keywords in the first SQL statement can be checked, such as whether there is a keyword such as select*. This keyword is for querying all data, and there may be a risk of querying a large amount of data. When the keyword is included in the first SQL statement and the amount of data in the queried data table is large (that is, the amount of data queried is greater than the preset amount of data), further check whether the paging parameter is set in the first SQL statement. This can be done by checking whether there is a keyword such as limit in the first SQL statement. This keyword is a paging keyword. The absence of this keyword indicates that the paging parameter is not set in the first SQL statement. The presence of this keyword indicates that the paging parameter is set in the first SQL statement. At this time, check whether the paging keyword is followed by a number (the number is the paging parameter) to determine whether the paging parameter is greater than the paging threshold (for example, it can be 1000). By checking the two keywords select and limit, it can be checked whether the first SQL statement meets the paging query requirements.
[0056] Among them, for data tables with a data volume greater than a preset data volume (such as a data volume exceeding 10W), the large data volume data tables in the main database can be checked by a scheduled query method every night or a pre-configured method, and then the table name is stored in the memory. When analyzing the first SQL statement, the table name in the first SQL statement is judged. If the table name in the first SQL statement is a large data volume table stored in the memory, the query must be a paging query.
[0057] The first SQL statement can be analyzed for security through the MyBatis plug-in. The MyBatis plug-in has the ability to analyze SQL query statements (which can be the first SQL statement or the second SQL statement mentioned later) in real time, and has a built-in evaluation algorithm to identify potential risks and non-optimal SQL expressions, and evaluate whether the SQL query statement meets the above security conditions, such as evaluating whether the SQL query statement has a full table scan, underutilized indexes, etc. When the MyBatis plug-in detects an SQL statement that may affect performance or data security, it will immediately trigger an alarm mechanism to notify the operation and maintenance personnel to conduct troubleshooting and optimization, thereby avoiding possible system bottlenecks or data anomalies.
[0058] Step 230: When the first category is an update category, determine a first data identifier to be operated by the first SQL statement, wherein the first data identifier includes a first data table identifier, or the first data identifier includes a first data table identifier and a first field identifier.
[0059] When the first category is the update category, the first data identifier to be operated by the first SQL statement can be further determined, and then it can be determined whether the first SQL statement needs to be synchronized to the slave database. The first data identifier may include a first data table identifier, or the first data identifier may include a first data table identifier and a first field identifier. The first field identifier is an identifier of a field in the first data table represented by the first data table identifier.
[0060] Step 240, when the first data identifier is a data identifier to be synchronized in the configuration information, the first SQL statement is written into a message queue, and the SQL statement in the message queue is used to synchronize to the slave database of the load server.
[0061] The configuration information includes pre-configured data identifiers that need to be synchronized, and the configuration information can also be updated at any time, that is, the user's update operation on the configuration information is obtained to obtain the latest configuration information, thereby realizing flexible configuration of the data identifiers that need to be synchronized. The data identifier in the configuration information can include a data table identifier, or the data identifier in the configuration information can also include a data table identifier and a field identifier, that is, the entire data table can be used as the data to be synchronized, or a certain field in a data table can be used as the data to be synchronized.
[0062] When the first data identifier is a data identifier to be synchronized in the configuration information, a synchronization operation is performed, the first SQL statement is copied, and the copied first SQL statement is written into a message queue, which can be viewed by the load server, and the load server obtains the SQL statement therein to synchronize to the slave database. The load server and the master server belong to the same server cluster.
[0063] The message queue can be a Kafka message queue, or it can also be ActiveMQ, RabbitMQ, RocketMQ, etc. Kafka is an open source stream processing platform developed by the Apache Software Foundation. It was originally developed by LinkedIn and was open sourced in early 2011. The platform is written in Scala and Java and is mainly used in the field of real-time processing of big data. Kafka is a distributed message queue based on the publish / subscribe model. It is generally used for decoupling between systems, asynchronous communication, peak load shaving, etc. It is generally used to cache data in stream computing. Kafka classifies messages according to topics when saving them. The sender of the message is called the producer, and the receiver of the message is called the consumer. In addition, the Kafka cluster consists of multiple Kafka instances, and each instance (Server) is called a Broker.
[0064] Exemplarily, the message queue is a Kafka message queue. When the first data identifier is the data identifier to be synchronized in the configuration information, the master server writes the first SQL statement into the Kafka message queue. At this time, the master server is the producer. The Broker in the Kafka cluster receives the first SQL statement sent by the master server and writes the first SQL statement into one or more pre-configured topics. The Broker retrieves the SQL statement from the one or more topics and sends it to the load server (consumer) subscribed to the topic. The load server can synchronize to the slave database based on the SQL statement.
[0065] Step 250: Execute the first SQL statement to operate on the primary database of the primary server.
[0066] Execute the first SQL statement to operate the primary database of the primary server. For example, when the first category of the first SQL statement is a query category, execute the query operation of the first SQL statement on the primary database; when the first category of the first SQL statement is an update category, execute the update (add, delete, modify) operation of the first SQL statement on the primary database, wherein, when the first data identifier is a data identifier to be synchronized in the configuration information, when the first SQL statement is written into the message queue or after the first SQL statement is written into the message queue, execute the update operation of the first SQL statement on the primary database, and when the first data identifier is not a data identifier to be synchronized in the configuration information, the update operation of the first SQL statement on the primary database can be directly executed.
[0067] In the embodiment of the present application, the master database and the slave database can be various types of relational databases, not limited to MySQL databases, but can also be Oracle databases, DB2 databases, PostgreSQL databases, etc.
[0068] In one embodiment of the present application, the execution of the first SQL statement on the main database of the main server includes: when the operation to be executed by the first SQL statement is to write the first data into the main database, if the first data includes sensitive data, encrypting the sensitive data; writing the encrypted sensitive data and other data in the first data except the sensitive data into the main database.
[0069] When the operation to be performed by the first SQL statement is to write the first data into the main database, the first data to be written into the main database is checked for sensitive data to determine whether the first data includes sensitive data. If the first data includes sensitive data, the sensitive data in the first data is encrypted, and the encrypted sensitive data and other data in the first data except the sensitive data are written into the main database. Sensitive data is pre-configured data in a designated table or designated field that needs to be encrypted, such as a telephone number, an ID card number, etc. The designated table or designated field that needs to be encrypted can be configured by the user, that is, the main server obtains the designated table identifier or designated field identifier configured by the user through the service provider interface to meet the user's needs. Exemplarily, a regular expression can be used to determine whether the first data includes sensitive data. When determining whether the first data includes sensitive data, it can be identified whether the first data includes sensitive data of the designated table identifier or the designated field identifier.
[0070] When the first category of the first SQL statement is a query type, if the first data to be queried is sensitive data, the first data is obtained from the main database and then decrypted.
[0071] The MyBatis plug-in can be used to determine whether the first data includes sensitive data, and encrypt the sensitive data when the first data includes sensitive data. The embodiment of the present application provides an encryption and decryption service based on configuration drive, which implements the automatic encryption and decryption process of data in a specified table or a specified field in a specified table within the MyBatis plug-in, so that even if the data is transmitted or stored over the network, it can be effectively protected to ensure the security of privacy information.
[0072] In one embodiment of the present application, if the first data includes sensitive data, encrypting the sensitive data includes: if the first data includes sensitive data, encrypting the sensitive data using a specified encryption algorithm, and the specified encryption algorithm includes a user-specified built-in encryption algorithm or a custom encryption algorithm.
[0073] The custom encryption algorithm is obtained through a service provider interface, that is, the main server obtains the user-defined encryption algorithm through the service provider interface.
[0074] When the first data includes sensitive data, the sensitive data is encrypted using a built-in encryption algorithm or a custom encryption algorithm specified by a user.
[0075] In order to meet the encryption needs of different users, a variety of encryption algorithms are provided for users to choose from. These encryption algorithms include built-in encryption algorithms directly integrated into the MyBatis plug-in, and also support scalability through the service provider interface (API), allowing users to customize and introduce their own encryption algorithms. This flexible configuration method not only ensures data security, but also takes into account the personalized needs of users.
[0076] The data synchronization method for master-slave databases provided in the embodiment of the present application can perform security analysis on the first SQL statement after intercepting the first SQL statement through the interceptor, and issue an alarm for the first SQL statement that meets the security conditions, so as to solve the problem that the master-slave synchronization mechanism of MySQL itself cannot perform security analysis, and can improve the performance of the database. By determining the first category of the first SQL statement, when the first category is an update category, determining the first data identifier to be operated by the first SQL statement, when the first data identifier is a data identifier to be synchronized in the configuration information, writing the first SQL statement into the message queue, executing the operation of the first SQL statement on the master database of the master server, and realizing data synchronization in the master-slave database through the message queue, there is no need to read the binlog file, which can avoid the impact on the database performance and improve the data synchronization efficiency. Moreover, the data synchronization method can be applied to various relational databases, thus widening the scope of application.
[0077] Based on the above technical solution, the method also includes: receiving a second SQL statement sent by the load server, the second category of the second SQL statement is an update category and the second data identifier to be operated by the second SQL statement is a data identifier to be synchronized in the configuration information; writing the second SQL statement into the message queue; executing the second SQL statement on the main database of the main server.
[0078] The second SQL statement is an SQL statement received by the load server. The second data identifier includes a second data table identifier, or the second data identifier includes a second data table identifier and a second field identifier.
[0079] When the load server receives the second SQL statement, it intercepts the second SQL statement through the interceptor, determines that the second category of the second SQL statement is the update category, and the second data identifier to be operated by the second SQL statement is the data identifier to be synchronized in the configuration information, and sends the second SQL statement to the main server, which performs data synchronization processing. The main server receives the second SQL statement sent by the load server, writes the second SQL statement into the message queue, and executes the update (add, delete, modify) operation of the second SQL statement on the main database. By writing the second SQL statement into the message queue, it is convenient for the load server to obtain the second SQL statement from the message queue and execute the update operation on the slave database.
[0080] When executing the second SQL statement to operate the main database, the sensitive data also needs to be encrypted and written into the main database. When the operation to be executed by the second SQL statement is to write the second data into the main database, if the second data includes sensitive data, the sensitive data in the second data is encrypted, and the encrypted sensitive data in the second data and other data in the second data except the sensitive data are written into the main database to improve the security of the data.
[0081] When the second SQL statement is of the update type and the second data identifier to be operated is the data identifier that needs to be synchronized in the configuration information, the main server will perform data synchronization processing. If the second SQL statement is of the query type or the second data identifier is a data identifier that does not need to be synchronized, the load server can directly perform database operations, thereby realizing the separation of reading and writing of business data, and the data synchronization operation is uniformly performed by the main server.
[0082] Figure 3 1 is a flowchart of a data synchronization method for a master-slave database provided in an embodiment of the present application. The data synchronization method for a master-slave database is applied to a master server. This embodiment uses the MyBatis plug-in to implement data synchronization operations as an example for explanation. Figure 3 As shown, the method includes: step 310 to step 350.
[0083] Step 310: intercept a first SQL statement through an interceptor, and determine a first category of the first SQL statement through a MyBatis plug-in, where the first category includes a query category or an update category.
[0084] The execution of the first SQL statement by the main server is intercepted by the interceptor in the MyBatis framework, the intercepted first SQL statement is passed to the MyBatis plug-in, the first category of the first SQL statement is determined by the MyBatis plug-in, and subsequent security analysis and synchronization operations are performed.
[0085] Step 320: When the MyBatis plug-in determines that the first SQL statement meets the security condition, an alarm is issued for the first SQL statement.
[0086] Step 330, when the first category is an update category, determine the first data identifier to be operated by the first SQL statement through the MyBatis plug-in, wherein the first data identifier includes a first data table identifier, or the first data identifier includes a first data table identifier and a first field identifier.
[0087] Step 340, when the first data identifier is a data identifier to be synchronized in the configuration information, the first SQL statement is written into the message queue through the MyBatis plug-in, and the SQL statement in the message queue is used to synchronize to the slave database of the load server.
[0088] Among them, the message queue is the Kafka message queue.
[0089] The embodiment of the present application abandons the traditional MySQL master-slave synchronization mechanism and adopts Kafka's publish / subscribe model, which not only improves the efficiency and flexibility of data synchronization, but also greatly enhances the scalability and fault tolerance of the system. Through Kafka's message queue mechanism, the data changes of the master database can be published to the Kafka message queue in real time, and the load server consumes these change data by subscribing to the corresponding topic, thereby realizing real-time data synchronization.
[0090] Step 350: Execute the operation of the first SQL statement on the primary database of the primary server through the MyBatis plug-in.
[0091] Optionally, when it is determined through the MyBatis plug-in that the operation to be performed by the first SQL statement is to write the first data into the main database, if the first data includes sensitive data, the sensitive data is encrypted; and the encrypted sensitive data and other data in the first data except the sensitive data are written into the main database through the MyBatis plug-in.
[0092] In the embodiment of the present application, the MyBatis plug-in successfully implements the inheritance and extension of the Interceptor interface of MyBatis, and specifically rewrites the key method - intercept. All monitoring and control logic (security analysis, data synchronization, sensitive data encryption, etc.) about SQL statements are effectively implemented in this method. The intercept method is called before each SQL statement is executed as an interception point. At this time, the MyBatis plug-in will check the SQL content, determine whether synchronization needs to be performed, analyze the rationality of SQL, and decide whether to perform encryption and decryption operations.
[0093] The embodiment of the present application uses the powerful expansion capability of the MyBatis plug-in to build an intelligent database operation management system that integrates SQL statement screening and interception, rationality analysis alarm, and data encryption and decryption, which greatly enhances the controllability and security of database operation behaviors in complex environments, and provides strong technical support for ensuring the data quality and system stability of enterprise-level applications. By cleverly integrating into the MyBatis framework, this solution is easy to integrate and deploy, and can maintain good compatibility with the native framework to the greatest extent.
[0094] The embodiment of the present application presents the monitoring service of data master-slave synchronization in a highly flexible manner, in the form of a Mybatis plug-in, providing users with convenient plug-in and unplug functions to ensure the flexibility and ease of use of the monitoring service; the monitoring service has a powerful asynchronous message monitoring capability, can go deep into message queues such as Kafka, track the flow of asynchronous data in real time, and provide users with comprehensive data monitoring and analysis; the monitoring service has a wide range of applicability, is compatible with all systems that use Mybatis as an ORM framework mapping, meets the monitoring needs of users in different scenarios, and provides users with a comprehensive and reliable monitoring solution; more fine-grained control, the Mybatis plug-in can filter and filter the synchronization data table by configuration, and can encrypt the specified table or the fields in the table; higher configurability, the Mybatis plug-in supports the configuration of filtering and filtering data tables, the configuration of encryption algorithms, the configuration of encryption tables or specific fields in encryption tables; a wider monitoring range, the Mybatis plug-in can monitor the master-slave synchronization of the Mysql database in addition to monitoring the master-slave synchronization of other relational databases; stronger security, the Mybatis plug-in provides data encryption and decryption services, which can encrypt and decrypt the specified table or the fields in the specified table.
[0095] Figure 4 is a flow chart of a data synchronization method for a master-slave database provided in an embodiment of the present application. The data synchronization method for a master-slave database is applied to a load server, such as Figure 4 As shown, the method includes: step 410 to step 420.
[0096] Step 410, obtain the first SQL statement from the message queue, the message queue is used to store SQL statements that the main server needs to synchronize to the load server, the first category of the first SQL statement is an update category and the first data identifier to be operated by the first SQL statement is the data identifier that needs to be synchronized in the configuration information.
[0097] The first data identifier includes a first data table identifier, or the first data identifier includes a first data table identifier and a first field identifier.
[0098] The configuration information includes pre-configured data identifiers that need to be synchronized, and the configuration information can also be updated at any time, that is, the user's update operation on the configuration information is obtained to obtain the latest configuration information, thereby realizing flexible configuration of the data identifiers that need to be synchronized. The data identifier in the configuration information can include a data table identifier, or the data identifier in the configuration information can also include a data table identifier and a field identifier at the same time, that is, the entire data table can be used as the data to be synchronized, or a certain field in a data table can be used as the data to be synchronized.
[0099] The load server monitors the message queue and obtains the first SQL statement that has changed therein to synchronize it to the slave database. The load server and the master server belong to the same server cluster.
[0100] The message queue can be a Kafka message queue, or it can also be ActiveMQ, RabbitMQ, RocketMQ, etc. Kafka is an open source stream processing platform developed by the Apache Software Foundation. It was originally developed by LinkedIn and was open sourced in early 2011. The platform is written in Scala and Java and is mainly used in the field of real-time processing of big data. Kafka is a distributed message queue based on the publish / subscribe model. It is generally used for decoupling between systems, asynchronous communication, peak load shaving, etc. It is generally used to cache data in stream computing. Kafka classifies messages according to topics when saving them. The sender of the message is called the producer, and the receiver of the message is called the consumer. In addition, the Kafka cluster consists of multiple Kafka instances, and each instance (Server) is called a Broker.
[0101] Exemplarily, the message queue is a Kafka message queue. When the first data identifier is the data identifier to be synchronized in the configuration information, the master server writes the first SQL statement into the Kafka message queue. At this time, the master server is the producer. The Broker in the Kafka cluster receives the first SQL statement sent by the master server and writes the first SQL statement into one or more pre-configured topics. The Broker retrieves the SQL statement from the one or more topics and sends it to the load server (consumer) subscribed to the topic. The load server can synchronize to the slave database based on the SQL statement.
[0102] Step 420: execute the first SQL statement to operate on the slave database of the load server.
[0103] The load server executes the first SQL statement to update (add, delete, modify) the slave database of the load server.
[0104] In the embodiment of the present application, the master database and the slave database can be various types of relational databases, not limited to MySQL databases, but can also be Oracle databases, DB2 databases, PostgreSQL databases, etc.
[0105] In one embodiment of the present application, the execution of the first SQL statement on the slave database of the load server includes: when the operation to be executed by the first SQL statement is to write the first data into the slave database, if the first data includes sensitive data, encrypting the sensitive data; writing the encrypted sensitive data and other data in the first data except the sensitive data into the slave database.
[0106] When the operation to be performed by the first SQL statement is to write the first data into the slave database, the first data to be written into the slave database is checked for sensitive data to determine whether the first data includes sensitive data. If the first data includes sensitive data, the sensitive data in the first data is encrypted, and the encrypted sensitive data and other data in the first data except the sensitive data are written into the slave database. Sensitive data is pre-configured data of a designated table or designated field that needs to be encrypted, such as a telephone number, an ID card number, etc. The designated table or designated field that needs to be encrypted can be configured by the user, that is, the main server or the load server obtains the designated table identifier or designated field identifier configured by the user through the service provider interface to meet the needs of the user. Exemplarily, a regular expression can be used to determine whether the first data includes sensitive data. When determining whether the first data includes sensitive data, it can be identified whether the first data includes sensitive data of the designated table identifier or the designated field identifier.
[0107] When querying data from a secondary database based on an SQL statement, if the data to be queried is sensitive data, the data is decrypted after being obtained from the secondary database.
[0108] The MyBatis plug-in can be used to determine whether the first data includes sensitive data, and encrypt the sensitive data when the first data includes sensitive data. The embodiment of the present application provides an encryption and decryption service based on configuration drive, which implements the automatic encryption and decryption process of data in a specified table or a specified field in a specified table within the MyBatis plug-in, so that even if the data is transmitted or stored over the network, it can be effectively protected to ensure the security of privacy information.
[0109] In one embodiment of the present application, if the first data includes sensitive data, encrypting the sensitive data includes: if the first data includes sensitive data, encrypting the sensitive data using a specified encryption algorithm, and the specified encryption algorithm includes a user-specified built-in encryption algorithm or a custom encryption algorithm.
[0110] Among them, the custom encryption algorithm is obtained through the service provider interface, that is, the main server or the load server obtains the user-defined encryption algorithm through the service provider interface and synchronizes it to other servers in the same cluster. For example, after the main server obtains the user-defined encryption algorithm through the service provider interface, it can be synchronized to each load server in the cluster.
[0111] When the first data includes sensitive data, the sensitive data is encrypted using a built-in encryption algorithm or a custom encryption algorithm specified by a user.
[0112] In order to meet the encryption needs of different users, a variety of encryption algorithms are provided for users to choose from. These encryption algorithms include built-in encryption algorithms directly integrated into the MyBatis plug-in, and also support scalability through the service provider interface (API), allowing users to customize and introduce their own encryption algorithms. This flexible configuration method not only ensures data security, but also takes into account the personalized needs of users.
[0113] In the data synchronization method of the master-slave database provided in the embodiment of the present application, the load server obtains the first SQL statement from the message queue, executes the first SQL statement to operate the slave database of the load server, and the message queue is used to save the SQL statements that the master server needs to synchronize to the load server. The message queue is used to realize the synchronization of data in the master-slave database, and there is no need to read the binlog file, which can avoid the impact on the database performance and improve the data synchronization efficiency. Moreover, the data synchronization method can be applied to various relational databases, which broadens the scope of application.
[0114] Figure 5 is a flow chart of a data synchronization method for a master-slave database provided in an embodiment of the present application. The data synchronization method for a master-slave database is applied to a load server, such as Figure 5 As shown, the method includes: steps 510 to 560.
[0115] Step 510: intercept the second SQL statement through an interceptor, and determine a second category of the second SQL statement, where the second category includes a query category or an update category.
[0116] The interceptor may be an interceptor of the MyBatis framework, or an interceptor in the Hibernate framework.
[0117] Before the load server executes the second SQL statement, the second SQL statement is intercepted by an interceptor in the load server to perform security analysis and data synchronization monitoring on the intercepted second SQL statement. The second SQL statement is analyzed to determine a second category of the second SQL statement, the second category may be a query category or an update category, and the update category may include operations such as adding, deleting, or modifying. The second SQL statement is the SQL statement received by the load server.
[0118] Step 520: When the second SQL statement meets the security condition, an alarm is issued for the second SQL statement.
[0119] Regardless of whether the second category of the second SQL statement is a query category or an update category, a security analysis needs to be performed on the second SQL statement.
[0120] A security analysis is performed on the second SQL statement to determine whether the second SQL statement meets the security conditions. When the second SQL statement meets the security conditions, it is determined that the second SQL statement has a security risk. At this time, an alarm is issued for the second SQL statement. This process does not interrupt the execution of the second SQL statement.
[0121] The security condition includes at least one of the following:
[0122] The number of nested relationships is greater than the preset number of levels;
[0123] Full table scan;
[0124] Indexes are not fully utilized;
[0125] When the second category is a query category, the number of multi-table associations is greater than or equal to a preset number;
[0126] When the second category is a query category, no paging parameter is set when the amount of data queried is greater than the preset amount of data;
[0127] When the second category is the query category, the paging parameter is greater than the paging threshold when the amount of data queried is greater than the preset data amount.
[0128] When performing security analysis on the second SQL statement, it can be analyzed whether the second SQL statement is too long or contains a complex nested relationship. When the number of nested relationship layers is greater than a preset number of layers, it is determined that the second SQL statement contains a complex nested relationship.
[0129] When the second category is the query category, it is also necessary to determine whether the number of multi-table associations is greater than or equal to a preset number. When the number of multi-table associations is greater than or equal to the preset number, it is determined that the second SQL statement has a security risk.
[0130] When the second category is the query type, it is also necessary to determine whether to set a paging parameter when the amount of data queried is greater than the preset data amount. If the paging parameter is not set, it is determined that the second SQL statement has a security risk.
[0131] When the second category is the query type, it is also necessary to determine whether the paging parameter settings are reasonable when the amount of data queried is greater than the preset amount of data and the paging parameters have been set. If the paging parameters in the second SQL statement are greater than the paging threshold, it is considered that the paging parameter settings are unreasonable, and it is determined that the second SQL statement has security risks. Among them, the paging threshold is the upper limit of the amount of data queried in a single time, which is also the amount of data displayed on one page.
[0132] In the above judgment of paging query, the keywords in the second SQL statement can be checked, such as whether there is a keyword such as select*. This keyword is for querying all data, and there may be a risk of querying a large amount of data. When the keyword is included in the second SQL statement and the amount of data in the queried data table is large (that is, the amount of data queried is greater than the preset amount of data), further check whether the paging parameter is set in the second SQL statement. This can be done by checking whether there is a keyword such as limit in the second SQL statement. This keyword is a paging keyword. The absence of this keyword indicates that the paging parameter is not set in the second SQL statement. The presence of this keyword indicates that the paging parameter is set in the second SQL statement. At this time, check whether the paging keyword is followed by a number (the number is the paging parameter) to determine whether the paging parameter is greater than the paging threshold (for example, it can be 1000). By checking the two keywords select and limit, it can be checked whether the second SQL statement meets the paging query requirements.
[0133] Among them, for data tables with a data volume greater than a preset data volume (such as a data volume exceeding 10W), the method of scheduled query every night or a pre-configured method can be used to check the data table with large data volume from the database, and then store the table name in the memory. When analyzing the second SQL statement, the table name in the second SQL statement is judged. If the table name in the second SQL statement is a large data volume table stored in the memory, the query must be a paging query.
[0134] The second SQL statement can be analyzed for security through the MyBatis plug-in. The MyBatis plug-in has the ability to analyze SQL query statements (which can be the second SQL statement or the first SQL statement mentioned in the above embodiment) in real time, and has a built-in set of evaluation algorithms to identify potential risks and non-optimal SQL expressions, and evaluate whether the SQL query statement meets the above security conditions, such as evaluating whether the SQL query statement has a full table scan, underutilized indexes, etc. When the MyBatis plug-in detects an SQL statement that may affect performance or data security, it will immediately trigger an alarm mechanism to notify the operation and maintenance personnel to conduct troubleshooting and optimization, thereby avoiding possible system bottlenecks or data anomalies.
[0135] Step 530: When the second category is an update category, determine a second data identifier to be operated by the second SQL statement, wherein the second data identifier includes a second data table identifier, or the second data identifier includes a second data table identifier and a second field identifier.
[0136] When the second category is the update category, the second data identifier to be operated by the second SQL statement can be further determined, and then it can be determined whether the second SQL statement needs to be synchronized to the slave database. The second data identifier may include a second data table identifier, or the second data identifier may include a second data table identifier and a second field identifier. The second field identifier is an identifier of a field in the second data table represented by the second data table identifier.
[0137] Step 540: When the second data identifier is a data identifier to be synchronized in the configuration information, the second SQL statement is sent to the primary server.
[0138] When the second data identifier is a data identifier to be synchronized in the configuration information, the main server performs a synchronization operation, the load server sends the second SQL statement to the main server, the main server copies the first SQL statement, and writes the copied second SQL statement into the message queue, and at the same time, the main server executes the second SQL statement on the main database. The load server and the main server belong to the same server cluster.
[0139] The message queue can be a Kafka message queue, or it can also be ActiveMQ, RabbitMQ, RocketMQ, etc. Kafka is an open source stream processing platform developed by the Apache Software Foundation. It was originally developed by LinkedIn and was open sourced in early 2011. The platform is written in Scala and Java and is mainly used in the field of real-time processing of big data. Kafka is a distributed message queue based on the publish / subscribe model. It is generally used for decoupling between systems, asynchronous communication, peak load shaving, etc. It is generally used to cache data in stream computing. Kafka classifies messages according to topics when saving them. The sender of the message is called the producer, and the receiver of the message is called the consumer. In addition, the Kafka cluster consists of multiple Kafka instances, and each instance (Server) is called a Broker.
[0140] Exemplarily, the message queue is a Kafka message queue, and the master server writes the second SQL statement sent by the slave server into the Kafka message queue. At this time, the master server is the producer. The Broker in the Kafka cluster receives the second SQL statement sent by the master server and writes the second SQL statement into one or more pre-configured topics. The Broker retrieves the SQL statement from one or more topics and sends it to the load server (consumer) subscribed to the topic. The load server can synchronize to the slave database based on the SQL statement.
[0141] Step 550: Obtain the second SQL statement from the message queue.
[0142] The load server monitors the message queue, and when a second SQL statement that needs to be synchronized is found, the load server obtains the second SQL statement from the message queue.
[0143] Exemplarily, the message queue is a Kafka message queue, and the load server subscribes to a target topic in the Kafka message queue. When there is a data change in the Kafka message queue of the target topic, the load server will receive a second SQL statement indicating the data change.
[0144] Step 560, execute the second SQL statement to operate on the slave database of the load server.
[0145] Execute the second SQL statement to operate the slave database of the load server. For example, when the second category of the second SQL statement is a query type, execute the second SQL statement to query the master database; when the second category of the second SQL statement is an update type, after obtaining the second SQL statement from the message queue, execute the second SQL statement to update (add, delete, modify) the slave database.
[0146] In one embodiment of the present application, the execution of the second SQL statement on the slave database of the load server includes: when the operation to be executed by the second SQL statement is to write the second data into the master database, if the second data includes sensitive data, encrypting the sensitive data in the second data; writing the encrypted sensitive data and other data in the second data except the sensitive data into the slave database.
[0147] When the operation to be performed by the second SQL statement is to write the second data into the slave database, the second data to be written into the slave database is checked for sensitive data to determine whether the second data includes sensitive data, and if the second data includes sensitive data, the sensitive data in the second data is encrypted, and the encrypted sensitive data and other data in the second data except the sensitive data are written into the slave database. When determining whether the first data includes sensitive data, it can be identified whether the first data includes sensitive data of a specified table identifier or a specified field identifier.
[0148] When the second category of the second SQL statement is a query type, if the second data to be queried is sensitive data, the second data is obtained from the main database and then decrypted.
[0149] The MyBatis plug-in can be used to determine whether the second data includes sensitive data, and encrypt the sensitive data when the second data includes sensitive data. The embodiment of the present application provides an encryption and decryption service based on configuration drive, which implements the automatic encryption and decryption process of data in a specified table or a specified field in a specified table within the MyBatis plug-in, so that even if the data is transmitted or stored over the network, it can be effectively protected to ensure the security of privacy information.
[0150] In one embodiment of the present application, if the second data includes sensitive data, the sensitive data in the second data is encrypted, including: if the second data includes sensitive data, the sensitive data in the second data is encrypting using a specified encryption algorithm, and the specified encryption algorithm includes a user-specified built-in encryption algorithm or a custom encryption algorithm.
[0151] Wherein, the custom encryption algorithm is obtained through a service providing interface.
[0152] When the second data includes sensitive data, the sensitive data in the second data is encrypted using a built-in encryption algorithm or a custom encryption algorithm specified by a user.
[0153] In order to meet the encryption needs of different users, a variety of encryption algorithms are provided for users to choose from. These encryption algorithms include built-in encryption algorithms directly integrated into the MyBatis plug-in, and also support scalability through the service provider interface (API), allowing users to customize and introduce their own encryption algorithms. This flexible configuration method not only ensures data security, but also takes into account the personalized needs of users.
[0154] Figure 6 is a flow chart of a data synchronization method for a master-slave database provided in an embodiment of the present application, such as Figure 6 As shown, in the main server and the load server, SQL monitoring plug-in Interceptor is provided, and the SQL monitoring plug-in Interceptor is the MyBatis plug-in mentioned in the above embodiment, and the plug-in monitors SQL statements in the master-slave synchronization process to screen and intercept, security analysis, and dynamic encryption and decryption. For screening and interception, in a distributed database environment, especially in a master-slave synchronization architecture, not all data changes need to be synchronized to the slave library, and the plug-in in the embodiment of the present application can identify and accurately intercept those SQL commands (i.e., update classes) for specific tables that should not be executed in the slave library based on the rule information pre-configured by the user, and this intelligent filtering strategy allows developers to customize flexibly, ensuring data consistency while optimizing synchronization efficiency. For security analysis, the plug-in has the ability to analyze SQL statements in real time, and a set of evaluation algorithms are built in to identify potential risks and non-optimal SQL expressions, such as full table scans, underutilized indexes, etc. When the plug-in detects SQL that may affect performance or data security, it will immediately trigger an alarm mechanism to notify the operation and maintenance personnel to conduct investigation and optimization, thereby avoiding possible system bottlenecks or data anomalies. For dynamic encryption and decryption, in order to meet the security storage requirements of sensitive data, the plug-in implements the automatic encryption and decryption process of data in the specified table or specific fields in the table, so that even if the data is transmitted or stored over the network, it can be effectively protected to ensure the security of privacy information.
[0155] like Figure 6As shown, the monitoring and management of database operations in the embodiment of the present application is based on the plug-in mechanism of the MyBatis persistence layer framework. Through the deep intervention of the MyBatis SQL execution process, the monitoring of the addition, deletion, modification and query operations of the database SQL under the main load environment is realized, the original execution SQL statement is obtained, and then the SQL content is analyzed and checked. For the SQL that is risk-free and needs to be synchronized to the load server, it is written into Kafka, and then consumed by the load server. The load server also includes the SQL monitoring plug-in Interceptor, which will also check the SQL statement. If the SQL statement belongs to the one that needs to be synchronized, it connects to the main server database for SQL synchronization execution to realize the read-write separation of the business (update class SQL is executed in the main database, and query class SQL is executed from the database of the load server itself that receives the SQL statement). Through the deep integration of MyBatis, a customized Interceptor named "Master-Slave Synchronization SQL Monitoring Plug-in" is realized, which can realize comprehensive monitoring and refined control management of business query behavior.
[0156] The core technology of this SQL monitoring plug-in is to connect and rewrite the key interface in MyBatis - Interceptor, especially its intercept method. In practical applications, especially in the MySQL master-slave synchronization architecture implemented by Kafka, the final link of data flow between the master and slave databases will be captured and executed by Kafka consumers.
[0157] The embodiment of the present application realizes master-slave synchronization through Kafka, does not need to read binlog files, will not affect the performance of the database, can improve data synchronization efficiency, and can be applicable to various types of relational databases, broadening the scope of application, and through dynamic encryption and decryption of sensitive data, ensures data security. Through security analysis, operation and maintenance personnel can be notified in time to optimize risky SQL statements to improve database query performance.
[0158] Figure 7 is a schematic diagram of the structure of a data synchronization device for a master-slave database provided in an embodiment of the present application. The device is applied to a master server, such as Figure 7 As shown, the device comprises:
[0159] A first category determination module 710, configured to intercept a first SQL statement through an interceptor and determine a first category of the first SQL statement, where the first category includes a query category or an update category;
[0160] An alarm module 720 is used to issue an alarm for the first SQL statement when the first SQL statement meets the security condition;
[0161] A first identifier determination module 730 is used to determine a first data identifier to be operated by the first SQL statement when the first category is an update category, wherein the first data identifier includes a first data table identifier, or the first data identifier includes a first data table identifier and a first field identifier;
[0162] A first SQL synchronization module 740 is used to write the first SQL statement into a message queue when the first data identifier is a data identifier to be synchronized in the configuration information, and the SQL statement in the message queue is used to synchronize to the slave database of the load server;
[0163] The first SQL execution module 750 is used to execute the operation of the first SQL statement on the primary database of the primary server.
[0164] Optionally, the first SQL execution module includes:
[0165] an encryption unit, configured to encrypt the sensitive data when the operation to be executed by the first SQL statement is to write the first data into the primary database if the first data includes sensitive data;
[0166] A data writing unit is used to write the encrypted sensitive data and other data in the first data except the sensitive data into the main database.
[0167] Optionally, the encryption unit is specifically used for:
[0168] If the first data includes sensitive data, the sensitive data is encrypted using a specified encryption algorithm, where the specified encryption algorithm includes a built-in encryption algorithm specified by a user or a custom encryption algorithm.
[0169] Optionally, the custom encryption algorithm is obtained through a service provider interface.
[0170] Optionally, the security condition includes at least one of the following:
[0171] The number of nested relationships is greater than the preset number of levels;
[0172] Full table scan;
[0173] Indexes are not fully utilized;
[0174] When the first category is a query category, the number of multi-table associations is greater than or equal to a preset number;
[0175] When the first category is a query category, no paging parameter is set when the amount of data queried is greater than a preset amount of data;
[0176] When the first category is the query category, the paging parameter is greater than the paging threshold when the amount of data queried is greater than the preset data amount.
[0177] Optionally, the method further includes:
[0178] A second SQL receiving module is used to receive a second SQL statement sent by the load server, wherein the second category of the second SQL statement is an update category and the second data identifier to be operated by the second SQL statement is a data identifier to be synchronized in the configuration information;
[0179] A second SQL synchronization module, used for writing the second SQL statement into the message queue;
[0180] The second SQL execution module is used to execute the second SQL statement to operate the primary database of the primary server.
[0181] The data synchronization device for a master-slave database provided in the embodiment of the present application is used to implement the various steps of the data synchronization method for a master-slave database described in the embodiment of the present application. The specific implementation methods of each module of the device refer to the corresponding steps and will not be repeated here.
[0182] The data synchronization device for master-slave databases provided in the embodiment of the present application can perform security analysis on the first SQL statement after intercepting the first SQL statement through the interceptor, and issue an alarm for the first SQL statement that meets the security conditions, so as to solve the problem that the master-slave synchronization mechanism of MySQL itself cannot perform security analysis, and can improve the performance of the database. By determining the first category of the first SQL statement, when the first category is an update category, determining the first data identifier to be operated by the first SQL statement, when the first data identifier is a data identifier to be synchronized in the configuration information, writing the first SQL statement into the message queue, executing the operation of the first SQL statement on the master database of the master server, and realizing data synchronization in the master-slave database through the message queue, there is no need to read the binlog file, which can avoid the impact on the database performance and improve the data synchronization efficiency. Moreover, the data synchronization method can be applied to various relational databases, thus widening the scope of application.
[0183] Figure 8 is a schematic diagram of the structure of a data synchronization device for a master-slave database provided in an embodiment of the present application, the device is applied to a load server, such as Figure 8 As shown, the device comprises:
[0184] A first SQL acquisition module 810 is used to acquire a first SQL statement from a message queue, wherein the message queue is used to store SQL statements that the master server needs to synchronize to the load server, wherein the first category of the first SQL statement is an update category and the first data identifier to be operated by the first SQL statement is a data identifier to be synchronized in the configuration information; wherein the first data identifier includes a first data table identifier, or the first data identifier includes a first data table identifier and a first field identifier;
[0185] The first SQL execution module 820 is used to execute the operation of the first SQL statement on the slave database of the load server.
[0186] Optionally, the device further comprises:
[0187] A second category determination module, configured to intercept a second SQL statement through an interceptor and determine a second category of the second SQL statement, where the second category includes a query category or an update category;
[0188] An alarm module, configured to generate an alarm for the second SQL statement when the second SQL statement meets a security condition;
[0189] A second identifier determination module, used for determining a second data identifier to be operated by the second SQL statement when the second category is an update category, wherein the second data identifier includes a second data table identifier, or the second data identifier includes a second data table identifier and a second field identifier;
[0190] A second SQL sending module, configured to send the second SQL statement to the primary server when the second data identifier is a data identifier to be synchronized in the configuration information;
[0191] A second SQL acquisition module, used to acquire the second SQL statement from the message queue;
[0192] The second SQL execution module is used to execute the second SQL statement to operate the slave database of the load server.
[0193] Optionally, the security condition includes at least one of the following:
[0194] The number of nested relationships is greater than the preset number of levels;
[0195] Full table scan;
[0196] Indexes are not fully utilized;
[0197] When the second category is a query category, the number of multi-table associations is greater than or equal to a preset number;
[0198] When the second category is a query category, no paging parameter is set when the amount of data queried is greater than the preset amount of data;
[0199] When the second category is the query category, the paging parameter is greater than the paging threshold when the amount of data queried is greater than the preset data amount.
[0200] Optionally, the first SQL execution module includes:
[0201] an encryption unit, configured to encrypt the sensitive data when the operation to be executed by the first SQL statement is to write the first data into the slave database if the first data includes sensitive data;
[0202] A data writing unit is used to write the encrypted sensitive data and other data in the first data except the sensitive data into the slave database.
[0203] Optionally, the encryption unit is specifically used for:
[0204] If the first data includes sensitive data, the sensitive data is encrypted using a specified encryption algorithm, where the specified encryption algorithm includes a built-in encryption algorithm specified by a user or a custom encryption algorithm.
[0205] The custom encryption algorithm is obtained through the service provider interface.
[0206] The data synchronization device for a master-slave database provided in the embodiment of the present application is used to implement the various steps of the data synchronization method for a master-slave database described in the embodiment of the present application. The specific implementation methods of each module of the device refer to the corresponding steps and will not be repeated here.
[0207] In the data synchronization device of the master-slave database provided by the embodiment of the present application, the load server obtains the first SQL statement from the message queue, executes the first SQL statement to operate the slave database of the load server, and the message queue is used to save the SQL statements that the master server needs to synchronize to the load server. The message queue is used to realize the synchronization of data in the master-slave database, and there is no need to read the binlog file, which can avoid the impact on the database performance and improve the data synchronization efficiency. Moreover, the data synchronization method can be applied to various relational databases, which broadens the scope of application.
[0208] Accordingly, an embodiment of the present application further provides an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the computer program, the master-slave database data synchronization method described in the embodiment of the present application is implemented. The electronic device may be a server, etc.
[0209] The embodiment of the present application also provides a computer-readable storage medium on which a computer program is stored. When the program is executed by a processor, the steps of the master-slave database data synchronization method as described in the embodiment of the present application are implemented.
[0210] Each embodiment in this specification is described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same or similar parts between the embodiments can be referred to each other. For the device embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment.
[0211] The above is a detailed introduction to a data synchronization method, device, electronic device and storage medium for a master-slave database provided in an embodiment of the present application. Specific examples are used in this article to illustrate the principles and implementation methods of the present application. The description of the above embodiments is only used to help understand the method of the present application and its core idea; at the same time, for general technical personnel in this field, according to the idea of the present application, there will be changes in the specific implementation method and application scope. In summary, the content of this specification should not be understood as a limitation on the present application.
[0212] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a necessary general hardware platform, and of course can also be implemented by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the prior art can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiments.
Claims
1. A data synchronization method for a master-slave database, characterized in that: Applied to the master server, including: intercepting a first SQL statement by an interceptor, and determining a first category of the first SQL statement, where the first category includes a query category or an update category; When the first SQL statement meets the security condition, an alarm is issued for the first SQL statement; When the first category is an update category, determining a first data identifier to be operated by the first SQL statement, wherein the first data identifier includes a first data table identifier, or the first data identifier includes a first data table identifier and a first field identifier; When the first data identifier is a data identifier to be synchronized in the configuration information, the first SQL statement is written into a message queue, and the SQL statement in the message queue is used to synchronize to a slave database of the load server; Execute the first SQL statement to operate on the primary database of the primary server.
2. The method according to claim 1, characterized in that The executing of the first SQL statement to operate on the primary database of the primary server includes: When the operation to be executed by the first SQL statement is to write the first data into the primary database, if the first data includes sensitive data, encrypting the sensitive data; The encrypted sensitive data and other data in the first data except the sensitive data are written into the main database.
3. The method according to claim 2, characterized in that If the first data includes sensitive data, encrypting the sensitive data includes: If the first data includes sensitive data, the sensitive data is encrypted using a specified encryption algorithm, where the specified encryption algorithm includes a built-in encryption algorithm specified by a user or a custom encryption algorithm.
4. The method according to claim 3, characterized in that The custom encryption algorithm is obtained through the service provider interface.
5. The method according to any one of claims 1 to 4, characterized in that: The security condition includes at least one of the following: The number of nested relationships is greater than the preset number of levels; Full table scan; Indexes are not fully utilized; When the first category is a query category, the number of multi-table associations is greater than or equal to a preset number; When the first category is a query category, no paging parameter is set when the amount of data queried is greater than a preset amount of data; When the first category is the query category, the paging parameter is greater than the paging threshold when the amount of data queried is greater than the preset data amount.
6. The method according to any one of claims 1 to 4, characterized in that: Also includes: receiving a second SQL statement sent by the load server, wherein the second category of the second SQL statement is an update category and the second data identifier to be operated by the second SQL statement is a data identifier to be synchronized in the configuration information; Writing the second SQL statement into the message queue; Execute the second SQL statement to operate on the primary database of the primary server.
7. A data synchronization method for a master-slave database, characterized in that: Applicable to load servers, including: Obtain a first SQL statement from a message queue, the message queue being used to store SQL statements that the master server needs to synchronize to the load server, wherein the first category of the first SQL statement is an update category and the first data identifier to be operated by the first SQL statement is a data identifier to be synchronized in the configuration information; wherein the first data identifier includes a first data table identifier, or the first data identifier includes a first data table identifier and a first field identifier; Execute the first SQL statement to operate on the slave database of the load server.
8. The method according to claim 7, characterized in that Also includes: intercepting a second SQL statement by an interceptor, and determining a second category of the second SQL statement, where the second category includes a query category or an update category; When the second SQL statement meets the security condition, an alarm is issued for the second SQL statement; When the second category is an update category, determining a second data identifier to be operated by the second SQL statement, wherein the second data identifier includes a second data table identifier, or the second data identifier includes a second data table identifier and a second field identifier; When the second data identifier is a data identifier to be synchronized in the configuration information, sending the second SQL statement to the primary server; Obtaining the second SQL statement from the message queue; Execute the second SQL statement to operate on the slave database of the load server.
9. The method according to claim 8, characterized in that The security condition includes at least one of the following: The number of nested relationships is greater than the preset number of levels; Full table scan; Indexes are not fully utilized; When the second category is a query category, the number of multi-table associations is greater than or equal to a preset number; When the second category is a query category, no paging parameter is set when the amount of data queried is greater than the preset amount of data; When the second category is the query category, the paging parameter is greater than the paging threshold when the amount of data queried is greater than the preset data amount.
10. The method according to any one of claims 7 to 9, characterized in that: The executing of the first SQL statement to operate on the slave database of the load server includes: When the operation to be executed by the first SQL statement is to write the first data into the slave database, if the first data includes sensitive data, encrypt the sensitive data; The encrypted sensitive data and other data in the first data except the sensitive data are written into the slave database.
11. The method according to claim 10, characterized in that If the first data includes sensitive data, encrypting the sensitive data includes: If the first data includes sensitive data, the sensitive data is encrypted using a specified encryption algorithm, where the specified encryption algorithm includes a built-in encryption algorithm specified by a user or a custom encryption algorithm.
12. The method according to claim 11, characterized in that The custom encryption algorithm is obtained through the service provider interface.
13. A data synchronization device for a master-slave database, characterized in that: Applied to the master server, including: A first category determination module, configured to intercept a first SQL statement through an interceptor and determine a first category of the first SQL statement, wherein the first category includes a query category or an update category; An alarm module, configured to generate an alarm for the first SQL statement when the first SQL statement meets a security condition; A first identifier determination module, used for determining a first data identifier to be operated by the first SQL statement when the first category is an update category, wherein the first data identifier includes a first data table identifier, or the first data identifier includes a first data table identifier and a first field identifier; A first SQL synchronization module, used for writing the first SQL statement into a message queue when the first data identifier is a data identifier to be synchronized in the configuration information, and the SQL statement in the message queue is used for synchronization to a slave database of the load server; The first SQL execution module is used to execute the operation of the first SQL statement on the primary database of the primary server.
14. A data synchronization device for a master-slave database, characterized in that: Applicable to load servers, including: A first SQL acquisition module is used to acquire a first SQL statement from a message queue, wherein the message queue is used to store SQL statements that the master server needs to synchronize to the load server, wherein the first category of the first SQL statement is an update category and the first data identifier to be operated by the first SQL statement is a data identifier to be synchronized in the configuration information; wherein the first data identifier includes a first data table identifier, or the first data identifier includes a first data table identifier and a first field identifier; The first SQL execution module is used to execute the operation of the first SQL statement on the slave database of the load server.
15. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the computer program, the master-slave database data synchronization method described in any one of claims 1 to 6 or the master-slave database data synchronization method described in any one of claims 7 to 12 is implemented.
16. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the steps of the master-slave database data synchronization method described in any one of claims 1 to 6 or the steps of the master-slave database data synchronization method described in any one of claims 7 to 12 are implemented.
Citation Information
Patent Citations
Cloud database SQL statement monitoring method and monitoring system
CN113505368A
Distributed data real-time synchronization method and system and data synchronization equipment
CN114428820A
Database data read-write method and system and storage medium
CN117592086A
