Easysoft Blog

Tue, 29 Nov 2016

Connecting ODBC Applications to MySQL

Installing the MySQL ODBC Driver

  1. Download the MySQL ODBC driver for your Windows platform. (Registration required.)
  2. Install and license the MySQL ODBC driver on the machine where your client application is installed.

    For example, if you want to analyse MySQL data in Excel, install the MySQL ODBC driver on the machine where Excel is installed.

    To do this, execute the file distribution that you downloaded in the previous step, and follow the on screen instructions.

    The install program starts the Easysoft License Manager, because you cannot use the MySQL ODBC Driver until a license is obtained.

    The following types of license are available:

    • Free time-limited trial license which gives you free and unrestricted use of the product for a limited period (usually 14 days).
    • Full license if you have purchased the product. On purchasing the product you are given an authorization code, which you use to obtain a license
  3. In Easysoft License Manager, enter your details.

    You MUST enter the Name, E-Mail Address and Company fields.

    The E-Mail Address MUST be the same as the address used to register and download from the Easysoft web site or you will be unable to obtain trial licenses.

  4. Choose Request License.

    You are asked for a license type.

  5. Do one of the following:
    • For a trial license click Time Limited Trial and then click Next.

      The License Manager asks what software you are licensing. Select Easysoft MySQL ODBC Driver from the drop-down list and then click Next.

    • If you have obtained an authorization code for a purchased license, select Non-expiring License and then click Next.

      The License Manager requests your authorization code.

      Enter the authorization code and then click Next.

  6. The License Manager displays a summary of the information you entered and allows you to choose the method of applying for your license.
  7. Do one of the following:
    • Choose On-line Request if your machine is connected to the internet and can make outgoing connections to port 8884.

      The License Manager then sends a request to the Easysoft license server to activate your license key automatically. This is the quickest method and results in your details being entered immediately into our support database.

    • The remaining three options (Email Request, Print Request and View Request) are all ways to obtain a license if your machine is off-line (i.e. does not have a connection to the internet).

      Each of these methods involves providing Easysoft with information including your machine number (a number unique to your machine) and then waiting to receive your license key.

      Instead of emailing your details to Easysoft, you can enter them directly at the Easysoft web site and your license key will be emailed to you automatically.

      To use this method, click View Request, and then visit:

      In the Licensing page, enter your machine number (and authorization code for purchased license), click Submit and your license key will be emailed to you.

      When you receive the license key, you can activate it either by double-clicking the email attachment or by clicking Enter License on the License Manager main screen and pasting the license key into the dialog box.

    • Choose Finish to exit the License Manager.

      The installation is complete.

Configuring an ODBC Data Source

Before you can use the MySQL ODBC Driver to connect your application to MySQL, you need to configure an ODBC data source. An ODBC data source stores the connection details for the target database (e.g. MySQL) and the ODBC driver that is required to connect to it (e.g. the MySQL ODBC driver).

You configure ODBC data sources in ODBC Administrator, which is included with Windows. How you run ODBC Administrator depends on whether your version of Windows is 32-bit or 64-bit:

Use ODBC Administrator to create a MySQL ODBC Driver data source:

  1. Choose the System DSN tab, and then choose Add.
  2. In the Create New Data Source dialog box, choose Easysoft ODBC-MySQL Driver, and then choose Finish.
  3. Complete these fields Easysoft ODBC-MySQL Driver DSN Setup dialog box:
    SettingValue
    DSNMYSQL
    User NameThe name of your MySQL user.
    PasswordThe password for your MySQL user.
    ServerThe host name or IP address of the machine on which your MySQL server is running.
  4. Use the Test button to verify that you can successfully connect to your MySQL database.

Connecting Your Application to MySQL

Microsoft Access

Microsoft Access uses an ODBC driver to interact with an external data source. ODBC is a data access technology, the Microsoft implementation of which is included with Windows. You can use the MySQL ODBC Driver to connect Microsoft Access to a MySQL Database, enabling you to retrieve and update MySQL data from a Microsoft Access front end.

  1. Open your Microsoft Access database.
  2. Do one of the following:
    • For Microsoft Access 2003 and earlier, on the File Menu, choose Get External Data > Link Tables.

      The Link dialog box is displayed.

    • For Microsoft Access 2007, on the External Data tab, choose More > ODBC Database.

      The Get External Data wizard starts.

    • For Microsoft Access 2010 and later, on the External Data tab, choose ODBC Database.

      The Get External Data wizard starts.

  3. Do one of the following:
    • For Microsoft Access 2003 and earlier, choose ODBC Databases from the Files of type list.
    • For Microsoft Access 2007 and later, choose Link to the data source by creating a linked table, and then choose OK.

    The Select Data Source dialog box is displayed.

  4. Choose your ODBC data source from the Machine Data Source list, and then choose OK.

    The Link Tables dialog box is displayed.

  5. Choose the table that you want to link and then choose OK.
  6. Choose the linked table in Microsoft Access, and then choose RETURN to work with the linked data.

Connecting Excel to MySQL

