By:Douglas Correa || Related Tips:More >Linked Servers
ProblemThere 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 alinked server from SQL Server to MySQL. In this tip we walk through how this can be done for MySQL from SQL Server.
SolutionIn this tip, we will show how SQL Server can access MySQL data via alinked 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:
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 ConfigurationsThe 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