mysql to mssql real-time synchronization solutions

Database is an essential part of the application, mysql is open source, so it is a lot of people, mssql is Microsoft, with the windows platform is very convenient, so there are a lot of people use it. Now the question is, how to synchronize the two databases, namely data contents remain exactly the same.

MySQL Migration Toolkit is an open source software tool provided by MySQL GUI can be, Microsoft SQL Server, Oracle, MySQL , Sybase Server, MaxDB Database Server database data to MySQL database migration for Microsoft Access, which would address the problems mssql to mysql synchronization of the; SQL Server migration Assistant (SSMA) team has developed 2008 for MySQL MySQL migration Assistant for Microsoft SQL Server migration Assistant. Microsoft also released three other Migration Assistant: SSMA for Access, SSMA for Oracle , and SSMA for Sybase (all v4.2), so it can solve the problem mysql to mssql, and methods are detailed: MySQL migration tool to MsSQL - -SSMA , to achieve the end.

……

Joke, the above said very easy to use tool, you can keep a spare. The real question now is: How do these two databases in real-time synchronization (mysql to mssql), ie, content data in real time to keep exactly the same. To put it plainly, it is to mysql insert (or modify, delete) a record, mssql followed inserted simultaneously (or modify, delete) the record is synchronized in real time, without fail.

problem

The so-called mysql to mssql real-time synchronization of data. Its requirements are:

(1) when a record is inserted in the database mysql, mssql also inserted into the corresponding database record;

(2) When the database update some records mysql, mssql also updates the corresponding database record;

(3) to delete a record in the database when mysql, mssql also delete the corresponding database record;

These records changes in the time and manner are random and uncertain, so it needs to monitor changes at any time. At the same time, the following agreement:

A. In the database prior to synchronization, mysql and mssql database structure and content exactly the same;

B. only the data insert, update and delete operations, does not change the primary key and structures.

analysis

To achieve real-time database synchronization solutions are generally three ways: First write a program to operate two databases, first delete the target database, then re-generate the target database according to the original database, but this method for large amount of data in the database operation is clearly impracticable; second is the use of SQL triggers, stored procedures and timing of jobs to accomplish real-time synchronization; third is to use third-party software, such as SyncNavigator, but the overall economy, stability and ease of maintenance and other factors, this program carefully use.

achieve

(1) New and mssql mysql temporary temporary databases, data structures corresponding to the official database rather, only to add a new field in each table OpType (operation for recording data modification);

(2) to establish trigger mysql database, when the action is to change the mysql database, mysql temporary database table generating respective corresponding record, and the record fields OpType (I represent insertion recording, U represents a modified record, D denotes delete records);

(3) the connection between the new mysql mssql temporary database and the temporary database;

(4) In the new database mssql temporarily stored procedures, when executing stored procedures, updates mssql temporary database to make it fully consistent with the temporary mysql database, while discarding the contents of the temporary mysql database;

(5) the timing of the new job, the timing to call mssql temporary database stored procedure;

(6) establish a temporary database trigger in mssql, according to OpType field type, mssql database update, and delete the contents mssql temporary database.

Thus, to achieve a mysql database mssql database of real-time synchronization, which, mssql mysql temporary database and temporary database only process data, after synchronization is complete, the record data in the table will be deleted. Here the so-called real time, but also on the timing planned mssql jobs.

A method of connecting mysql Mssql

1
2
3
4
5
6
7
8
9
10
EXEC  sp_addlinkedserver
@server =  'MySQL' ,
@srvproduct= 'MySql'  ,
@provider =  'MSDASQL' ,
@provstr =  'Driver={MySQL ODBC 5.2 UNICODE Driver};
Server=localhost;
Database=mysql_temp;
User=root;
Password=****;
Option=3;'

  增加权限:

1
2
3
4
5
6
EXEC  sp_addlinkedsrvlogin
@rmtsrvname= 'MySql'  ,
@useself= 'false'  ,
@locallogin= 'sa'  ,
@rmtuser= 'root'  ,
@rmtpassword= '****'

  优化

由于实际数据库可能在极短时间内对一条记录进行多次频繁更新,为防止同步错误,可以两张临时表中再建立状态字段(IsUpdate)。具体操作方法:

(1)当mysql修改记录时,触发的mysql_temp表中的IsUpdate字段值标记为0;

(2)当存储过程调用时,将mysql_temp表中更新到mssql_temp的同时,将mysql_temp表中的IsUpdate字段值标记为1,并删除值为1的记录;(先更新值,再复制,最后删除,下同。)

(3)mssql_temp触发器运行时,除了将mssql_temp的记录更新到mssql外,还将删除mssql_temp表中IsUpdate字段值标记为2,并删除值为2的记录。

这样,可达到两个效果:一是更新永远是实时同步,不会有误;二是可减少存储过程的调用频率,可节省资源。

 

Guess you like

Origin www.cnblogs.com/SyncNavigator8-4-1/p/10977485.html