# Connecting with Python

This guide explains how to establish a connection between a Python application and a MySQL database using the `<span class="s3">mysql-connector-python</span>` package. It walks through the necessary setup, configuration, and execution of a simple SQL query.

## **Variables**

Certain parameters must be provided to establish a successful connection to a MySQL database. Below is a breakdown of each required variable, its purpose, and where to find it. Here’s what each variable represents:

<table border="1" id="bkmrk-variable-description" style="border-collapse: collapse; border-color: rgb(0, 0, 0); width: 100%;"><thead><tr><th style="border-color: rgb(0, 0, 0); width: 10.4864%;">**Variable**

</th><th style="border-color: rgb(0, 0, 0); width: 39.9308%;">**Description**

</th><th style="border-color: rgb(0, 0, 0); width: 49.5828%;">**Purpose**

</th></tr></thead><tbody><tr><td style="border-color: rgb(0, 0, 0); width: 10.4864%;">`USER`

</td><td style="border-color: rgb(0, 0, 0); width: 39.9308%;">MySQL username, from the Elestio service overview page

</td><td style="border-color: rgb(0, 0, 0); width: 49.5828%;">Identifies the database user who has permission to access the MySQL database.

</td></tr><tr><td style="border-color: rgb(0, 0, 0); width: 10.4864%;">`PASSWORD`

</td><td style="border-color: rgb(0, 0, 0); width: 39.9308%;">MySQL password, from the Elestio service overview page

</td><td style="border-color: rgb(0, 0, 0); width: 49.5828%;">The authentication key is required for the specified USER to access the database.

</td></tr><tr><td style="border-color: rgb(0, 0, 0); width: 10.4864%;">`HOST`

</td><td style="border-color: rgb(0, 0, 0); width: 39.9308%;">Hostname for MySQL connection, from the Elestio service overview page

</td><td style="border-color: rgb(0, 0, 0); width: 49.5828%;">The address of the server hosting the MySQL database.

</td></tr><tr><td style="border-color: rgb(0, 0, 0); width: 10.4864%;">`PORT`

</td><td style="border-color: rgb(0, 0, 0); width: 39.9308%;">Port for MySQL connection, from the Elestio service overview page

</td><td style="border-color: rgb(0, 0, 0); width: 49.5828%;">The network port used to connect to MySQL. The default port is 3306.

</td></tr><tr><td style="border-color: rgb(0, 0, 0); width: 10.4864%;">`DATABASE`

</td><td style="border-color: rgb(0, 0, 0); width: 39.9308%;">Database Name for MySQL connection, from the Elestio service overview page

</td><td style="border-color: rgb(0, 0, 0); width: 49.5828%;">The name of the database being accessed. A MySQL instance can contain multiple databases.

</td></tr></tbody></table>

These values can usually be found in the Elestio service overview details as shown in the image below, make sure to take a copy of these details and add it to the code moving ahead.

[![image.png](https://docs.elest.io/uploads/images/gallery/2025-04/scaled-1680-/88Kimage.png)](https://docs.elest.io/uploads/images/gallery/2025-04/88Kimage.png)

## **Prerequisites**

- **Install Python**
    - Check if Python is installed by running: `python --version`
    - If not installed, download it from [python.org](https://www.python.org/downloads/) and install it.

- **Install the `mysql-connector-python` Package**
    - The <span class="s2">mysql-connector-python</span> package enables Python applications to interact with MySQL. Install it using: `pip install mysql-connector-python`

## **Code**

Once all prerequisites are set up, create a new file named `<span class="s2">mysql_connect.py</span>` and add the following code:

```python
import mysql.connector

# Database connection configuration
config = {
    "host": "HOST",
    "user": "USER",
    "password": "PASSWORD",
    "database": "DATABASE",
    "port": PORT
}

try:
    # Establish the connection
    connection = mysql.connector.connect(**config)
    print("Connected to MySQL")

    # Create a cursor and execute a test query
    cursor = connection.cursor()
    cursor.execute("SELECT VERSION()")

    # Fetch and print the result
    version = cursor.fetchone()
    print("MySQL Version:", version[0])

except mysql.connector.Error as err:
    print("Connection failed:", err)

finally:
    if 'cursor' in locals():
        cursor.close()
    if 'connection' in locals() and connection.is_connected():
        connection.close()
        print("Connection closed")
```

To execute the script, open the terminal or command prompt and navigate to the directory where `<span class="s1">mysql_connect.py</span>` is located. Once in the correct directory, run the script with the command:

```
python mysql_connect.py
```

If the connection is successful, the terminal will display output similar to:

```
Connected to MySQL
MySQL Version: 8.0.41
Connection closed
```