先安装mysql的odbc驱动,配置数据源,然后sqlserver执行代码:
/****** Object: LinkedServer [TEST_MYSQL] Script Date: 01/08/2020 12:06:42 ******/ EXEC master.dbo.sp_addlinkedserver @server = N'TEST_MYSQL', @srvproduct=N'MySQL', @provider=N'MSDASQL', @provstr=N'DRIVER={MySQL ODBC 8.0 Unicode Driver};SERVER=192.168.1.8;DATABASE=database123;UID=name123;PORT=3306;' /* For security reasons the linked server remote logins password is changed with ######## */ EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N'TEST_MYSQL',@useself=N'False',@locallogin=NULL,@rmtuser=N'database123',@rmtpassword='12345678' GO EXEC master.dbo.sp_serveroption @server=N'TEST_MYSQL', @optname=N'collation compatible', @optvalue=N'false' GO EXEC master.dbo.sp_serveroption @server=N'TEST_MYSQL', @optname=N'data access', @optvalue=N'true' GO EXEC master.dbo.sp_serveroption @server=N'TEST_MYSQL', @optname=N'dist', @optvalue=N'false' GO EXEC master.dbo.sp_serveroption @server=N'TEST_MYSQL', @optname=N'pub', @optvalue=N'false' GO EXEC master.dbo.sp_serveroption @server=N'TEST_MYSQL', @optname=N'rpc', @optvalue=N'false' GO EXEC master.dbo.sp_serveroption @server=N'TEST_MYSQL', @optname=N'rpc out', @optvalue=N'true' GO EXEC master.dbo.sp_serveroption @server=N'TEST_MYSQL', @optname=N'sub', @optvalue=N'false' GO EXEC master.dbo.sp_serveroption @server=N'TEST_MYSQL', @optname=N'connect timeout', @optvalue=N'0' GO EXEC master.dbo.sp_serveroption @server=N'TEST_MYSQL', @optname=N'collation name', @optvalue=null GO EXEC master.dbo.sp_serveroption @server=N'TEST_MYSQL', @optname=N'lazy schema validation', @optvalue=N'false' GO EXEC master.dbo.sp_serveroption @server=N'TEST_MYSQL', @optname=N'query timeout', @optvalue=N'0' GO EXEC master.dbo.sp_serveroption @server=N'TEST_MYSQL', @optname=N'use remote collation', @optvalue=N'true' GO EXEC master.dbo.sp_serveroption @server=N'TEST_MYSQL', @optname=N'remote proc transaction promotion', @optvalue=N'false' GO