SyncNavigator是一款功能強大的數據庫同步軟件,適用於SQL SERVER, MySQL,具備自動/定時同步數據、無人值守、故障自動恢復、同構/異構數據庫同步、斷點續傳和增量同步等功能,支持Windows xp以上全部操做系統,適用於大容量數據庫快速同步。html
安裝包下載地址:https://www.syncnavigator.cn/Setup.zipmysql
幫助文檔地址:https://www.syncnavigator.cn/Help_zh-CN.chmsql
Web文檔地址:https://www.syncnavigator.cn/chm/index.htm數據庫
由於業務須要要要實現 sqlserver和 mysql 異構數據庫 數據同步;起先一點思路都沒有,因而到網上一頓好找;服務器
最後找到了2種解決方案:session
一、利用第三方工具實現;ide
二、利用ODBC實現;工具
第一種測試結果不理想,弊端是:一、不穩定 二、出了問題不易定位三、不夠智能oop
最後採用了第二種ODBC來實現,運行了快1年沒有出過問題;今天共享出來以幫助其餘朋友;sqlserver
---安裝安裝mysqlconnector http://www.mysql.com/products/connector/ /* 配置mysqlconnector ODBC數據管理器->系統DSN->添加->mysql ODBC 5.3 ANSI driver->填入data source name如jt,mysql的ip、用戶名、密碼便可
--新建連接服務器
exec sp_addlinkedserver
@server='MySqll_Aggregation' , --ODBC裏面data source name
@srvproduct='MySql' , --本身隨便
@provider='MSDASQL' , --固定這個
@datasrc=NULL,
@location=NULL,
@provstr='DRIVER={MySQL ODBC 5.3 ANSI Driver};SERVER=172.17.22.3;DATABASE=bi;UID=zhaowenzhong;PORT=3306;',
@catalog = NULL
---建立鏈接mysql數據庫的帳號及密碼
exec sp_addlinkedsrvlogin
@rmtsrvname='MySqll_Aggregation' ,----ODBC裏面data source name
@useself='false' ,
@rmtuser='zhaowenzhong' ,---mysql帳號
@rmtpassword='mysqldba@2015' ;--mysql帳號其密碼
---測試是否能夠訪問mysql數據庫中的表
select * from openquery(MySqll_Aggregation,'SELECT * FROM tb; ')
-----創建容許遠程訪問鏈接操做
USE [master]
GO
EXEC master .dbo. sp_serveroption @server =N'MySqll_Aggregation' , @optname= N'rpc out', @optvalue=N'TRUE'
GO
EXEC master .dbo. sp_serveroption @server =N'MySqll_Aggregation' , @optname= N'remote proc transaction promotion', @optvalue =N'false'
GO
--創建LOOPBACK 服務器連接
EXEC sp_addlinkedserver @server = N'loopback' , @srvproduct = N' ' , @provider = N'SQLNCLI',
@datasrc = @@SERVERNAME
go
--設置服務器連接選項,阻止SQL Server 因爲遠過程調用而將本地事務提高爲分佈事務(重點)
USE [master]
GO
EXEC master .dbo. sp_serveroption @server =N'loopback', @optname= N'rpc out', @optvalue=N'TRUE'
GO
EXEC master .dbo. sp_serveroption @server =N'loopback', @optname= N'remote proc transaction promotion' , @optvalue=N'false'
GO
---編寫觸發器和存儲過程
--insert
CREATE TRIGGER TR_INSERT_TB ON DB_TY2015 .DBO. TB
FOR INSERT
AS
DECLARE @ID INT,
@QTY INT
SELECT @ID =ID, @QTY=QTY FROM INSERTED;
BEGIN
EXEC loopback .db_ty2015. dbo.sp_insert @id, @qty;
END
CREATE PROCEDURE SP_INSERT
@ID INT ,
@QTY INT
AS
BEGIN
SET NOCOUNT ON
INSERT OPENQUERY (db_ty2015, 'select * from tb')(id ,qty) values(@id ,@qty);
SET NOCOUNT OFF
END
---update
CREATE TRIGGER TR_UPDATE_TB ON DB_TY2015 .DBO. TB
FOR UPDATE
AS
DECLARE @ID INT, @QTY INT
SELECT @ID =ID, @QTY=QTY FROM INSERTED;
BEGIN
EXEC loopback .db_ty2015. dbo.sp_update @id, @qty;
END
CREATE PROCEDURE SP_UPDATE
@ID INT ,
@QTY INT
AS
BEGIN
SET NOCOUNT ON
UPDATE OPENQUERY (db_ty2015, 'select * from tb') set qty =@qty where id =@id
SET NOCOUNT OFF
END
--delete
CREATE TRIGGER TR_DELETE_TB ON DB_TY2015 .DBO. TB
FOR DELETE
AS
DECLARE @ID INT
SELECT @ID =ID FROM DELETED ;
BEGIN
EXEC loopback .db_ty2015. dbo.sp_DELETE @id;
END
CREATE PROCEDURE SP_DELETE
@ID INT
AS
BEGIN
SET NOCOUNT ON
DELETE OPENQUERY (db_ty2015, 'select * from tb') where id =@id
SET NOCOUNT OFF
END
-------初始化數據 表已存在的狀況
insert openquery (MySqll_Aggregation, 'select * from bi.tb') select * from [FSLogin] .[dbo]. [tb] with(nolock )
---從mysql同步表結構及數據到sqlserver上(與本案例無關)
select * into [SqlServerDBName].dbo .tb from openquery (localmysql, 'select * from mysqldbname.weibosession')
由於業務須要要要實現 sqlserver和 mysql 異構數據庫 數據同步;起先一點思路都沒有,因而到網上一頓好找;
最後找到了2種解決方案:
一、利用第三方工具實現;
二、利用ODBC實現;
第一種測試結果不理想,弊端是:一、不穩定 二、出了問題不易定位三、不夠智能
最後採用了第二種ODBC來實現,運行了快1年沒有出過問題;今天共享出來以幫助其餘朋友;
---安裝安裝mysqlconnector http://www.mysql.com/products/connector/ /* 配置mysqlconnector ODBC數據管理器->系統DSN->添加->mysql ODBC 5.3 ANSI driver->填入data source name如jt,mysql的ip、用戶名、密碼便可
--新建連接服務器
exec sp_addlinkedserver
@server='MySqll_Aggregation' , --ODBC裏面data source name
@srvproduct='MySql' , --本身隨便
@provider='MSDASQL' , --固定這個
@datasrc=NULL,
@location=NULL,
@provstr='DRIVER={MySQL ODBC 5.3 ANSI Driver};SERVER=172.17.22.3;DATABASE=bi;UID=zhaowenzhong;PORT=3306;',
@catalog = NULL
---建立鏈接mysql數據庫的帳號及密碼
exec sp_addlinkedsrvlogin
@rmtsrvname='MySqll_Aggregation' ,----ODBC裏面data source name
@useself='false' ,
@rmtuser='zhaowenzhong' ,---mysql帳號
@rmtpassword='mysqldba@2015' ;--mysql帳號其密碼
---測試是否能夠訪問mysql數據庫中的表
select * from openquery(MySqll_Aggregation,'SELECT * FROM tb; ')
-----創建容許遠程訪問鏈接操做
USE [master]
GO
EXEC master .dbo. sp_serveroption @server =N'MySqll_Aggregation' , @optname= N'rpc out', @optvalue=N'TRUE'
GO
EXEC master .dbo. sp_serveroption @server =N'MySqll_Aggregation' , @optname= N'remote proc transaction promotion', @optvalue =N'false'
GO
--創建LOOPBACK 服務器連接
EXEC sp_addlinkedserver @server = N'loopback' , @srvproduct = N' ' , @provider = N'SQLNCLI',
@datasrc = @@SERVERNAME
go
--設置服務器連接選項,阻止SQL Server 因爲遠過程調用而將本地事務提高爲分佈事務(重點)
USE [master]
GO
EXEC master .dbo. sp_serveroption @server =N'loopback', @optname= N'rpc out', @optvalue=N'TRUE'
GO
EXEC master .dbo. sp_serveroption @server =N'loopback', @optname= N'remote proc transaction promotion' , @optvalue=N'false'
GO
---編寫觸發器和存儲過程
--insert
CREATE TRIGGER TR_INSERT_TB ON DB_TY2015 .DBO. TB
FOR INSERT
AS
DECLARE @ID INT,
@QTY INT
SELECT @ID =ID, @QTY=QTY FROM INSERTED;
BEGIN
EXEC loopback .db_ty2015. dbo.sp_insert @id, @qty;
END
CREATE PROCEDURE SP_INSERT
@ID INT ,
@QTY INT
AS
BEGIN
SET NOCOUNT ON
INSERT OPENQUERY (db_ty2015, 'select * from tb')(id ,qty) values(@id ,@qty);
SET NOCOUNT OFF
END
---update
CREATE TRIGGER TR_UPDATE_TB ON DB_TY2015 .DBO. TB
FOR UPDATE
AS
DECLARE @ID INT, @QTY INT
SELECT @ID =ID, @QTY=QTY FROM INSERTED;
BEGIN
EXEC loopback .db_ty2015. dbo.sp_update @id, @qty;
END
CREATE PROCEDURE SP_UPDATE
@ID INT ,
@QTY INT
AS
BEGIN
SET NOCOUNT ON
UPDATE OPENQUERY (db_ty2015, 'select * from tb') set qty =@qty where id =@id
SET NOCOUNT OFF
END
--delete
CREATE TRIGGER TR_DELETE_TB ON DB_TY2015 .DBO. TB
FOR DELETE
AS
DECLARE @ID INT
SELECT @ID =ID FROM DELETED ;
BEGIN
EXEC loopback .db_ty2015. dbo.sp_DELETE @id;
END
CREATE PROCEDURE SP_DELETE
@ID INT
AS
BEGIN
SET NOCOUNT ON
DELETE OPENQUERY (db_ty2015, 'select * from tb') where id =@id
SET NOCOUNT OFF
END
-------初始化數據 表已存在的狀況
insert openquery (MySqll_Aggregation, 'select * from bi.tb') select * from [FSLogin] .[dbo]. [tb] with(nolock )
---從mysql同步表結構及數據到sqlserver上(與本案例無關)
select * into [SqlServerDBName].dbo .tb from openquery (localmysql, 'select * from mysqldbname.weibosession')