Follow these steps to return data from MySQL to Microsoft Excel by using Microsoft Query:

  1. Do one of the following:
    • For Excel 2007 and later, on the Data tab, choose Get External Data. In the From Other Sources group, choose From Microsoft Query.
    • For Excel 2003 and earlier, on the Data menu, choose Import External Data, and then choose New Database Query.

    The New Database Query command uses Microsoft Query to import data. Microsoft Query is an optional feature, which by default is not installed. If you do not have Microsoft Query installed, choosing New Database Query will prompt you to install it. To do this, in Control Panel, choose Programs and Features (or Add or Remove Programs). Choose Microsoft Office (if you installed Excel as part of Office) or Microsoft Excel, and then choose Change. Follow the instructions on screen. Select Choose advanced customization of applications during the Setup wizard if this option is present for your version of Office / Excel. Microsoft Query is located under Office Tools.

    When you have installed Microsoft Query, repeat step 1.

  2. In the Choose Data Source dialog box, choose your MySQL ODBC driver data source from the list, and then choose OK.
  3. Choose OK when the message "This data source contains no visible tables." is displayed.
  4. In the Query Wizard, choose Cancel.
  5. Choose Yes when prompted whether to continue editing this query in Microsoft Query.
  6. In the Add Tables dialog box, choose Close.
  7. Choose the SQL button.
  8. In the SQL statement dialog box, type a SQL query. For example:
    select * from employees
    
  9. On the File menu choose, Return data to Excel.
  10. Choose a location for the data on your worksheet when prompted.

Connecting Oracle to MySQL

The following instructions show you how to connect Oracle on Windows to MySQL. For further information about DG4ODBC, refer to our DG4ODBC for Windows tutorial.

  1. Create a DG4ODBC init file. To do this, change to the %ORACLE_HOME%\hs\admin directory. Create a copy of the file initdg4odbc.ora. Name the new file initmysql.ora.

    Note In these instructions, replace %ORACLE_HOME% with the location of your ORACLE HOME directory. For example, C:\oraclexe\app\oracle\product\11.2.0\server.

  2. Ensure these parameters and values are present in your init file:
    HS_FDS_CONNECT_INFO = my_mysql_odbc_dsn

    Replace my_mysql_odbc_dsn with the name of a MySQL ODBC driver data source that connects to the target MySQL server. For example

    HS_FDS_CONNECT_INFO = "32-bit MySQL System"

    If your MySQL server does not require you to supply a user name and a password, include IgnoreAuth=Yes in the HS_FDS_CONNECT_INFO parameter value. For example:

    HS_FDS_CONNECT_INFO = "32-bit MySQL System;IgnoreAuth=Yes"
  3. Add this line to increase Oracle / MySQL compatibility:
    HS_FDS_QUOTE_IDENTIFIER = FALSE
  4. Comment out the line that enables DG4ODBC tracing. For example:
    #HS_FDS_TRACE_LEVEL = <trace_level>
  5. Add an entry to %ORACLE_HOME%\network\admin\listener.ora that creates a SID_NAME for DG4ODBC. For example:
    SID_LIST_LISTENER =
     (SID_LIST =
       (SID_DESC=
         (SID_NAME=mysql)
         (ORACLE_HOME=%ORACLE_HOME%)
         (PROGRAM=dg4odbc)
       )
     )
  6. Add a DG4ODBC entry to %ORACLE_HOME%\network\admin\tnsnames.ora that specifies the SID_NAME created in the previous step. For example:
    MYSQL =
      (DESCRIPTION =
        (ADDRESS = (PROTOCOL = TCP)(HOST = oracle_host)(PORT = 1521))
        (CONNECT_DATA =
          (SID = mysql)
        )
        (HS = OK)
      )

    Replace oracle_host with the host name of your Oracle machine.

  7. Start (or restart) the Oracle Listener:
    cd %ORACLE_HOME%\bin
    lsnrctl stop
    lsnrctl start
  8. Connect to your Oracle database in SQL*Plus.
  9. In SQL*Plus, create a database link for the target MySQL server. For example:
    CREATE PUBLIC DATABASE LINK mysqllink CONNECT TO
    "my_mysql_user" IDENTIFIED by "my_mysql_password" USING 'mysql';

    – Or, if your MySQL server does not require you to supply a user name and a password:

    CREATE PUBLIC DATABASE LINK mysqllink USING 'mysql';
Notes

Connecting SQL Server (Linked Server / SSIS) to MySQL

Linked Server

These steps show you how to use a linked server to work with MySQL data, just as if you were working with data stored in a SQL Server table:

  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 is 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, type "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 MySQL ODBC data source, and then choose OK.
  8. Choose the Security page.
  9. Choose Be made using this security context.
  10. Type your MySQL user name and password in the spaces provided. 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 MySQL ODBC driver, it will not have the latest version of the System PATH environment variable. The MySQL ODBC driver 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 MySQL ODBC driver.
    • If you made a mistake when specifying the MySQL ODBC driver 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.
  11. 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...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 MySQL ODBC driver. 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 will get error 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')
      

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, type ".
  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 MySQL 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, type film. Choose Mappings and then choose OK.
  18. Choose the Start button to insert the records from film.csv file into MySQL.