SQL Server data synchronizes to MySQL in real time

Source: Internet
Author: User
Tags datetime odbc mysql in create database

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, username, password 3. Create a new linked server exec sp_addlinkedserver@server= ' JT ',--ODBC inside data source name@srvproduct= ' MySQL ' ,--Own casually @provider= ' msdasql ',--fixed 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_addlinkedsrvlogin@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 new Library and table CREATE database Suzhou;create table sz (ID int NOT NULL identity (1,1) primary Key,orderno char ( Not null,ordertime datetime NOT NULL default GETDATE (), remark varchar) gocreate table sz (id int (Not NULL, OrderNo char (a) Not null,ordertime datetime (6) is not NULL, remark varchar (), primary key (ID)) Engine=innod B Default Charset=utf8---5. Establish a loop--establish loopback server link exec sp_addlinkedserver @server = n ' loopback ', @srvproduct = N ', @pr Ovider = N ' SQLNCLI ', @datasrc = @ @SERVERNAMEgo--Sets the server link option to prevent SQL Server from promoting local transactions to distributed transactions (emphasis) use because of a far-process call [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 ', @optvalue promotion ' false ' Go----6. Write triggers and stored procedures----6.1 insert--rewrite triggers use Suzhougoalter trigger TR_INSERT_SZ on suzhou.dbo.szfor insertasdeclare @id int, @orderno char, @ordertime datetime, @remark varchar 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 (@ordertime datetime, @remark varchar) asbeginset NOCOUNT On;insert openquery (JT, ' SELECT * From Sz ') (Id,orderno,ordertime,remark) VALUES (@id, @orderno, @ordertime, @remark) endgo----6.2 update--rewrite triggers use Suzhougocreate trigger TR_UPDATE_SZ on suzhou.dbo.szfor updateasdeclare @orderno char (@remark varchar) SELECT @ Orderno=orderno, @remark =remark from Inserted;beginexec loopback.suzhou.dbo.sp_update @orderno, @ The remarkendgo--stored procedure use suzhougocreate PROCEDURE sp_update (@orderno char (), @remark varchar) asbeginset 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 triggers use suzhougocreate trigger-TR_DELETE_SZ on Suzhou.dbo.szfor Deleteasdeclare @orderno Char (a) Select @orderno =orderno from Deleted;beginexec Loopback.suzhou.dbo.sp_delete @ordernoendgo--Stored procedure use suzhougocreate PROCEDURE sp_delete (@orderno char (20)) AsbeginsetNOCOUNT On;delete OPENQUERY (JT, ' select * from sz ') where orderno= @ordernoENDgo---Delete data test use Suzhougodelete from SZ whe Re orderno= ' a001 ' Go

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.