Learn more about SQL Server tools

mssqltips logo
 

Tutorials          DBA          Dev          BI          Career          Categories          Webcasts          Whitepapers          Today's Tip          Join

Tutorials      DBA      Dev      BI      Categories      Webcasts

DBA    Dev    BI    Categories

 

Access MySQL data from SQL Server via a Linked Server


By:   |   Read Comments (1)   |   Related Tips: More > Linked Servers

ALERT: Did you know 66% of DBAs say their workload is increasing! - Click here to learn more.


Problem

There are often times when you need to access data from MySQL from within SQL Server.  You could export and import the data, but another option is to create a linked server from SQL Server to MySQL. In this tip we walk through how this can be done for MySQL from SQL Server.

Solution

In this tip, we will show how SQL Server can access MySQL data via a linked server.  You can get a copy of the latest version of Connector/ODBC binaries and source from: http://dev.mysql.com/downloads/connector/odbc/.

Before installing the Connector/ODBC driver on Windows:

  • Make sure your Microsoft Data Access Components (MDAC) are up to date.
  • Make sure you have the Microsoft Visual C++ 2013 Redistributable Package on your system.

The steps to complete the installation are very simple, just follow the installation steps in the tool.

MySQL Connector/ODBC 5.3 - Setup Wizard

Once the driver has been installed you will see the driver in the ODBC Data Source Administrator.

Once the driver has been installed you will see the driver in the ODBC Data Source Administrator.

To connect from SQL Server we will create a connection via a Linked Server, before that we need to understand the connection string we will use.

For example:

Driver={MySQL ODBC 5.3 ANSI Driver};DATABASE=database_name;OPTION=134217728;PWD=user_password;UID=user_identification;SERVER=server_name

The table below has the parameters that can be used for the connection string.

Parameter Default Value Comment
user ODBC The user name used to connect to MySQL.
uid ODBC Synonymous withuser. Added in 3.51.16.
server localhost The host name of the MySQL server.
database   The default database.
option 0 Options that specify how Connector/ODBC works.
port 3306 The TCP/IP port to use if server is not localhost.
initstmt   Initial statement. A statement to execute when connecting to MySQL. In version 3.51 the parameter is called stmt. The driver supports the initial statement being executed only at the time of the initial connection.
password   The password for the user account on server.
pwd   Synonymous with password. Added in 3.51.16.
socket   The Unix socket file or Windows named pipe to connect to if server is localhost.
sslca   The path to a file with a list of trust SSL CAs. Added in 3.51.16.
sslcapath   The path to a directory that contains trusted SSL CA certificates in PEM format. Added in 3.51.16.
sslcert   The name of the SSL certificate file to use for establishing a secure connection. Added in 3.51.16.
sslcipher   A list of permissible ciphers to use for SSL encryption. The cipher list has the same format as the openssl ciphers command. Added in 3.51.16.
sslkey   The name of the SSL key file to use for establishing a secure connection. Added in 3.51.16.
rsakey   The full-path name of the PEM file that contains the RSA public key for using the SHA256 authentication plugin of MySQL. Added in 5.3.4.
sslverify   If set to 1, the SSL certificate will be verified when used with the MySQL connection. If not set, then the default behavior is to ignore SSL certificate verification.
charset   The character set to use for the connection. Added in 3.51.17.
readtimeout   The timeout in seconds for attempts to read from the server. Each attempt uses this timeout value and there are retries if necessary, so the total effective timeout value is three times the option value. You can set the value so that a lost connection can be detected earlier than the TCP/IP Close_Wait_Timeout value of 10 minutes. This option works only for TCP/IP connections, and only for Windows prior to MySQL 5.1.12. Corresponds to the MYSQL_OPT_READ_TIMEOUT option of the MySQL Client Library. Added in 3.51.27.
writetimeout   The timeout in seconds for attempts to write to the server. Each attempt uses this timeout value and there are net_retry_count retries if necessary, so the total effective timeout value is net_retry_counttimes the option value. This option works only for TCP/IP connections, and only for Windows prior to MySQL 5.1.12. Corresponds to the MYSQL_OPT_WRITE_TIMEOUToption of the MySQL Client Library. Added in 3.51.27.
interactive   If set to 1, the CLIENT_INTERACTIVEconnection option of mysql_real_connectis enabled.
prefetch 0 When set to a non-zero value N, causes all queries in the connection to return N rows at a time rather than the entire result set. Useful for queries against very large tables where it is not practical to retrieve the whole result set at once. You can scroll through the result set, N records at a time. This option works only with forward-only cursors. It does not work when the option parameter MULTI_STATEMENTSis set. It can be used in combination with the option parameter NO_CACHE. Its behavior in ADO applications is undefined: the prefetching might or might not occur.
no_ssps 0 In Connector/ODBC 5.2, by default, server-side prepared statements are used. When this option is set to a non-zero value, prepared statements are emulated on the client side, which is the same behavior as in 5.1 and 3.51. Added in 5.2.
can_handle_exp_pwd 0 Indicates that the application can deal with an expired password, which is signalled by an SQL state of 08004 (“Server rejected the connection”) and a native error code ER_MUST_CHANGE_PASSWORD_LOGIN(1862). The connection is “sandboxed”, and can do nothing other than issue a SET PASSWORD statement. To establish a connection in this case, your application must either use the initstmt connection option to set a new password at the start, or issue a SET PASSWORD statement immediately after connecting. Once the expired password is reset, the restrictions on the connection are lifted. See ALTER USER Syntax for details about password expiration for MySQL server accounts. Added in 5.2.4.

