Design and implementation of MySQL database synchronization tool

One, background

  During the test, the test for different teams for different testing purposes, we may have multiple sets of test environment. In product version iterative process, according to business needs, the structure of the database will be some changes, such as: add tables, fields, indexes, modify tables, fields, indexes and other operations, in some process is not standardized companies, developers do not follow standardize operations, SQL is not timely submit these changes to the database to SVN / Git, when the modified code is deployed to a new business environment will lead to error, thus affecting the efficiency of the test. Another point to say, even if the process specification of large companies, have adopted the core business infrastructure sub-library sub-table, and thousands tables do we have adopted to execute the SQL manually add and modify fields it? This of course is wrong, there may be students think, we can take the form of a scripting language batch update and modify the corresponding database, this is also a way, but only if this is a very clear difference between executives of the two databases, if executives had no idea the differences between the two databases do? Some students may think they can source database structure and data which are imported into the target database, this would resolve. So plausible, but actually wrong. Earlier we said, several sets of test environments, their role may be different, for example: test environment for internal testing, the FBI and the FBI external environment for the system, if we put the database structure and test environment All data import FBI environment, the environment of the original FBI data does not exist, and no longer outside the FBI, so this is not a good way.

  For all of these reasons, a database structure synchronization tool looks like a better solution.

Second, realize the function

Based on the above analysis, the tool need to implement the following three functions

  • Analysis (diff): difference in the analysis of the source and target databases structure, the synchronization and recommendations to determine the difference before copying the source and target databases to perform the analysis;
  • Synchronization (sync): Structure Only database synchronization, data is not synchronized;
  • Copy (copy): In the case of data not required can be used directly copied into the target database all databases and data structures of the source database;

Third, the realization of ideas

Specific process is as follows:

  • Parses the incoming instruction, comprising: IP source and target databases, port number, user name, password, and name of the database to perform an action (diff, sync, copy);
  • Analysis db, execute SQL;
  • Under the analysis db table, execute SQL;
  • Fields and indexes analysis table, execute SQL;

Fourth, the analysis process

We want to compare and analyze, including the structure of the database: the database, the database the following tables, fields and indexes in the table, that we should be specific on how to analyze and compare it?

Since we do is MySQL database synchronization tools, then we need to have a little in-depth understanding of the MySQL database. In MySQL, the INFORMATION_SCHEMA seen as a database, to be exact information database. Which holds all the information about the current MySQL other databases maintained by the server. Such as database names, field and index database tables, tables and access rights, and so on. So we should be concerned about the INFORMATION_SCHEMA following several tables:

  • SCHEMATA: provides information about the current mysql instance for all databases. SHOW DATABASES results taken from this table;
  • TABLES: provides information about the database tables (including views). A detailed presentation of the table belongs to which schema, table type, table engine, creation time and other information. Results SHOW TABLES FROM SCHEMANAME taken from the table;
  • COLUMNS: provides column information table. Detailed presentation of all information for each column and row of a particular table. Results SHOW COLUMNS FROM SCHEMANAME.TABLENAME taken from the table;
  • STATISTICS: provides information about table indexes. Results SHOW INDEX FROM SCHEMANAME.TABLENAME taken from the table;

Key SQL:

  • SELECT * FROM SCHEMATA WHERE SCHEMA_NAME='XXX';
  • SELECT * FROM TABLES WHERE TABLE_SCHEMA='XXX' AND TABLE_NAME='XX';
  • SELECT * FROM COLUMNS WHERE TABLE_SCHEMA='XXX' AND TABLE_NAME='XX';
  • SELECT * FROM STATISTICS WHERE TABLE_SCHEMA='XXX' AND TABLE_NAME='XX';

Fifth, code implementation

 

Six issues

Guess you like

Origin www.cnblogs.com/L-Test/p/11668928.html