最近需要在SQL Server中访问MySQL的数据,故写下一下文章作为记录。
总体步骤:配置ODBC -> 建立新的链接服务器 -> 测试
一、配置ODBC
1.1 在SQL Server 服务器上安装MySQL的ODBC驱动
下载地址:https://dev.mysql.com/downloads/connector/odbc/
1.2 安装好ODBC驱动后,在SQL Server服务器上配置系统DNS
填写相应的参数
例子:
Data Source Name: bps
TCP/IP Server:localhost Port:3306
User: root
Password: root
Database: bps
二、建立新的链接服务器
右键“链接服务器”-> 新建链接服务器
新建链接服务器对应的脚本为:
user[master]
GO
EXEC master.dbo.sp_addlinkedserver @server = N'BPS', @srvproduct=N'bpsProj', @provider=N'MSDASQL', @datasrc=N'bps', @provstr=N'Server=localhost;Database=bps;User=root;Password=root;'
GO
EXEC master.dbo.sp_serveroption @server=N'BPS', @optname=N'collation compatible', @optvalue=N'false'
GO
EXEC master.dbo.sp_serveroption @server=N'BPS', @optname=N'data access', @optvalue=N'true'
GO
EXEC master.dbo.sp_serveroption @server=N'BPS', @optname=N'dist', @optvalue=N'false'
GO
EXEC master.dbo.sp_serveroption @server=N'BPS', @optname=N'pub', @optvalue=N'false'
GO
EXEC master.dbo.sp_serveroption @server=N'BPS', @optname=N'rpc', @optvalue=N'false'
GO
EXEC master.dbo.sp_serveroption @server=N'BPS', @optname=N'rpc out', @optvalue=N'false'
GO
EXEC master.dbo.sp_serveroption @server=N'BPS', @optname=N'sub', @optvalue=N'false'
GO
EXEC master.dbo.sp_serveroption @server=N'BPS', @optname=N'connect timeout',@optvalue=N'0'
GO
EXEC master.dbo.sp_serveroption @server=N'BPS', @optname=N'collation name', @optvalue=null
GO
EXEC master.dbo.sp_serveroption @server=N'BPS', @optname=N'lazy schema validation', @optvalue=N'false'
GO
EXEC master.dbo.sp_serveroption @server=N'BPS', @optname=N'query timeout', @optvalue=N'0'
GO
EXEC master.dbo.sp_serveroption @server=N'BPS', @optname=N'use remote collation', @optvalue=N'true'
GO
EXEC master.dbo.sp_serveroption @server=N'BPS', @optname=N'remote proc transaction promotion', @optvalue=N'true'
GO
USE [master]
GO
EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname = N'BPS', @locallogin = NULL , @useself = N'False'
GO
访问接口字符串可以参考以下网址:https://www.connectionstrings.com/mysql-connector-net-mysqlconnection/