How do I connect Microsoft SQL Server to MySQL?

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.

Installing and licensing the ODBC-JDBC Gateway

  1. Download the Windows ODBC-JDBC Gateway.
  2. Install and license the ODBC-JDBC Gateway on the Windows machine where SQL Server, Java, and the MySQL JDBC driver are installed.

    For installation instructions, refer to the ODBC-JDBC Gateway documentation.

Configuring an ODBC data source

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.

Creating a ODBC-JDBC Gateway ODBC data source for MySQL

  1. Choose the System DSN tab, and then choose Add.
  2. In the Create New Data Source dialog box, choose ODBC-JDBC Gateway, and then choose Finish.
  3. Complete the ODBC-JDBC Gateway DSN Setup dialog box:
    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
  4. Use the Test button to verify that you can successfully connect to MySQL.

You can now use the ODBC-JDBC Gateway Data Source to connect SQL Server to MySQL.

Example: Retrieve MySQL data by using a linked server

  1. In Microsoft SQL Server Management Studio, connect to the SQL Server instance you want to create the linked server against.

    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.

  2. Right-click Server Objects > Linked Servers > Providers > MSDASQL. From the pop-up menu, choose Properties.
  3. Ensure the following provider options are checked:
    • Nested queries
    • Level zero only
    • Allow inprocess
    • Supports 'Like' Operator

    The other provider options should be unchecked.

  4. Right-click Server Objects. From the pop-up menu choose New > Linked Server.
  5. In the Linked server box, enter MYSQL.
  6. From the Provider list, choose Microsoft OLE DB Provider for ODBC Drivers.
  7. In the Data source box, type the name of your ODBC-JDBC Gateway ODBC data source (MYSQL_DSN if you're following the example), and then choose OK.

    SQL Server verifies the linked server by testing the connection.

    • If you get the error "Specified driver could not be loaded due to system error 126: The specified module could not be found", choose Yes when prompted whether to keep the linked server. You need to restart your SQL Server instance before you can use the linked server. If SQL Server was already running when you installed the ODBC-JDBC Gateway, it will not have the latest version of the System 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.
    • If you made a mistake when specifying the ODBC-JDBC Gateway data source, you get the error "Data source name not found and no default driver specified. If you get this error, choose No when prompted whether to keep the linked server and edit the value in the Data source box.
  8. You can query your MySQL data either by using a:
    • Four part table name in a distributed query.

      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
      
    • Pass-through query in an 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')
      

Example: Integrate MySQL with SQL Server by using SQL Server Integration Services (SSIS)

These instructions assume that you have Microsoft Visual Studio and SQL Server Data Tools for Visual Studio installed.

  1. Create a .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"
  2. In Visual Studio, create a new Integration Services Project.
  3. Drag a Data Flow Task from the Toolbox to the Control Flow tab.
  4. Choose the Data Flow tab.
  5. Drag a Flat File Source from the Toolbox to the Data Flow tab, and then press Return.

    Flat File Source is under the Other Sources list.

  6. In the Flat File Source Editor, choose New.
  7. In the Flat File Connection Manager Editor dialog box, browse for your .csv file.
  8. In the text qualifier box, enter ".
  9. In the Advanced options, change the DataType of the last_update column to database timestamp [DT_DBTIMESTAMP]. Choose OK.
  10. In the Error Output section, change the truncation action for description to Ignore failure. Choose OK.
  11. Drag an ODBC Destination from the Toolbox to the Data Flow tab, and then press Return.

    ODBC Destination is in the Other Destinations list.

  12. Select the Flat File Source. Drag the blue arrow over to the ODBC Destination.
  13. Select the ODBC Destination, and then press Return.
  14. In the ODBC Destination dialog box, choose New.
  15. In the Configure ODBC Connection Manager dialog box, choose New.
  16. In the Connection Manager dialog box, choose your ODBC-JDBC Gateway ODBC data source, and then use the OK button to return to the ODBC Source dialog box.
  17. In the Name of the table or the view list, enter film. Choose Mappings and then choose OK.
  18. Choose the Start button to insert the records from film.csv file into MySQL.