HOWTO: Setup SQL Server Linked Server to MySQL

In: SQL Server Written by: Taylor Gerring

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

use the User DSN tab. System DSN Select the System DSN tab to configure a data source for the entire system. bring up a Run dialog box with Start Run or WinKey+R. Create New Data Source Scroll down in the Create New Data Source window and select MySQL ODBC 3.If you re using Windows Server 2003. If you only want to create the DSN for a specific user (such as your service account). In either OS. If you re using Windows Server 2008. type in odbcad32 and hit Enter.51 Driver and click Finish . In either scenario. use the Start Menu search box directly. select the Add button. .

but I like to specify additional options to optimize the connection between the servers. I have run into Out of Memory errors that require restarting the service. Password. These settings are the bare minimum required to connect MySQL and SQL Server via a linked server. and Database should all be obvious. Without these.51 Configure Data Source application. User. clicking the Configure button will bring up the Connector/ODBC 3. Select a Data Source Name I typically name it after the application or database I m connecting to. Test ODBC Connection After you ve entered all the required parameters. . The Server. This is where you can specify all the connection settings for connecting SQL Server to MySQL. click the Test button to ensure a connection can be made to the MySQL server.MySQL Connector Login Settings Once added.

MySQL Connector Advanced Flags 2 Settings Next. in my experience. This can actually be a performance penalty if you perform the same query multiple times to the same linked server. this option is perfectly suited.MySQL Connector Advanced Flags 1 Settings Select the Advanced tab and you ll be placed on the Flags 1 sub-tab. switch to the Flags 2 tab and select Don t Cache Result (forward only cursors) . However. Check the boxes labeled Allow Big Results and Use Compressed Protocol . is to pull data into a single server. . in which case. the reason to connect SQL Server to MySQL.

Linked Servers Providers.MySQL Connector Advanced Flags 3 Settings On the Flags 3 tab. Provider Properties Drill down to Server Object Properties . I am not aware of any way to change these settings on a per-connection basis. When you re done setting all these options. select Force Use Of Forward Only Cursors . select the Ok button. Configure Linked Server Provider Adjusting the Linked Server Provider is simple. and select . right-click MSDASQL. you are adjusting it for all connections using that provider. but it comes with a caveat: When adjusting a provider.

When done. click OK . the last step in our process is to create the actual MySQL Linked Server. Ensure the following four options are checked: y y y y Nested queries Level zero only Allow inprocess Supports Like Operator All other options should be unchecked. . Create Linked Server to MySQL Finally.Set Provider Options The Provider Options for Microsoft OLE DB Provider for ODBC Drivers dialog box will open allowing you to configure several options.

Once there. Lastly.Create a New Linked Server You should already have Linked Servers expanded in the Object Explorer tree. right-click Linked Servers and select New Linked Server New linked Server Settings The New Linked Server dialog box will open. this last step is very simple. I used MYSQLAPP. I like to name it after the product or database I m connecting to. In my example. find it in Server Objects Linked Server. As with the Data Source Name. Ensure that the Other data source option is selected and choose Microsoft OLE DB Provider for ODBC Drivers from the Provider dropdown. The Product name doesn t matter so much as the Data source must match what you provided in the MySQL Connector/ODBC configuration. Because we specified all our connection settings in the ODBC Data Source Administrator. Press OK when complete. . specify the Product name and Data source. Name the linked server. If not.