> ## Documentation Index
> Fetch the complete documentation index at: https://docs.domino.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Connect to MySQL

This topic describes how to connect to [MySQL](https://www.mysql.com/) from Domino. You must have network connectivity between MySQL and your Domino deployment.

The easiest way to connect to MySQL from Domino is to create a Domino Data Source as described below.

## Create a MySQL tabular Data Source

1. From the navigation pane, click **Data > Data Sources**.

2. Click **Create a Data Source**.

3. In the New Data Source window, from **Select Data Store**, select **MySQL**.

4. Enter the **Host**. Valid values are `<host string>:<port>` or `<host string>`.

   If no port is specified, the default is 3306.

5. Enter the name of the **Database**.

6. Enter the **Data Source Name**.

7. Optional: Enter a **Description** to explain the purpose of the Data Source to others.

8. Click **Next**.

9. Specify the credentials for authenticating to MySQL.

   Basic authentication is supported by default. IAM credential propagation might also be available if your administrator has [enabled it](/cloud/admin/data-administration/external-data/configure-ds-auth#iam-auth).

   <Note>
     IAM-authenticated connections can be used only for executing jobs and workspaces. Other execution types, such as scheduled jobs and Domino endpoints, require basic authentication.
   </Note>

10. Click **Next** (or **Skip for Now** to configure authentication later).

11. Select whether **Everyone** can access this Data Source or just **Specific users or organizations**.

12. Select who can view and use the Data Source in projects.

13. Click **Finish Setup**.

## Alternate way to connect to a MySQL Data Source

<Warning>
  This section describes an alternate method to connect to the MySQL Data Source. Domino does not officially support this method.
</Warning>

1. Domino recommends the [mysql-connector-python](https://dev.mysql.com/doc/connector-python/en/) library to interact with MySQL databases from Python.

2. Use the following Dockerfile instruction to [install psycopg2](https://www.psycopg.org/docs/install.html) in your environment. You must have [pip](https://pypi.org/project/pip/) installed.

   ```dockerfile theme={null}
   USER root
   RUN pip install mysql-connector-python
   USER ubuntu
   ```

3. You must set up the Domino environment variables to store secure information about your MySQL connection.

   * `MYSQL_HOST`

     Hostname where your MySQL service is running. Make sure your MySQL service and network firewall are configured to accept connections from Domino.

   * `MYSQL_USER`

     The MySQL user you want to authenticate as.

   * `MYSQL_PASSWORD`

     The password for the user chosen previously.

     See [Store Project credentials](/cloud/platform-capabilities/core-concepts/projects/configure-projects/store-project-credentials) to learn more about Domino environment variables.

4. See the [mysql-connector-python documentation](https://dev.mysql.com/doc/connector-python/en/) for information about how to use the package. The following is an example to connect to MySQL with `mysql-connector-python` where:

   * You have set up environment variables with the host, user, and password.

   * Your user has access to a database named `db1` in the target MySQL instance.

   * The `db1` database contains a table called `employees`.

     ```python theme={null}
     from mysql.connector import (connection)
     import os

     # fetch values from environment variables and set the target database
     hostname = os.environ['MYSQL_HOST']
     username = os.environ['MYSQL_USER']
     password = os.environ['MYSQL_PASSWORD']
     dbname = 'db1'

     # establish connection to db1 database in your mysql service
     cnx = connection.MySQLConnection(user=username,
                                      password=password,
                                      host=hostname,
                                      database=dbname)

     # create cursor for passing queries to database
     cursor = cnx.cursor()

     # define query
     query = ("SELECT * FROM employees")

     # execute query
     cursor.execute(query)

     # print results
     for row in cursor:
       print(row)

     # close connection
     cnx.close()
     ```

### R and RMySQL

1. To interact with MySQL services from R, Domino recommends the [RMySQL](https://www.rdocumentation.org/packages/RMySQL/versions/0.10.16) library.

2. Use the following Dockerfile instructions to add RMySQL to your environment.

   ```dockerfile theme={null}
   RUN sudo apt-get install -y libmariadb-client-lgpl-dev
   RUN R -e 'install.packages("RMySQL")'
   ```

3. Set the following Domino environment variables to store secure information about your MySQL connection.

   * `MYSQL_HOST`

     Hostname where your MySQL service is running. Make sure your MySQL service and network firewall are configured to accept connections from Domino.

   * `MYSQL_USER`

     The MySQL user you want to authenticate as.

   * `MYSQL_PASSWORD`

     The password for the user chosen previously.

     See [Store Project credentials](/cloud/platform-capabilities/core-concepts/projects/configure-projects/store-project-credentials) to learn more about Domino environment variables.

4. See the [RMySQL documentation](https://www.rdocumentation.org/packages/RMySQL/versions/0.10.16) for information about how to use the package. The following is an example for connecting to MySQL with RMySQL where:

   * You have set up environment variables with the host, user, and password.

   * Your user has access to a database named `db1` in the target MySQL instance.

   * The database contains a table named `employees`.

     ```r theme={null}
     # load the library
     library(RMySQL)

     # fetch values from environment variables and set the target database
     hostname <- Sys.getenv['MYSQL_HOST']
     username <-  Sys.getenv['MYSQL_USER']
     password <- Sys.getenv['MYSQL_PASSWORD']
     database <- 'db1'

     # set up a driver and use it to create a connection to your database
     con <- dbConnect(RMySQL::MySQL(), host = hostname,
      user = username, password = password, dbname = database)

     # run a query and load the response into a dataframe
     df_mysql <- dbGetQuery(con, "SELECT * FROM employees")

     # close your connection when finished
     dbDisconnect(con)
     ```

## Next steps

* After connecting to your Data Source, learn how to [Use Data Sources](/cloud/platform-capabilities/core-concepts/data/data-source-connectors/use-data-sources).

* [Share this Data Source](/cloud/platform-capabilities/core-concepts/data/sharing-and-security/share-data-sources) with your collaborators.
