HOWTO: Setup SQL Server Linked Server to MySQL

In: SQL Server


25 Feb 2009

Despite being completely proprietary, one of the nice connectivity features offered in SQL Server is the ability to query other servers through a Linked Server. Essentially, a linked server is a method of directly querying another RDBMS; this often happens through the use of an ODBC driver installed on the server. Fortunately, many popular databases provide this ODBC driver, giving SQL Server the ability to connect to a wide range of other systems. I’ve already written about how to connect Oracle and SQL Server. In this post, I’m going to go through the steps necessary to connect SQL Server and MySQL.

The first step is to fetch an appropriate MySQL Connector/ODBC 5.1 download. Drivers are available for a variety of OS’s, but we’re obviously focused on Windows or Window x64, which should correspond to the version of SQL Server installed. After you’ve downloaded and installed the driver, we have a few things to configure, so let’s get started:

Configure a MySQL DSN

The first step is to configure a MySQL data source by running the ODBC Data Source Administrator. This step is technically entirely optional, but allows a simpler configuration in the SQL Server Linked Server settings. Instead of composing a complicated MySQL connection string, we can use a simple GUI application.

Run odbcad32

Run odbcad32

If you’re using Windows Server 2003, bring up a Run dialog box with Start→Run or WinKey+R. If you’re using Windows Server 2008, use the Start Menu search box directly. In either OS, type in “odbcad32″ and hit Enter.

System DSN

System DSN

Select the System DSN tab to configure a data source for the entire system. If you only want to create the DSN for a specific user (such as your service account), use the User DSN tab. In either scenario, select the “Add…” button.

Create New Data Source

Create New Data Source

Scroll down in the Create New Data Source window and select “MySQL ODBC 3.51 Driver” and click “Finish”.

MySQL Connector Login Settings

MySQL Connector Login Settings

Once added, clicking the “Configure…” button will bring up the Connector/ODBC 3.51 Configure Data Source application. This is where you can specify all the connection settings for connecting SQL Server to MySQL. Select a Data Source Name – I typically name it after the application or database I’m connecting to. The Server, User, Password, and Database should all be obvious.

Test ODBC Connection

Test ODBC Connection

After you’ve entered all the required parameters, click the “Test” button to ensure a connection can be made to the MySQL server.

These settings are the bare minimum required to connect MySQL and SQL Server via a linked server, but I like to specify additional options to optimize the connection between the servers. Without these, I have run into “Out of Memory” errors that require restarting the service.

MySQL Connector Advanced Flags 1 Settings

MySQL Connector Advanced Flags 1 Settings

Select the Advanced tab and you’ll be placed on the “Flags 1″ sub-tab. Check the boxes labeled “Allow Big Results” and “Use Compressed Protocol”.

MySQL Connector Advanced Flags 2 Settings

MySQL Connector Advanced Flags 2 Settings

Next, switch to the “Flags 2″ tab and select “Don’t Cache Result (forward only cursors)”. This can actually be a performance penalty if you perform the same query multiple times to the same linked server. However, in my experience, the reason to connect SQL Server to MySQL, is to pull data into a single server, in which case, this option is perfectly suited.

MySQL Connector Advanced Flags 3 Settings

MySQL Connector Advanced Flags 3 Settings

On the “Flags 3″ tab, select “Force Use Of Forward Only Cursors”. When you’re done setting all these options, select the “Ok” button.

Configure Linked Server Provider

Adjusting the Linked Server Provider is simple, but it comes with a caveat: When adjusting a provider, you are adjusting it for all connections using that provider. I am not aware of any way to change these settings on a per-connection basis.

Provider Properties

Provider Properties

Drill down to Server Object → Linked Servers → Providers, right-click MSDASQL, and select “Properties”.

Set Provider Options

Set Provider Options

The Provider Options for Microsoft OLE DB Provider for ODBC Drivers dialog box will open allowing you to configure several options. Ensure the following four options are checked:

  • Nested queries
  • Level zero only
  • Allow inprocess
  • Supports ‘Like’ Operator

All other options should be unchecked. When done, click “OK”.

Create Linked Server to MySQL

Finally, the last step in our process is to create the actual MySQL Linked Server.

Create a New Linked Server

Create a New Linked Server

You should already have Linked Servers expanded in the Object Explorer tree. If not, find it in Server Objects → Linked Server. Once there, right-click Linked Servers and select “New Linked Server…”

New linked Server Settings

New linked Server Settings

The New Linked Server dialog box will open. Because we specified all our connection settings in the ODBC Data Source Administrator, this last step is very simple. Name the linked server. As with the Data Source Name, I like to name it after the product or database I’m connecting to. In my example, I used MYSQLAPP. Ensure that the “Other data source” option is selected and choose “Microsoft OLE DB Provider for ODBC Drivers” from the Provider dropdown. Lastly, specify the Product name and Data source. The Product name doesn’t matter so much as the Data source must match what you provided in the MySQL Connector/ODBC configuration. Press “OK” when complete.

