Note Use the MySQL ODBC driver to connect MySQL to SQL Server. The following article refers to an earlier less direct solution.
Use the ODBC-JDBC Gateway to connect Microsoft SQL Server to MySQL and:
MySQL provide several connectors to enable client applications to access MySQL databases. One such connector is the MySQL Connector/J, which is a piece of database middleware known as a JDBC driver. A JDBC driver allows a Java application to access external data. MySQL's JDBC driver allows Java applications to access data stored in MySQL.
SQL Server is not written in Java however and does not provide a way for a linked server to access a database by using JDBC. SQL Server can use an ODBC driver, a different piece of database middleware, to access external data. The ODBC-JDBC Gateway connects an application that uses ODBC to a database that uses JDBC. To the application, the ODBC-JDBC Gateway is an ODBC driver. To the JDBC driver the ODBC-JDBC Gateway is a Java application.
For installation instructions, refer to the ODBC-JDBC Gateway documentation.
Before you can use the ODBC-JDBC Gateway to connect your ODBC application to MySQL, you need to configure a system ODBC data source. An ODBC data source stores the connection details for the target database.
You configure ODBC data sources in ODBC Data Source Administrator, which is included with Windows.
There are two versions of ODBC Data Source Administrator. The version of ODBC Data Source Administrator that you need to run depends on whether you have a 32-bit or a 64-bit version of SQL Server. To find out which version of SQL Server you have, connect to your SQL Server instance, and then run this SQL statement:
select SERVERPROPERTY('edition')
If you have the 64-bit version of SQL Server and want to use a linked server with MySQL, you need to run 64-bit version of ODBC Data Source Administrator. To do this, in the Windows Run dialog box, enter:
%windir%\system32\odbcad32.exe
If you have the 32-bit version of SQL Server or want to use SSIS with MySQL, you need to run the 32-bit version of ODBC Data Source Administrator. To do this, in the Windows Run dialog box, enter:
%windir%\syswow64\odbcad32.exe
Use ODBC Data Source Administrator to create your ODBC-JDBC Gateway data source.
ODBC-JDBC Gateway
, and then choose Finish.Setting | Value |
---|---|
DSN | MySQL_DSN |
User Name | mysqluser |
Password | mysqluserpassword |
Driver Class | com.mysql.jdbc.Driver |
Class Path |
path\mysql-connector-java-version-bin.jar For example: C:\Program Files (x86)\MySQL\MySQL Connector J\mysql-connector-java-5.1.35-bin.jar |
URL |
jdbc:mysql://mysql_machine:mysql_port/mysql_database For example: jdbc:mysql://localhost:3306/test |
You can now use the ODBC-JDBC Gateway Data Source to connect SQL Server to MySQL.
You need to log on with an account that's a member of the SQL Server sysadmin
fixed server role to create a linked server.
The other provider options should be unchecked.
MYSQL
.Microsoft OLE DB Provider for ODBC Drivers
.MYSQL_DSN
if you're following the example), and then choose OK.
SQL Server verifies the linked server by testing the connection.
Path
environment variable. The ODBC-JDBC Gateway Setup program adds entries for the driver to the System Path
. Restarting the instance makes these changes available to SQL Server, allowing it to load the ODBC-JDBC Gateway.A four part table name has the format:
server_name.[database_name].[schema_name].table_name.
For MySQL you need to omit the database name and schema. For example:
SELECT * from MYSQL_JDBC...sales_by_film_category
OPENQUERY
function. For example:
SELECT * FROM OPENQUERY(MYSQL, 'SELECT * FROM sales_by_film_category')
SQL Server sends pass-through queries as uninterpreted query strings to the ODBC-JDBC Gateway. This means that SQL Server does not apply any kind of logic to the query or try to estimate what that query will do.
Note The MySQL TEXT
data type is incompatible with SQL Server. If you attempt to retrieve a TEXT
column, you'll get errors such as "Requested conversion is not supported." or "Restricted data type attribute violation." To use a TEXT
data in SQL Server, you need to convert the data in MySQL to a type that SQL Server does support. For example:
SELECT * FROM OPENQUERY(MYSQL, 'SELECT CAST(description AS char(255)) AS description from film where film_id = 1000')
These instructions assume that you have Microsoft Visual Studio and SQL Server Data Tools for Visual Studio installed.
.csv
file named film.csv
with the following contents:
film_id,title,description,release_year,language_id,original_language_id,rental_duration,rental_rate,length,replacement_cost,rating,special_features,last_update 1001,"Mad Max: Fury Road","Haunted by his turbulent past, Mad Max believes the best way to survive is to wander alone",2015,1,1,3,"4.99",50,"18.99","NC-17","Trailers,Commentaries,Behind the Scenes","2015-05-08 00:00:01" 1002,"Avengers: Age of Ultron","When Tony Stark tries to jumpstart a dormant peacekeeping program, things go awry",2015,1,1,3,"4.99",50,"18.99","NC-17","Trailers,Commentaries,Behind the Scenes","2015-05-08 00:00:01"
Flat File Source is under the Other Sources list.
.csv
file."
.database timestamp [DT_DBTIMESTAMP]
. Choose OK.Ignore failure
. Choose OK.ODBC Destination is in the Other Destinations list.
film
. Choose Mappings and then choose OK.
film.csv
file into MySQL.