Method for migrating Mysql data to Redis in batches
By using multi-thread processing and connection pool technology when migrating MySQL data to Redis, the problem of cumbersome and time-consuming migration in the existing technology is solved, an efficient data migration process is achieved, and the migration speed and accuracy are significantly improved.
Patent Information
- Application Number
- CN202310347709.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-03-27
- Publication Date
- 2024-09-27
AI Technical Summary
Existing shell or interpreted scripting languages cannot achieve multi-threaded work when migrating data from multiple Mysql databases to Redis clusters, making large-scale data migration cumbersome and time-consuming.
Obtain the number of CPU cores N through the main thread, start a thread pool containing N worker threads, read the configuration file information, put the task list into the message queue, and establish Mysql and Redis connection pools according to the number of databases, and the worker threads will start from the queue Fetch messages from the database, read the table structure and write data to achieve multi-threaded processing.
It saves data migration time and improves the accuracy and efficiency of the migration process.
Smart Images

Figure CN118708306A
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of data processing, and in particular relates to a method for migrating MySQL data to Redis in batches. Background Art
[0002] Redis is a key-value storage system that supports the storage of value types including string, list, set, zset (sortedset--ordered set) and hash. On this basis, Redis also supports various sorting methods, and periodically writes updated data to disk or writes modification operations to additional record files to achieve master-slave synchronization. In some cases, it can play a good supplementary role to relational databases. Python is a cross-platform computer programming language, and Mysql is a relational database management system. Relational databases store data in different tables instead of putting all data in a large warehouse, which increases speed and flexibility.
[0003] For example, Chinese patent CN109710485A provides a method for collecting Mysql database monitoring data using Python language, obtaining the machine name of the host through Python; putting the connection information of multiple Mysql databases into a string array; connecting to each Mysql database, and querying various indicators of the database, and putting them into a Python dictionary; obtaining the Mysql monitoring indicators, and extracting the values of several important monitoring indicators that need to be collected, mainly including the query rate per second, system throughput, number of slow queries / second, total number of connections created, number of running connections, number of rejected connections, buffer pool indicators; calculating the required monitoring indicator values; organizing the calculated values of each monitoring item into the storage format of a time series database and forwarding them to the monitoring system.
[0004] At present, migrating data from Mysql to Redis is generally done using shell or python programming. For example, Chinese patent CN115480711A discloses a data migration method and related devices, in which the data migration system includes a data migration unit, and the data migration unit includes a thread manager, at least one data reader, and at least one data writer. The thread manager obtains the disk bandwidth of the source cluster, determines the number of threads used to perform data migration based on the disk bandwidth, and establishes one or more threads based on the number of threads. The target thread corresponds to a target data reader and a target data writer. The target data reader obtains the target row key from the message queue, obtains the target data to be migrated corresponding to the target row key from the source cluster based on the target row key, and sends the target data to be migrated to the target data writer. The target data writer stores the target data to be migrated in the target cluster.
[0005] However, for large-scale migration of data from multiple MySQL databases to Redis clusters, Shell or other interpreted scripting languages cannot work in multiple threads, and large-scale data migration will be cumbersome and time-consuming. Summary of the invention
[0006] The purpose of the present invention is to provide a method for migrating Mysql data to Redis in batches, wherein the main thread obtains the number of CPU cores N of the machine, starts a thread pool including N working threads, reads configuration file information, and the main thread puts the read task list into a message queue, etc., thereby realizing the processing process of multi-threaded task processing and large-scale data migration, solving the existing problems, saving data migration time, and improving accuracy.
[0007] In order to solve the above technical problems, the present invention is achieved through the following technical solutions:
[0008] The present invention provides a method for migrating MySQL data to Redis in batches, comprising the following steps:
[0009] The main thread obtains the number of CPU cores N and starts a thread pool containing N worker threads;
[0010] The main thread reads the configuration file information; the method of the main thread reading the configuration file information is:
[0011] The main thread reads the DBManager block name, reads the DBNum tag under the DBManager block, and obtains the number of databases M that need to be accessed; when the main thread reads the DBManager block name, the DBNum tag, and the number of databases, if the DBManager block name is not found or the number of databases is unreasonable, the process is exited;
[0012] Loop to concatenate the block name DB_x, and obtain the information of the data to be migrated under each block name, where 1≤x≤M; in the process of reading the block name, tag name and the corresponding value, if the name is not found or the value is unreasonable, send a message to exit the process gracefully;
[0013] Under the DB_1 block name, read the values corresponding to IP, User, Passwd, DBName, and TableCount respectively to obtain the number of data tables T, and concatenate the tag names TabName, ColumnList, and RedisKeyColumn in a loop to obtain the table name, column name, and Redis key name of the data source;
[0014] Read the Redis block name, and read the values corresponding to the IP_Port and Passwd tags under the Redis block; in the process of reading the block name, tag name, and corresponding value, if the name is not found or the value is unreasonable, send a message to exit the process gracefully;
[0015] The main thread puts the read task list into the message queue;
[0016] According to the number of databases M, use MySQL c++ API to establish M MySQL connection pools;
[0017] Establish a Redis connection pool;
[0018] The worker thread takes the message from the queue, obtains a connection from the corresponding connection pool according to the MySQL database number, and reads the table structure;
[0019] Determine the composition of the Redis key based on the RedisKeyCloumn configured for each task;
[0020] The worker thread reads a record from the MySQL database and writes the record to the Redis cluster.
[0021] Furthermore, in the process of the main thread reading the Redis block name, IP_Port, Passwd tag name and the corresponding value, if the Redis block name is not found or the value is unreasonable, the process is exited.
[0022] Furthermore, the task list includes a Mysql database number, a Mysql table name, a Mysql field name, and a Redis key name.
[0023] Furthermore, each connection pool only establishes one connection with MySQL when it is initialized. When processing a task, it first determines whether there is an idle connection in the corresponding connection pool:
[0024] If there is an idle connection, use the connection to access the database and change the connection status to "in use". After the task is processed, change the connection status to "idle".
[0025] If there is no idle connection, the working thread obtains the corresponding database connection parameters according to the "database number" in the task information;
[0026] Among them, the method for determining whether there are idle connections in the corresponding connection pool is:
[0027] If the connection pool number = the database number in the task information, there will be no idle connections;
[0028] Otherwise, there are idle connections.
[0029] Furthermore, when the Redis connection pool is initialized, only one connection with the Redis cluster is established. When processing a task, it is first determined whether there is an idle connection in the Redis connection pool:
[0030] When there is an idle connection, use the connection to access the Redis cluster and change the connection status to "in use". After the task processing is completed, change the connection status to "idle".
[0031] When there are no idle connections, the worker thread uses the Redis C++ API to establish a connection based on the connection parameters of the Redis cluster.
[0032] Furthermore, when the worker thread reads the table structure, if there is no available connection, a new connection is created; the table structure is read using the c++ API provided by the MySQL official website to see if all configured column names exist; if not, an error message is input to the logger.
[0033] Furthermore, the worker thread only reads configured fields when reading records from the Mysql database; if only the configured fields are successfully transferred to the Redis cluster, the number of successful records is increased by 1; if it fails, the number of failed records is increased by 1, and the error information is input into the logger.
[0034] Furthermore, when all data processing of a task list is completed, the counted Mysql library name, table name, total number of records, number of successful records written to Redis, and number of failed records written to Redis are input to the logger; the logger writes the log file in an asynchronous manner.
[0035] The present invention has the following beneficial effects:
[0036] The present invention obtains the number of CPU cores N of the machine through the main thread, starts a thread pool containing N working threads, the main thread reads the configuration file information, and puts the read task list into the message queue; according to the number of databases M, M Mysql connection pools are established, and a Redis connection pool is established; the working thread takes out the message from the queue, and obtains a connection from the corresponding connection pool according to the Mysql database number; the c++API provided by the Mysql official website is used to read the table structure to see whether the configured column names all exist, and if not, the error information is input into the log recorder. According to the RedisKeyCloumn configured for each task, the composition mode of Redis key is constructed; the working thread reads a record from Mysql, and then writes the record into the Redis cluster. If the transfer is successful, the number of successful entries is increased by 1; if it fails, the number of failed entries is increased by 1, and the error information is input into the log recorder; the processing process of multi-threaded task processing and large-scale data migration is realized, saving data migration time and improving accuracy.
[0037] Of course, any product implementing the present invention does not necessarily need to achieve all of the advantages described above at the same time. BRIEF DESCRIPTION OF THE DRAWINGS
[0038] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the accompanying drawings required for describing the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other accompanying drawings can be obtained based on these accompanying drawings without paying creative work.
[0039] Figure 1 The figure is a schematic diagram of the work of migrating Mysql data to Redis in batches according to the present invention. DETAILED DESCRIPTION
[0040] In the following description, specific details such as specific system structures, technologies, etc. are provided for the purpose of illustration rather than limitation, so as to provide a thorough understanding of the embodiments of the present application. However, it should be clear to those skilled in the art that the present application may also be implemented in other embodiments without these specific details. In other cases, detailed descriptions of well-known systems, devices, circuits, and methods are omitted to prevent unnecessary details from obstructing the description of the present application.
[0041] It should be understood that when used in the present specification and the appended claims, the term "comprising" indicates the presence of described features, wholes, steps, operations, elements and / or components, but does not exclude the presence or addition of one or more other features, wholes, steps, operations, elements, components and / or combinations thereof.
[0042] It should also be understood that the term “and / or” used in the specification and appended claims refers to any and all possible combinations of one or more of the associated listed items, and includes these combinations.
[0043] As used in the specification and appended claims of this application, the term "if" can be interpreted as "when" or "uponce" or "in response to determining" or "in response to detecting", depending on the context. Similarly, the phrase "if it is determined" or "if [described condition or event] is detected" can be interpreted as meaning "uponce it is determined" or "in response to determining" or "uponce [described condition or event] is detected" or "in response to detecting [described condition or event]", depending on the context.
[0044] In addition, in the description of the present application specification and the appended claims, the terms "first", "second", "third", etc. are only used to distinguish the descriptions and cannot be understood as indicating or implying relative importance.
[0045] References to "one embodiment" or "some embodiments" etc. described in the specification of this application mean that one or more embodiments of the present application include specific features, structures or characteristics described in conjunction with the embodiment. Therefore, the statements "in one embodiment", "in some embodiments", "in some other embodiments", "in some other embodiments", etc. that appear in different places in this specification do not necessarily refer to the same embodiment, but mean "one or more but not all embodiments", unless otherwise specifically emphasized in other ways. The terms "including", "comprising", "having" and their variations all mean "including but not limited to", unless otherwise specifically emphasized in other ways.
[0046] See also Figure 1 As shown, the present invention is a method for migrating MySQL data to Redis in batches, comprising the following steps:
[0047] Step 1: The main thread obtains the number of CPU cores N and starts a thread pool containing N worker threads;
[0048] Step 2: The main thread reads the configuration file information; the method for the main thread to read the configuration file information is:
[0049] The main thread reads the DBManager (database manager) block name, reads the DBNum (number of databases) tag under the DBManager block, and obtains the number of databases M that need to be accessed; when the main thread reads the DBManager block name, DBNum tag, and the number of databases, if the block name, tag name, or the number of databases is not found or the number of databases is unreasonable, the process is exited; as an embodiment provided by the present invention, preferably, after the logger completes writing the file, the process is exited; as an embodiment provided by the present invention, preferably, for example, DBNum=abc, when the number of databases is not a positive integer, it indicates an unreasonable situation;
[0050] Loop to concatenate the block name DB_x, and obtain the information of the data to be migrated under each block name, where 1≤x≤M;
[0051] Under the DB_1 block name, read the values corresponding to IP, User, Passwd, DBName, and TableCount respectively to obtain the number of data tables T, and concatenate the tag names TabName, ColumnList, and RedisKeyColumn in a loop to obtain the table name, column name, and Redis key name of the data source; the method for reading data under the DB_2 to DB_M blocks is the same as the method for reading data under the DB_1 block name, and loop processing can be performed when reading configuration items;
[0052] Read the Redis block name, and read the values corresponding to the IP_Port and Passwd tags under the Redis block; in the process of reading the block name, tag name, and corresponding value, if the name is not found or the value is unreasonable, send a message to exit the process gracefully;
[0053] The main thread puts the read task list (each task includes: Mysql database number, Mysql table name, Mysql field name, Redis key name) into the message queue;
[0054] According to the number of databases M, use MySQL c++ API to establish M MySQL connection pools;
[0055] Establish a Redis connection pool;
[0056] The worker thread takes the message from the queue and obtains a connection from the corresponding connection pool according to the MySQL database number (if no connection is available, a new one is created). Use the C++ API provided by the MySQL official website to read the table structure (for example: desc account displays the table structure) to see if the configured column names exist. If not, the error message is input to the logger;
[0057] Determine the composition of the Redis key according to the RedisKeyCloumn configured for each task, for example: account_18812345678, acl_18812345678;
[0058] The worker thread reads a record from the MySQL database and writes the record to the Redis cluster.
[0059] As an embodiment provided by the present invention, preferably, an example of the main thread reading the configuration file information is: Configuration file:
[0060]
[0061]
[0062]
[0063] As an embodiment provided by the present invention, preferably, during the process of the main thread reading the Redis block name, IP_Port, Passwd tag name and the corresponding value, if the Redis block name is not found or the value is unreasonable, the process is exited.
[0064] As an embodiment provided by the present invention, preferably, the task list includes a Mysql database number, a Mysql table name, a Mysql field name, and a Redis key name.
[0065] As an embodiment provided by the present invention, preferably, each connection pool only establishes one connection with MySQL when initialized, and when processing a task, first determine whether there is an idle connection in the corresponding connection pool:
[0066] If there is an idle connection, use the connection to access the database and change the connection status to "in use". After the task is processed, change the connection status to "idle".
[0067] If there is no idle connection, the working thread obtains the corresponding database connection parameters according to the "database number" in the task information;
[0068] Among them, the method for determining whether there are idle connections in the corresponding connection pool is:
[0069] If the connection pool number = the database number in the task information, there will be no idle connections;
[0070] Otherwise, there are idle connections.
[0071] As an embodiment provided by the present invention, preferably, the Redis connection pool only establishes one connection with the Redis cluster when it is initialized. When processing a task, it is first determined whether there is an idle connection in the Redis connection pool:
[0072] When there is an idle connection, use the connection to access the Redis cluster and change the connection status to "in use". After the task processing is completed, change the connection status to "idle".
[0073] When there are no idle connections, the worker thread uses the Redis C++ API to establish a connection based on the connection parameters of the Redis cluster.
[0074] As an embodiment provided by the present invention, preferably, when the working thread reads the table structure, if there is no available connection, a new connection is created; the table structure is read using the c++ API provided by the Mysql official website to see whether the configured column names all exist, and if not, an error message is input into the logger.
[0075] As an embodiment provided by the present invention, preferably, the working thread only reads the configured fields when reading records from the Mysql database; if only the configured fields are read and successfully transferred to the Redis cluster, the number of successful entries is increased by 1; if it fails, the number of failed entries is increased by 1, and the error information (such as: mobile phone number, error code) is input into the log recorder.
[0076] As an embodiment provided by the present invention, preferably, when all data processing of a task list is completed, the counted Mysql library name, table name, total number of records, number of successful records written to Redis, and number of failed records written to Redis are input into a logger (Logger); the logger writes log files in an asynchronous manner.
[0077] A method for batch migration of Mysql data to Redis, wherein the main thread obtains the number of CPU cores N of the machine, starts a thread pool containing N worker threads, the main thread reads the configuration file information, and puts the read task list into the message queue; according to the number of databases M, M Mysql connection pools are established, and a Redis connection pool is established; the worker thread takes out the message from the queue, and obtains a connection from the corresponding connection pool according to the Mysql database number; the c++ API provided by the Mysql official website is used to read the table structure to see whether the configured column names all exist, and if not, the error information is input to the logger. According to the RedisKeyCloumn configured for each task, the composition method of Rediskey is constructed; the worker thread reads a record from Mysql, and then writes the record to the Redis cluster. If the transfer is successful, the number of successful records is increased by 1; if it fails, the number of failed records is increased by 1, and the error information is input to the logger; the processing process of multi-threaded task processing and large-scale data migration is realized, saving data migration time and improving accuracy.
[0078] In the description of this specification, the description with reference to the terms "one embodiment", "example", "specific example", etc. means that the specific features, structures, materials or characteristics described in conjunction with the embodiment or example are included in at least one embodiment or example of the present invention. In this specification, the schematic representation of the above terms does not necessarily refer to the same embodiment or example. Moreover, the specific features, structures, materials or characteristics described can be combined in any one or more embodiments or examples in a suitable manner.
[0079] The preferred embodiments of the present invention disclosed above are only used to help illustrate the present invention. The preferred embodiments do not describe all the details in detail, nor do they limit the invention to the specific implementation methods described. Obviously, many modifications and changes can be made according to the content of this specification. This specification selects and specifically describes these embodiments in order to better explain the principles and practical applications of the present invention, so that those skilled in the art can understand and use the present invention well. The present invention is limited only by the claims and their full scope and equivalents.
Claims
1. A method for migrating MySQL data to Redis in batches, characterized in that: The following steps are involved: The main thread obtains the number of CPU cores N and starts a thread pool containing N worker threads; The main thread reads the configuration file information; The main thread puts the read task list into the message queue; According to the number of databases M, use MySQL c++ API to establish M MySQL connection pools and a Redis connection pool; The worker thread takes the message from the queue, obtains a connection from the corresponding connection pool according to the MySQL database number, and reads the table structure; Determine the composition of the Redis key based on the RedisKeyCloumn configured for each task; The worker thread reads a record from the MySQL database and writes the record to the Redis cluster.
2. A method for migrating MySQL data to Redis in batches according to claim 1, characterized in that: The method for the main thread to read the configuration file information is: The main thread reads the DBManager block name, reads the DBNum tag under the DBManager block, and obtains the number of databases M that need to be accessed; Loop to concatenate the block name DB_x, and obtain the information of the data to be migrated under each block name, where 1≤x≤M; Under the DB_1 block name, read the values corresponding to the IP, User, Passwd, DBName, and TableCount tags respectively to obtain the number of data tables T, and concatenate the tag names TabName, ColumnList, and RedisKeyColumn in a loop to obtain the table name, column name, and Redis key name of the data source; Read the Redis block name, and read the values corresponding to the IP_Port and Passwd tags under the Redis block; in the process of reading the block name, tag name, and corresponding value, if the name is not found or the value is unreasonable, send a message to exit the process gracefully.
3. A method for migrating MySQL data to Redis in batches according to claim 2, characterized in that: When the main thread reads the DBManager block name, DBNum label, and the number of databases, if the DBManager block name is not found or the number of databases is unreasonable, the process is exited.
4. A method for migrating MySQL data to Redis in batches according to claim 2, characterized in that: In the process of the main thread reading the Redis block name, IP_Port, Passwd tag name and the corresponding value, if the Redis block name is not found or the value is unreasonable, the process is exited.
5. A method for migrating MySQL data to Redis in batches according to claim 1, characterized in that: The task list includes MySQL database number, MySQL table name, MySQL field name, and Redis key name.
6. A method for migrating MySQL data to Redis in batches according to claim 1, characterized in that: Each connection pool only establishes one connection with MySQL when it is initialized. When processing a task, first determine whether there is an idle connection in the corresponding connection pool: If there is an idle connection, use the connection to access the database and change the connection status to "in use". After the task is processed, change the connection status to "idle". If there is no idle connection, the working thread obtains the corresponding database connection parameters according to the "database number" in the task information.
7. A method for migrating MySQL data to Redis in batches according to claim 1, characterized in that: When the Redis connection pool is initialized, only one connection with the Redis cluster is established. When processing a task, first determine whether there is an idle connection in the Redis connection pool: When there is an idle connection, use the connection to access the Redis cluster and change the connection status to "in use". After the task processing is completed, change the connection status to "idle". When there are no idle connections, the worker thread uses the Redis C++ API to establish a connection based on the connection parameters of the Redis cluster.
8. A method for migrating MySQL data to Redis in batches according to claim 1, characterized in that: When the worker thread reads the table structure, if there is no available connection, a new connection is created; the c++ API provided by the Mysql official website is used to read the table structure to see whether the configured column names all exist. If not, an error message is input to the logger.
9. A method for migrating MySQL data to Redis in batches according to claim 1, characterized in that: The worker thread only reads the configured fields when reading records from the Mysql database; if only the configured fields are read and successfully transferred to the Redis cluster, the number of successful records is increased by 1; If it fails, the number of failures is increased by 1 and the error information is entered into the logger.
10. A method for migrating MySQL data to Redis in batches according to claim 1, characterized in that: When all data processing of a task list is completed, the counted Mysql library name, table name, total number of records, number of successful records written to Redis, and number of failed records written to Redis are input to the logger; the logger writes the log file in an asynchronous manner.