Testing the SQL Server to MySQL connection

If everything has been set correctly, you should be able to execute a query directly again the MySQL database from SQL Server Management Studio. For example:

SELECT TOP 10 TABLE_NAME FROM MYSQLAPP...tables WHERE TABLE_TYPE != 'MEMORY'

If you’ve done everything correctly, you should get back a result set. There are several error message you might receive:

OLE DB provider "MSDASQL" for linked server "MYSQLAPP" 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 "MYSQLAPP".

The message indicates that the Data source name you’ve specified for the linked server does not match that of the Data Source Name specified in the MySQL Connector.

Msg 7313, Level 16, State 1, Line 1
An invalid schema or catalog was specified for the provider "MSDASQL" for linked server "MySQLApp".

This uninsightful error is a result of not correctly setting the options for the Linked Server Provider.

Msg 7399, Level 16, State 1, Line 1
The OLE DB provider "MSDASQL" for linked server "MySQLApp" reported an error. The provider did not give any information about the error.
Msg 7312, Level 16, State 1, Line 1
Invalid use of schema or catalog for OLE DB provider "MSDASQL" for linked server "MySQLApp". A four-part name was supplied, but the provider does not expose the necessary interfaces to use a catalog or schema.

This “four-part name” error is due to a limitation in the MySQL ODBC driver. You cannot switch catalogs/schemas using dotted notation. Instead, you will have to register another DSN and Linked Server for the different catalogs you want to access. Be sure and follow the three-dot notation noted in the example query.

If, however, you want to access other schemas, you can do so utilizing OPENQUERY. This is also a great way to test your connection if you’re receiving problems. The syntax looks like this:

SELECT * FROM OPENQUERY(MYSQLAPP, 'SELECT * FROM INFORMATION_SCHEMA.TABLES LIMIT 10')

Notice that the actual query syntax in the string must be in the MySQL format (SQL Server does not support the LIMIT keyword). Additionally, you can specify a different schema using SCHEMA.TABLENAME in the query.

Conclusion

Creating a linked server between SQL Server and MySQL is a simple process. The first time requires you to install the software and configure the Linked Server Provider, but all subsequent connections require only a DSN and Linked Server.

  • Share/Bookmark

33 Responses to HOWTO: Setup SQL Server Linked Server to MySQL

Avatar

Mike McLoughlin

April 3rd, 2009 at 2:04 am

We tries this but are getting an error you have not mentioned.

Msg 7399, Level 16, State 1, Line 3
The OLE DB provider “MSDASQL” for linked server “test3″ reported an error. The provider did not give any information about the error.
Msg 7303, Level 16, State 1, Line 3
Cannot initialize the data source object of OLE DB provider “MSDASQL” for linked server “test3″.

Any ideas?

Avatar

Taylor Gerring

April 3rd, 2009 at 11:53 am

What happens if you try to run a query using OPENQUERY? For example,

SELECT * FROM OPENQUERY(test3, 'SELECT * FROM INFORMATION_SCHEMA.TABLES LIMIT 10')

Avatar

Eric Dossey

April 6th, 2009 at 2:29 pm

Thanks,

this was very helpfull and worked great.

EricD

Avatar

Greg

May 5th, 2009 at 4:03 pm

hey,

this has been very helpful, but i keep getting error 7303…
I doubled checked the DNS name I gave my odbc connection. but still no success. do we need the “provider string?

greg

Avatar

Taylor Gerring

May 5th, 2009 at 4:17 pm

@Greg

I would check two things:
1. Test the ODBC connection you setup in the MySQL Connector. Just hit the “Test” button and verify that the connection succeeds.
2. Copy and paste the “Data Source Name” from the connector to the Linked Server Data Source to ensure you have no typos.

If you still have a problem, try expanding the Linked Server and drilling down through the catalog and tables to see if you have access.

Let me know how that goes for you. If all else fails, you can still try and specify an absolute connection string.

Avatar

BigBen

May 6th, 2009 at 3:46 pm

I had to set the absolute connect string to get this to work.
installing the ODBC driver and configuring the system DSN in Data sources was straightforward (and yes, be sure to hit the TEST Connection button when configuring the ODBC source).

Other docs on the MySQL site all pointed to the absolute connect string, and once I entered that, all was well.

Avatar

alegorn

May 11th, 2009 at 10:35 pm

I had the same error. The problem was solved by installing the 5.1 driver and using connection string, without ODBC. Connect through ODBC I have not succeeded, although Access takes the data source.

Avatar

