Python May 10, 2026

How to Connect Python to a Remote MySQL Database

Python is one of the most popular languages for data analysis and web development. Connecting it to a remote MySQL database like FreeDB is efficient and secure using the official connector.

1. Install the Connector

The most reliable way to connect is using the official mysql-connector-python package:

pip install mysql-connector-python

2. Establish a Connection

Use the following code structure to connect and query your FreeDB database:

import mysql.connector

# Configuration using your FreeDB credentials
config = {
  'user': 'freedb_your_username',
  'password': 'your_password',
  'host': 'mysql.freedb.tech',
  'database': 'freedb_your_database_name',
  'raise_on_warnings': True
}

try:
    conn = mysql.connector.connect(**config)
    print("Successfully connected to FreeDB!")
    
    cursor = conn.cursor()
    cursor.execute("SELECT DATABASE();")
    record = cursor.fetchone()
    print("You are connected to database:", record)

except mysql.connector.Error as err:
    print(f"Error: {err}")

finally:
    if 'conn' in locals() and conn.is_connected():
        cursor.close()
        conn.close()
                

3. Troubleshooting

If you cannot connect, ensure that your local network allows outgoing connections on port 3306. FreeDB allows connections from any IP, so you do not need to whitelist your address on our dashboard.