Main Content

MySQLODBC forWindows

This tutorial shows how to set up a data source and connect to a MySQL®database using the Database Explorer app or the command line. The tutorial uses a MySQL ODBC 5.3 Driver to connect to the MySQL database.

Step 1. Verify the driver installation.

The ODBC driver is typically preinstalled on your computer. For details about the driver installation or troubleshooting the installation, contact your database administrator or refer to your database documentation on ODBC drivers. For information about the Microsoft®ODBC Data Source Administrator, seeDriver Installation.

Note:

Database Toolbox™ no longer supports connecting to a database using a 32-bit driver. Use the 64-bit version of MySQL. If you have issues working with the ODBC driver, use the JDBC driver instead. For details, seeMySQL JDBC for Windows. For details about working with the 64-bit version of Windows®, seeUsing Previous MATLAB Releases.

Step 2. Set up the data source using the Database Explorer app.

Database Explorer应用访问微软ODBC Data Source Administrator automatically when you configure an ODBC data source. Alternatively, you can access the Microsoft ODBC Data Source Administrator using theconfigureODBCDataSourcefunction.

  1. Open the Database Explorer app by clicking theAppstab on the MATLAB®Toolstrip. Then, on the right of theAppssection, click theShow more画廊箭头打开应用程序。下Database Connectivity and Reporting, clickDatabase Explorer. Alternatively, enterdatabaseExplorerat the command line.

  2. In theData Sourcesection, selectConfigure Data Source>Configure ODBC data source.

    Configure Data Source selection with the selected Configure ODBC data source

    在ODBCData Source Administrator dialog box, you define the ODBC data source.

    Tip

    When setting up an ODBC data source, you can specify a user data source name (DSN) or a system DSN. A user DSN is specific to the person logged into a machine. Only this person sees the data sources that are defined on the user DSN tab. A system DSN is not specific to the person logged into a machine. Any person who logs into the machine can see the data sources that are defined on the system DSN tab. Your ability to set up a user DSN or system DSN depends on the database and ODBC driver you are using. For details, contact the database administrator or refer to the ODBC driver documentation.

  3. Click theSystem DSNtab, and then clickAdd.

    ODBC Data Source Administrator (64-bit) dialog box with the selected System DSN tab

    The Create New Data Source dialog box opens and displays a list of installed ODBC drivers.

  4. Select the ODBC driverMySQL ODBC 5.2a Driver.

    Note

    The name of the ODBC driver can vary.

    ClickFinish.

  5. In the MySQL Connector/ODBC Data Source Configuration dialog box, fill out the boxes.

    MySQL Connector/ODBC Data Source Configuration dialog box

    • In theData Source Namebox, enter an appropriate name for the data source, such asMySQL. You use this name to establish a connection to your database.

    • In theDescriptionbox, enter a description for this data source, such asMySQL database.

    • In theTCP/IP Serverbox, enter the name of your database server. Consult your database administrator for the name of your database server.

    • In thePortbox, enter the port number. The default port number is3306.

    • In theUserbox, enter your user name.

    • In thePasswordbox, enter your password.

    • In theDatabasebox, enter the name of your database.

  6. ClickTestto test the connection to your database. If your computer successfully connects to the database, the Test Result dialog box opens and displays a message indicating the connection is successful.

  7. ClickOKin the MySQL Connector/ODBC Data Source Configuration dialog box. The ODBC Data Source Administrator dialog box shows the ODBC data sourceMySQL.

    After you complete the data source setup, connect to the MySQL database using the Database Explorer app or the command line with the native ODBC connection.

Step 3. Connect using the Database Explorer app or the command line.

Connect toMySQLUsing Database Explorer App

  1. On theDatabase Explorertab, in theConnectionssection, clickConnectand select the data source for the connection.

  2. In the connection dialog box, enter a user name and password, or leave these boxes blank if your database does not require them. ClickConnect.

    The Catalog and Schema dialog box opens.

  3. In theCataloglist, select the catalog. ClickOK.

    The app connects to the database and displays its tables in theData Browserpane. A data source tab appears to the right of the pane. The title of the data source tab is the data source name that you defined during the setup. The data source tab contains emptySQL QueryandData Previewpanes.

  4. Select tables in theData Browserpane to query the database.

  5. Close the data source tab to close the SQL query. In theConnectionssection, close the database connection by clickingClose Connection.

    Note

    If multiple connections are open, close the database connection of your choice by selecting the corresponding data source from theClose Connectionlist.

Connect toMySQLUsing ODBC Driver and Command Line

  1. Connect to the database with the ODBC data source name. For example, this code assumes that you are connecting to a data source namedMySQLwith the user nameusernameand the passwordpwd.

    conn = database('MySQL','username','pwd');
  2. Close the database connection.

    close(conn)

See Also

Apps

Functions

Related Topics