Latest sql server keyword news - HOWTO: Setup SQL Server Linked | Keyword Suggestion Tools

May 26th, 2009 at 8:32 pm

[...] HOWTO: Setup SQL Server Linked Server to MySQL [...]

Avatar

Executing MySQL Stored Procedures from SQL Server | youdidwhatwithtsql.com

June 8th, 2009 at 2:02 pm

[...] Configure your MySQL Server as a Linked Server in SSMS. Plenty of guides on the net about this so I won’t repeat it here. Here’s good one. [...]

Avatar

Arthur Fuller

June 9th, 2009 at 9:35 am

Your article was very timely, as I’ve recently been pondering this approach. However, your focus is on querying the linked server. Can one also write to the MySQL database using this technique? That’s what I want to do — write all the data from a SQL Server database to a corresponding MySQL database. Can this be done? If so, then I could set up an SSIS package to perform the task according to a schedule.

Thanks.

Avatar

Simon

June 10th, 2009 at 3:29 pm

Thanks for this post – Very useful!

Avatar

Taylor Gerring

June 10th, 2009 at 3:36 pm

@Arthur Fuller
You shouldn’t have any issues performing writes back to MySQL.

Avatar

mkoenings

July 15th, 2009 at 8:20 am

works great, even on a 64bits 2003 Server with SQL Server 2008
Thanks a lot!

Avatar

Bogdan

July 20th, 2009 at 9:39 am

Great Post!

Now can really get data from the DSN that I had to create.

I like Linked Server from SQL Server 2005. :D

100nx!

Avatar

Luca Zavarella

August 12th, 2009 at 6:49 am

Do you have issues like “Unable to begin a distributed transaction” using ON INSERT triggers? If not, how did you solved this problem?

Thank you in advantage.

Avatar

S Mummert

September 3rd, 2009 at 10:10 am

Dangling Connections – has anyone seem the number of connections increase from the server that is linking to the mySQL? We have limited the app server connection pool to 30 connections (cold fusion) but the number keeps growing. It seems like the connection to mySQL, managed by MDAC is not flushing and closing the connections correctly hence the number of connections keeps increasing with every call.

Any thoughts? We are in 64 bit SQL 2008 with 64 bit 2003 OS. My SQL is 5.1

Avatar

David Saacke

September 10th, 2009 at 1:42 pm

Thanks for the post. Had an issue with ODBC recognition…we’re MS SQL Server 2008, 64-bit but I had to configure the 32-bit ODBC (was not recognizing 64-bit).

Issue I have now – Linked server succeeds but I cannot see any of the tables (literally, from Mgmt Studio when I try to branch out the Table listing, none appear). When I try to query, object does not exist. I don’t think this is a priv issue on my mysql db – but not sure how to get tables to appear.

Any advice appreciated

Avatar

sensei

November 2nd, 2009 at 11:56 am

This method works, but it sucks because it can only return a max of 2 rows. this is a broken mechanism. not the method, but mysql.

its a good db, but it needs a lot more work to make it to the big time.

Avatar

Keith

November 6th, 2009 at 7:35 am

Thanks for this how-to.
Maybe you can help me with an issue I am facing.
I have established a link between MS SQL 2000 and MySQL. I have successfully queried the MySQL database from MS SQL Analyzer.

Now I would like to store passwords on the MySQL table using the MySQL “AES_ENCRYPT()” function. (using MS SQL Analyzer) Can this be done? and if so how?

Avatar

Taylor Gerring

November 6th, 2009 at 9:47 am

@Keith
You have a few different options:

1. Implement an AES Encrypt function on SQL Server and pass it through already encrypted. This is a bit harder since You’re on SQL Server 2000.
2. Pass the plaintext to a stored procedure on MySQL and have that proc encrypt it locally.
3. See if you can’t get INSERT OPENQUERY() to work with a remote function. I tried and couldn’t figure out if this is even possible due to the syntax.

Avatar

Keith

November 6th, 2009 at 10:03 am

>2. Pass the plaintext to a stored procedure on MySQL and have that proc encrypt it locally.

Any thoughts on how to pull this off?

Avatar

Troy

December 8th, 2009 at 6:22 am

Hi there,

I’ve tried to setup the DSN connection but when I try a test I get this msg – connection failed: [HY000][MYSQL][ODBC 5.1 Driver]Unknown MySQL server host.
I’m trying to connect my SQL server 2005 to a hosted MYSQL Dbase.
Not sure where to start?
any help would be great.
thanks

Avatar

Taylor Gerring

December 8th, 2009 at 1:55 pm

@Troy

It seems like it can’t even find the MySQL server. Ensure that traditional connections work before trying something un-interactive as Linked Servers. MySQL Query browser would be a good place to start.

My guess is that outside connections to the hosted MySQL instance are blocked by default.

