MySQL database server data synchronization across

1. Background
Recently, the company and the open source project Zentao project requirements for data butt, because the larger the risk of changes to the open source project, using the related tables Zentao database with local database synchronization program. I tested the feasibility of the program.
. mysql Version: 5.7.20
Local database A (as Zentao database), the local database B, Native C; native to the Windows environment.

2. Program exploration
program 1
project manager mean written in A flip-flop, when the data in the table changes the ID issued to C, query data changes in A by C, writes B. Required during installation mysql-udf-http plug (Reference: https: //www.2cto.com/database/201801/713571.html).
But after inquiries found the problem: The plug-in supports only to MySQL5.5X and can only be installed in a Linux environment, conflict, rejection of the project.

Scenario 2
asked the technical manager of the idea:

A change in the monitoring data with the trigger, and then call the stored procedure B to push data to, the database library by cross BDlink.

Query data, DBlink is the oracle of cross-database access methods, SQL Server can be used linkServer. In mysql5.0 version of the above, you can use FEDERATED engine (the default engine is InnoDB). (Reference: https: //blog.csdn.net/langkeziju/article/details/50462943)
Use: Engine FEDERATED table when construction of the table and the remote database, that this statement

ENGINE=FEDERATED CONNECTION=‘mysql://user:password@ip:port/test1/test_table’;
如下:

CREATE TABLE federated_table (
id int(20) NOT NULL auto_increment,
name varchar(32) NOT NULL default '',
age varchar(4) NOT NULL default '0',
sts char(1) NOT NULL default '0',
office VARCHAR(64) NOT NULL default '0',
telephone VARCHAR(15) NOT NULL default '0',
PRIMARY KEY (id),
KEY name (name),
KEY age (age),
KEY sts (sts),
KEY telephone (telephone),
KEY office (office)

) ENGINE=FEDERATED CONNECTION='mysql://user:password@ip:port/test1/test_table';
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
provided that you can access a remote database, configure read and write access, mysql open FEDERATED engine.
After the construction of local data with remote data, the operation can be performed curd synchronization.

Personal understanding: connection establishment is to map the remote data to the local tables inside, or the data is actually stored on a remote database, just opened a local read-write "client." I wrote federated_table a trigger operate properly modified B, the data is modified in A, B table data changes but not fire.

Since data can be synchronized over by FEDERATED, you can establish a mapping A in B.
Because C in the program map table and no real meaning, some field mapping table data need to be synchronized with the actual business table B, or may be used to operate the program using triggers and stored procedures.

Triggers Example:

show TRIGGERS; # View all the triggers
drop TRIGGER updateFromFed; # delete trigger, need to remove the build can be modified

delimiter $$ #修改语句结束标志为$$
create TRIGGER insertFromFederated AFTER INSERT
on federated_table for each ROW
begin #状态=1执行插入操作
DECLARE de VARCHAR(128);
set de = CONCAT(IFNULL(new.name,''),'-',IFNULL(new.age,''),'-',IFNULL(new.office,''));
if new.sts="1" THEN
INSERT into local1(id,name,description,sts)
values (new.id,new.name,de,new.sts);
INSERT into local2(id,name,office,sts)
values (new.id,de,new.office,new.sts);
END IF;
END $$
delimiter ;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Reference: https: //blog.csdn.net/qq_33556185/article/details/77745449

Stored procedure example:

#mysql The following example sets the parameters of the write select into ID dead time value is null, but the use of the ID passed in parameter values as parameter set is not null.
show create procedure test; # View
CALL test (); # call
drop procedure if exists test; # delete

delimiter $$
create PROCEDURE test()
begin
DECLARE id VARCHAR(32);
SET id='8302';
DECLARE v_name VARCHAR(128)
SELECT local2.name INTO v_name from local2 where id = 5;
SELECT id,v_name;
#INSERT into local2(id,name,office,sts) VALUES (id,des,'办公室','8');
end $$
delimiter ;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
参考:http://www.shouce.ren/api/view/a/11695

Scenario 2 give up
in the early stages determined using Option 2, but in truth discussions with users and DBA discovered a new problem:

1.FEDERATED engines are available, but need to modify the server address in the case of the occurrence of the target server migration, re-construction of the table, maintenance more trouble;
modify uncontrolled 2.Zentao database, and can synchronize its state database but can not be controlled with B changes conflict.

3. finally established
in order to solve the above problem 2, only one of the user engagement modified at the same time, by controlling the status bits.
To solve the problem 1, ETL decided kettle for operation, or write A / B two local databases directly procedure C, and the data A timing synchronization by a timer.
As a result of problems involving the late kettle operation and maintenance of the product, we tend to use the latter method.

Guess you like

Origin www.cnblogs.com/HKROnline-SyncNavigator/p/10971499.html