Вы находитесь на странице: 1из 10

HOWTO: Setup SQL Server Linked Server to MySQL | Idea Excursion

Pgina 1 de 10

Home

About

Idea Excursion
Technology Musings

Entries (RSS)

Comments (RSS)

Categories

Archives
February 2010 January 2010 November 2009 October 2009 September 2009 August 2009 June 2009 May 2009 April 2009 March 2009 February 2009 January 2009 December 2008 November 2008 October 2008 September 2008 August 2008

HOWTO: Setup SQL Server Linked Server to MySQL


IN: SQL SERVER Migrao de SQL Server
Sincronizao de dados SQL Server com suporte de dados heterogneos www.HiTSW.com

.NET Data Analysis Tool


Transform raw data into visible patterns & trends, code free! www.componentone.com/OLAP

Google (6) Gmail (1) Hardware (1) Internet (3) iPhone (6) Linux (11) Mac (3) Microsoft (37) Windows (35) Active Directory (2) SQL Server (24) SSIS (6) VirtualBox (11)

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

Popular Posts

Recent Posts

25
Feb

wide range of other systems. Ive already written about how to connect Oracle and SQL Server. In this post, Im 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 OSs, but were obviously focused on Windows or Window x64, which should correspond to the version of SQL Server installed. After youve downloaded and installed the driver, we have a few things to configure, so lets get started:

Recent Comments Tags


HOWTO: Sync Google Calendar on iPhone OS 3.0 Windows 7 in VirtualBox: Shared Folders workaround HOWTO: Setup SQL Server Linked Server to MySQL Installing Windows 7 Beta 1 in VirtualBox 2.1.0 Connecting to Oracle from SQL Server HOWTO: Synchronize Dropbox and ZumoDrive on Windows All About Linux Swap Part 2: Management Efficiently query the DATE in DATETIME Windows 7 RTM download details All About Linux Swap Part 3: Analysis

2009

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 If youre using Windows Server 2003, bring up a Run dialog box with StartRun or WinKey+R. If youre using Windows Server 2008, use the Start Menu search box directly. In either OS, type in odbcad32 and hit Enter.

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.

http://www.ideaexcursion.com/2009/02/25/howto-setup-sql-server-linked-server-to-m... 19/02/2010

HOWTO: Setup SQL Server Linked Server to MySQL | Idea Excursion

Pgina 2 de 10

Blogroll
/home/nick Code of Doom SQL Skills: Kimberly L. Tripp

Other
From the Ledge Idea Excursion preAmped Taylor Gerring

Create New Data Source Scroll down in the Create New Data Source window and select MySQL ODBC 3.51 Driver and click Finish.
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 United States.

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 Im connecting to. The Server, User, Password, and Database should all be obvious.

Test ODBC Connection After youve 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 Select the Advanced tab and youll be placed on the Flags 1 sub-tab. Check the boxes labeled Allow Big Results and Use Compressed Protocol.

http://www.ideaexcursion.com/2009/02/25/howto-setup-sql-server-linked-server-to-m... 19/02/2010

HOWTO: Setup SQL Server Linked Server to MySQL | Idea Excursion

Pgina 3 de 10

MySQL Connector Advanced Flags 2 Settings Next, switch to the Flags 2 tab and select Dont 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 On the Flags 3 tab, select Force Use Of Forward Only Cursors. When youre 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 Drill down to Server Object Linked Servers Providers, right-click MSDASQL, and select Properties.

Set Provider Options

http://www.ideaexcursion.com/2009/02/25/howto-setup-sql-server-linked-server-to-m... 19/02/2010

HOWTO: Setup SQL Server Linked Server to MySQL | Idea Excursion

Pgina 4 de 10

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 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 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 Im 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 doesnt 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 youve 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 n 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 youve 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.

http://www.ideaexcursion.com/2009/02/25/howto-setup-sql-server-linked-server-to-m... 19/02/2010

HOWTO: Setup SQL Server Linked Server to MySQL | Idea Excursion

Pgina 5 de 10

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 provid 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 youre 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.

MAPILab.com

Ads by Google

Tags: HOWTO , MySQL , SQL Server , SQL Server 2005 , SQL Server 2008

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


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?

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')

Eric Dossey
April 6th, 2009 at 2:29 pm

Thanks, this was very helpfull and worked great. EricD

Greg
May 5th, 2009 at 4:03 pm

hey,

http://www.ideaexcursion.com/2009/02/25/howto-setup-sql-server-linked-server-to-m... 19/02/2010

HOWTO: Setup SQL Server Linked Server to MySQL | Idea Excursion

Pgina 6 de 10

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

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.

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.

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.

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 [...]

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 wont repeat it here. Heres good one. [...]

Arthur Fuller
June 9th, 2009 at 9:35 am

Your article was very timely, as Ive 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? Thats 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.

http://www.ideaexcursion.com/2009/02/25/howto-setup-sql-server-linked-server-to-m... 19/02/2010

HOWTO: Setup SQL Server Linked Server to MySQL | Idea Excursion

Pgina 7 de 10

Thanks.

Simon
June 10th, 2009 at 3:29 pm

Thanks for this post Very useful!

Taylor Gerring
June 10th, 2009 at 3:36 pm

@Arthur Fuller You shouldnt have any issues performing writes back to MySQL.

mkoenings
July 15th, 2009 at 8:20 am

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

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. 100nx!

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.

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

David Saacke
September 10th, 2009 at 1:42 pm

Thanks for the post. Had an issue with ODBC recognition were 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 dont think this is a priv issue on my mysql db but not sure how to get tables to appear.

http://www.ideaexcursion.com/2009/02/25/howto-setup-sql-server-linked-server-to-m... 19/02/2010

HOWTO: Setup SQL Server Linked Server to MySQL | Idea Excursion

Pgina 8 de 10

Any advice appreciated

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.

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?

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 Youre 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 cant get INSERT OPENQUERY() to work with a remote function. I tried and couldnt figure out if this is even possible due to the syntax.

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?

Troy
December 8th, 2009 at 6:22 am

Hi there, Ive 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. Im trying to connect my SQL server 2005 to a hosted MYSQL Dbase. Not sure where to start? any help would be great. thanks

Taylor Gerring
December 8th, 2009 at 1:55 pm

@Troy It seems like it cant even find the MySQL server. Ensure that traditional connections work before trying something un-

http://www.ideaexcursion.com/2009/02/25/howto-setup-sql-server-linked-server-to-m... 19/02/2010

HOWTO: Setup SQL Server Linked Server to MySQL | Idea Excursion

Pgina 9 de 10

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.

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.

Ben
January 4th, 2010 at 9:55 pm

I keep running into a funny issue with my Linked Server setup. If I have the Dont 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 cant 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?

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 shouldnt matter in most cases.

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 nonexistent dates such as 0000-00-000, 1999-00-00 or 2010-0100, all of which SQL Server will choke on. Also, T-SQL datetime limits it to dates after year 1753, though this shouldnt matter in most cases.

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?

http://www.ideaexcursion.com/2009/02/25/howto-setup-sql-server-linked-server-to-m... 19/02/2010

HOWTO: Setup SQL Server Linked Server to MySQL | Idea Excursion

Pgina 10 de 10

http://www.ideaexcursion.com/2009/02/25/howto-setup-sql-server-linked-server-to-m... 19/02/2010

Вам также может понравиться