Avatar

Dustin

January 4th, 2010 at 12:30 pm

Hello,

Great post. Got anything on random column errors when running select statements against the linked server? Such as..

Msg 7342, Level 16, State 1, Line 1
An unexpected NULL value was returned for column “[MSDASQL].TIMESTAMP_COL” from OLE DB provider “MSDASQL” for linked server “DB_LINK”. This column cannot be NULL.

OR

Msg 7347, Level 16, State 1, Line 1
OLE DB provider ‘MSDASQL’ for linked server ‘DB_LINK’ returned data that does not match expected data length for column ‘[KBOX_DB_LINK]…[TABLENAME].COL1′. The (maximum) expected data length is 14, while the returned data length is 8.

Thanks ahead of time.

Avatar

Ben

January 4th, 2010 at 9:55 pm

I keep running into a funny issue with my Linked Server setup. If I have the “Don’t cache results” and “Force forward only cursors” options checked, any time I run an insert I get:
“OLE DB provider “MSDASQL” for linked server “test” returned message “[MySQL][ODBC 3.51 Driver][mysqld-5.0.77-log]Commands out of sync; you can’t run this command now”.”

I can run the inserts without these options checked but after some time the MySQL server returns an “Out of Memory” error (which I imagine is do to it caching results). Anyone have some insights into this?

Avatar

Ben

January 5th, 2010 at 8:30 am

@Dustin, in my experience is usually do to an invalid date in the MySQL DB. MySQL allows for some none existant dates such as 0000-00-000, 1999-00-00 or 2010-01-00, all of which SQL Server will choke on. Also, T-SQL datetime limits it to dates after year 1753, though this shouldn’t matter in most cases.

Avatar

Ben

January 5th, 2010 at 8:33 am

@Dustin, in my experience that error usually has to do with an invalid date in the MySQL DB. MySQL allows for some non-existent dates such as 0000-00-000, 1999-00-00 or 2010-01-00, all of which SQL Server will choke on. Also, T-SQL datetime limits it to dates after year 1753, though this shouldn’t matter in most cases.

Avatar

John

January 13th, 2010 at 10:08 am

Great article, but I seem to have an issue with the MSDASQL provider does not show up in SQL Server Management Studio… Any thoughts?

Avatar

John

January 13th, 2010 at 10:13 am

Nevermind – I found the issue. I needed to install the following hotfix from Microsoft:
http://www.microsoft.com/downloads/details.aspx?FamilyID=000364db-5e8b-44a8-b9be-ca44d18b059b&displaylang=en#filelist

Avatar

Edward

January 15th, 2010 at 3:51 am

I’ve defined MySQL as a linked server and this is working fine. However, I’m having problems with diacritical charaters. For example the name André is displayed as André. MySQL is in utf8. Using the MySQL Query Browser gives the same result. However, if I first use ’set character_set_results = NULL’, the name is displayed correct. How can I do this in MSSQL using the linked server?

Thanks in advance.

Avatar

DaveC

January 29th, 2010 at 3:46 am

I’m trying to link a MYSQL database (held on a LINUX server) as a linked server through SQL Server 2005.

I have installed all the relevant software – ODBC MYQL drivers (latest version) and have set up an ODBC DNS which seems to connect with no problems. I have also set up the linked server which enables me to see the MYSQL database and expand to the tables, however i can’t see any tables, the folder is empty.

This seems to me like its a priovelege issue within MYSQL, howevere all the priveleges seem okay and look like they are setup to see SQL server 2005 user.

Can anyone shed any ligtht on this?

Could it be something to do with it being on a LINUX server?

Thanks in advance.

Avatar

ChrisC

February 5th, 2010 at 7:11 am

Does anyone have information on linking SQL2005 with a SQLite sever?

Avatar

ben

February 23rd, 2010 at 5:42 pm

I used this process and it worked great – however the only problem that I had was when I would run a query through MS SQL it would crash. This was due to the 32-bit overflow from MS SQL. To solve it follow this process in “Flag 3″ – Checked the option “Limit Column Size to 32-bit range”

Hope this helps anyone that is having problems. Thanks again for the great article

Comment Form

  • Kurt Hitchen: A friend has let me "see" there calendar in Google Calendar, but because their calendar is set to pr [...]
  • bugoy8: https://www.google.com/calendar/dav/nba_13_%4cos+%41ngeles+%4cakers#sports@group.v.calendar.google.c [...]
  • Speaker: Wasn't able to understand how to access the shared folder in the quest OS from this tutorial. Hmmm [...]
  • Elena Kuznetsova: I have killed an hour to figure out how to sync Google contacts' birthday and I have this done! U [...]
  • Andy: I have been trying to figure this out forever.....Thank you ever so much!! [...]


This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 United States.