sqlserver資料即時同步到mysql

來源:互聯網
上載者:User

1.安裝安裝mysqlconnector2.配置mysqlconnectorODBC資料管理器->系統DSN->添加->mysql ODBC 5.3 ANSI driver->填入data source name如jt,mysql的ip、使用者名稱、密碼即可3.建立連結的伺服器exec sp_addlinkedserver@server='jt', --ODBC裡面data source name@srvproduct='mysql', --自己隨便@provider='MSDASQL', --固定這個@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和mysql建立庫和表create database suzhou;create table sz(id int not null identity(1,1) 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.建立迴環--建立LOOPBACK 伺服器連結EXEC sp_addlinkedserver @server = N'loopback' , @srvproduct = N' ' , @provider = N'SQLNCLI',@datasrc = @@SERVERNAMEgo--設定伺服器連結選項,阻止SQL Server 由於遠程序呼叫而將本地事務提升為分布事務(重點)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 promotion', @optvalue=N'false'GO----6.編寫觸發器和預存程序----6.1 insert--重寫觸發器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--預存程序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--重寫觸發器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--預存程序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資料測試use suzhougoupdate sz set remark='ocpyang' where orderno='a001'go----6.3 delete--重寫觸發器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--預存程序use suzhougocreate PROCEDURE sp_delete(@orderno char(20))ASBEGINSET NOCOUNT ON;delete openquery(jt, 'select * from sz') where orderno=@ordernoENDgo---delete資料測試use suzhougodelete from sz where orderno='a001'go

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.