By: Douglas Correa | Updated: 2016-11-21 | Comments (11) | Related: > Linked Servers
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.
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:
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:
- Linked server - type the name of the MySQL server you want to link to.
- Server type - select Other data source
- Provider - specify an OLE DB Provider, in this case I selected Microsoft OLE DB Provider for ODBC Drivers
- 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)
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.
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;PWD=P@ssw0rd;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.
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.
About the author
This author pledges the content of this article is based on professional experience and not AI generated.
View all my tips
Article Last Updated: 2016-11-21