Recommended Connector/ODBC Option Values for Different Configurations

The option argument is used to tell Connector/ODBC that the client isn't 100% ODBC compliant.

Configuration Parameter Settings Option Value
Microsoft Access, Visual Basic FOUND_ROWS=1; 2
Microsoft Access (with improved DELETE queries) FOUND_ROWS=1;DYNAMIC_CURSOR=1; 34
Microsoft SQL Server COLUMN_SIZE_S32=1; 134217728
Large tables with too many rows COMPRESSED_PROTO=1; 2048
Sybase PowerBuilder IGNORE_SPACE=1;FLAG_SAFE=1; 135168
Query log generation (Debug mode) LOG_QUERY=1; 524288
Large tables with no-cache results NO_CACHE=1;FORWARD_CURSOR=1; 3145728
Applications that run full-table "SELECT * FROM ... " query, but read only a small number (N) of rows from the result PREFETCH=N Not Applicable

Create a SQL Server Linked Server to MySQL

In SQL Server Management Studio, open Object Explorer, expand Server Objects, right-click Linked Servers, and then click New Linked Server. On the General page do the following:

  1. Linked server - type the name of the MySQL server you want to link to.
  2. Server type - select Other data source
  3. Provider - specify an OLE DB Provider, in this case I selected Microsoft OLE DB Provider for ODBC Drivers
  4. Provider string - enter the connection string we discussed above Driver={MySQL ODBC 5.3 ANSI Driver};DATABASE=database_name;OPTION=134217728;PWD=user_password;UID=user_identification;SERVER=server_name)
Create a SQL Server Linked Server to MySQL

On the Server Options page we need to adjust the RPC and RPC Out settings to True. Think about an RPC (Remote Procedure Call) as being a stored procedure being run remotely from server 1 to linked server 2.

On the Server Options page we need to adjust the RPC and RPC Out settings to True

You can also create a linked server using this T-SQL command:

USE [master]
GO
EXEC master.dbo.sp_addlinkedserver 
@server = N'MYSQL'
,@srvproduct=N'MySQL'
,@provider=N'MSDASQL'
,@provstr=N'Driver={MySQL ODBC 5.3 ANSI Driver};DATABASE=test;OPTION=134217728;[email protected];UID=test;SERVER=srvtest'
GO

EXEC master.dbo.sp_serveroption @server=N'MYSQL', @optname=N'rpc', @optvalue=N'true'
GO
EXEC master.dbo.sp_serveroption @server=N'MYSQL', @optname=N'rpc out', @optvalue=N'true'
GO

Access MySQL data from SQL Server

After the linked server has been created you will be able to see the remote objects in the object explorer.

Access MySQL data from SQL Server

We can also run a query to access the data.

We can also run a query to access the data.

Connector/ODBC Performance

The Connector/ODBC driver has been optimized to provide very fast performance. If you experience problems with the performance of Connector/ODBC or notice a large amount of disk activity for simple queries, there are a number of aspects to check:

  • Ensure that ODBC Tracing is not enabled. With tracing enabled, a lot of information is recorded in the tracing file by the ODBC Manager. You can check, and disable tracing within Windows using the Tracing panel of the ODBC Data Source Administrator.
  • Make sure you are using the standard version of the driver and not the debug version. The debug version includes additional checks and reporting measures.
  • Ensure that the driver trace (option value 4) and query logging (option value 524288) are not enabled.
Next Steps
  • More about option parameters here.
  • Connector/ODBC errors FAQ.
  • Learn about how to export data from MySQL to SQL Server from this tip.


Last Update:


next webcast button


next tip button



About the author
MSSQLTips author Douglas Correa Douglas Correa is a database professional, focused on tuning, high-availability and infrastructure.

View all my tips
Related Resources





Post a comment or let the author know this tip helped.

All comments are reviewed, so stay on subject or we may delete your comment. Note: your email address is not published. Required fields are marked with an asterisk (*).

*Name    *Email    Email me updates 


Signup for our newsletter
 I agree by submitting my data to receive communications, account updates and/or special offers about SQL Server from MSSQLTips and/or its Sponsors. I have read the privacy statement and understand I may unsubscribe at any time.



    



Tuesday, February 28, 2017 - 7:37:07 PM - Michael Back To Top

 To get this to work (in MS SQL server management studio) under the Security page/option set to "Be made using the login's current security context".  I did not add login data in this tab.

 

 


Learn more about SQL Server tools