Synchronize SQL server data to mysql in real time
1. install and install mysqlconnector2. configure mysqlconnectorODBC data Manager-> system DSN-> Add-> mysql ODBC 5.3 ANSI driver-> Fill in data source name such as jt, mysql ip address, user name, and password. create a connection server exec sp_addmediaserver @ server = 'jt ', -- data source name @ srvproduct = 'mysql' in ODBC, -- casually @ provider = 'msdasql ', -- fix this @ datasrc = NULL, @ location = NULL, @ provstr = 'driver = {MySQL ODBC 5.3 ansi driver}; SERVER = 192.168.5.188; DATABASE = suzhou; UID = root; PORT = 3306; ', @ catalog = NULLexec sp_add1_srvlogin @ rmtsrvname = 'jt', @ useself = 'false', @ rmtuser = 'root', @ rmtpassword = 'Password '; select * from openquery (jt, 'select * FROM sz; ') GOUSE [master] GOEXEC master. dbo. sp_serveroption @ server = n'jt ', @ optname = n'rpc out', @ optvalue = n'true' GOEXEC master. dbo. sp_serveroption @ server = n'jt ', @ optname = n'remote proc transaction promotion', @ optvalue = n'false' GO --- 4. sqlserver and mysql create database AND table create database suzhou; create table sz (id int not null identity () primary key, orderno char (20) not null, ordertime datetime not null default getdate (), remark varchar (200) gocreate table sz (id int (11) not null, orderno char (20) not null, ordertime datetime (6) not null, remark varchar (200), primary key (id) engine = innodb default charset = utf8; --- 5. create LOOPBACK -- create loopback server link EXEC sp_addmediaserver @ server = n'loopback', @ srvproduct = n'', @ provider = n'sqlncli ', @ datasrc = @ SERVERNAMEgo -- sets the Server link option to prevent SQL Server from upgrading local transactions to distributed transactions due to Remote Procedure Calls (important) USE [master] GOEXEC master. dbo. sp_serveroption @ server = n'loopback', @ optname = n'rpc out', @ optvalue = n'true' GOEXEC master. dbo. sp_serveroption @ server = n'loopback', @ optname = n'remote proc transaction prodemo', @ optvalue = n'false' GO ---- 6. writing triggers and stored procedures ---- 6.1 insert -- rewrite the trigger use suzhougoalter trigger tr_insert_sz on suzhou. dbo. szfor insertasdeclare @ id int, @ orderno char (20), @ ordertime datetime, @ remark varchar (200) select @ id = id, @ orderno = orderno, @ ordertime = ordertime, @ remark = remark from inserted; beginprint @ idprint @ ordernoprint @ ordertimeprint @ remarkexec loopback. suzhou. dbo. sp_insert @ id, @ orderno, @ ordertime, @ remarkendgo -- stored PROCEDURE use suzhougocreate PROCEDURE sp_insert (@ id int, @ orderno char (20), @ ordertime datetime, @ remark varchar (200) asbeginset nocount on; Insert openquery (jt, 'select * from sz ') (id, orderno, ordertime, remark) values (@ id, @ orderno, @ ordertime, @ remark) ENDgo ---- 6.2 update -- rewrite the trigger use suzhougocreate trigger tr_update_sz on suzhou. dbo. szfor updateasdeclare @ orderno char (20), @ remark varchar (200) select @ orderno = orderno, @ remark = remark from inserted; beginexec loopback. suzhou. dbo. sp_update @ orderno, @ remarkendgo -- stored PROCEDURE use suzhougocreate PROCEDURE sp_update (@ orderno char (20), @ remark varchar (200) asbeginset nocount on; update openquery (jt, 'select * from sz ') set remark = @ remark where orderno = @ ordernoENDgo --- update data test use suzhougoupdate sz set remark = 'ocpyang' where orderno = 'a001' go ---- 6.3 delete -- rewrite trigger use suzhougocreate trigger tr_delete_sz on suzhou. dbo. szfor deleteasdeclare @ orderno char (20) select @ orderno = orderno from deleted; beginexec loopback. suzhou. dbo. sp_delete @ ordernoendgo -- stored PROCEDURE use suzhougocreate PROCEDURE sp_delete (@ orderno char (20) asbeginset nocount on; delete openquery (jt, 'select * from sz ') where orderno = @ ordernoENDgo --- delete data test use suzhougodelete from sz where orderno = 'a001' go