Matthew MacFarland
Matthew MacFarland

Reputation: 2731

How can I create a SQL server linked server to MySQL without a DSN?

I would like to create a linked server to MySQL but I prefer not to reference a DSN because it's more management overhead to set that up. In many environment I can usually replace a DSN with a connection string that has all the bits of data that would normally be configured and saved with the DSN. With MySQL linked servers I have not been successful.

My last attempt uses this (sanitized) script:

USE [master]
GO

DECLARE @LinkedServer VARCHAR(20)

SET @LinkedServer = 'MYSQL_DSNLESS'

IF EXISTS ( SELECT  * FROM  sys.servers WHERE   [name] = @LinkedServer )
    EXEC sp_dropserver @server = @LinkedServer, @droplogins = 'droplogins'

EXEC master.dbo.sp_addlinkedserver 
     @server = @LinkedServer
    ,@srvproduct = @LinkedServer
    ,@datasrc = @LinkedServer
    ,@provider = N'MSDASQL'
    ,@provstr = N'DRIVER={MySQL ODBC 5.3 ANSI Driver};SERVER=servername;PORT=3306;DATABASE=mysqlschemaname;USER=mysqlusername;PASSWORD=mysqlpassword;OPTION=3'
    ,@catalog = N'ecn'

EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname = @LinkedServer, @useself = N'False', @locallogin = NULL, @rmtuser = N'mysqlusername', @rmtpassword = 'mysqlpassword'

EXEC master.dbo.sp_serveroption @server = @LinkedServer, @optname = N'collation compatible', @optvalue = N'false'
EXEC master.dbo.sp_serveroption @server = @LinkedServer, @optname = N'data access', @optvalue = N'true'
EXEC master.dbo.sp_serveroption @server = @LinkedServer, @optname = N'dist', @optvalue = N'false'
EXEC master.dbo.sp_serveroption @server = @LinkedServer, @optname = N'pub', @optvalue = N'false'
EXEC master.dbo.sp_serveroption @server = @LinkedServer, @optname = N'rpc', @optvalue = N'false'
EXEC master.dbo.sp_serveroption @server = @LinkedServer, @optname = N'rpc out', @optvalue = N'false'
EXEC master.dbo.sp_serveroption @server = @LinkedServer, @optname = N'sub', @optvalue = N'false'
EXEC master.dbo.sp_serveroption @server = @LinkedServer, @optname = N'connect timeout', @optvalue = N'0'
EXEC master.dbo.sp_serveroption @server = @LinkedServer, @optname = N'collation name', @optvalue = NULL
EXEC master.dbo.sp_serveroption @server = @LinkedServer, @optname = N'lazy schema validation', @optvalue = N'false'
EXEC master.dbo.sp_serveroption @server = @LinkedServer, @optname = N'query timeout', @optvalue = N'0'
EXEC master.dbo.sp_serveroption @server = @LinkedServer, @optname = N'use remote collation', @optvalue = N'true'
EXEC master.dbo.sp_serveroption @server = @LinkedServer, @optname = N'remote proc transaction promotion', @optvalue = N'true'
GO

This script runs and creates the linked server object but when I attempt to run a query like this:

SELECT * FROM OPENQUERY(MYSQL_DSNLESS, 'SELECT * FROM mysqlschemaname.table01')

I get this error:

OLE DB provider "MSDASQL" for linked server "MYSQL_DSNLESS" returned message "[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified".
Msg 7303, Level 16, State 1, Line 1
Cannot initialize the data source object of OLE DB provider "MSDASQL" for linked server "MYSQL_DSNLESS".

Is this technique even valid and if so what changes are required to make it work?

Upvotes: 1

Views: 1389

Answers (1)

Zalakain
Zalakain

Reputation: 606

You can define a Linked Server in SQLServer without a DSN for sure. In my case, I've found dealing with DSN was a waste of time (the issue being the character pages used here and there; how can we be in XXI sicle in and this being still unresolved by vendors!!??) so just used the "Provider String" instead.

So, the bare minimum information needed is: - Provider: I've used "Microsoft OLE DB Provider for ODBC Drivers" - Product name: "MySQL" if that's the DB you want to link to - Provider string: here you state the Driver to use, Server and Port to connect to (Port may not be required if using defaults, not 100% sure), Username and password (it will be clear text I'm afraid, so better use a dedicated credential for this)

And that's all. You can specify something else like Database to connect to, or other "Option" values (check MySQL's ODBC Connector info for this).

Thus, in Matthew Macfarland's case above it would be something like this:

EXEC master.dbo.sp_addlinkedserver @server = @LinkedServer ,@srvproduct = N'MySQL' ,@provider = N'MSDASQL' ,@provstr = N'DRIVER={MySQL ODBC 5.3 ANSI Driver};SERVER=servername;PORT=3306;DATABASE=mysqlschemaname;USER=mysqlusername;PASSWORD=mysqlpassword;OPTION=3'

Upvotes: 2

Related Questions