0

My Python script hangs when trying to connect my Flask app to a MySQL database using mysql-connector-python. The MySQL server is running because it runs perfectly in VSCode, but I can't connect via the command line.

What I’ve Tried:

Verified MySQL server is running. Installed mysql-connector-python (v9.2.0). Added connect_timeout.

I even set a timeout on the connection attempt so why does the script hang, and how can I fix it?

Here’s the test for the connection:

print("Starting database connection test...")

import mysql.connector
import os
from dotenv import load_dotenv

load_dotenv(r"C:\Users\Skip\Documents\personal-projects-github\hairsalon-flask-web-app\.env")

print("Environment variables loaded.")

db_config = {
    "host": os.getenv("DB_HOST", "localhost"), 
    "user": os.getenv("DB_USER", "root"),       
    "password": os.getenv("DB_PASSWORD", ""),  
    "database": os.getenv("DB_NAME", "salon"), 
    "port": int(os.getenv("DB_PORT", "3306")), 
    "auth_plugin": "mysql_native_password",
}

print("Attempting to connect to the database...")
print("Database configuration:", db_config)

try:
    print("Connecting to MySQL...")
    conn = mysql.connector.connect(**db_config)
    print("Connected to MySQL database!")
    conn.close()
except Exception as e:
    print("Error connecting to MySQL:", e)

Edit: Here's a sample output

C:\Users\Skip\Documents\personal-projects-github\hairsalon-flask-web-app>python tests\sql-connector_test.py
Starting database connection test...
Environment variables loaded.
Attempting to connect to the database...
Database configuration: {'host': 'localhost', 'user': 'root', 'password': '####', 'database': 'salon', 'port': 3306, 'auth_plugin': 'mysql_native_password'}
Connecting to MySQL...

C:\Users\Skip\Documents\personal-projects-github\hairsalon-flask-web-app>
5
  • Your command-line environment only has access to environment variables that are explicitly set within that session or globally in your system. Print the values of the environment variables used in db_config immediately before the connection attempt to see if they exist and are correct. Commented Mar 16 at 4:45
  • "...but I can't connect via the command line." What's the error message? Commented Mar 16 at 5:26
  • @Manmadeofmeat that's the thing, there was no error message. It ran everything in the try block but stopped here: conn = mysql.connector.connect(**db_config, connect_timeout=5) and that left me with the hanging print statement: print("Connecting to MySQL...") Commented Mar 16 at 9:49
  • @Lewis ohh I see. I also did try that and it was able to fetch the correct variables and values. I added many print statements as well it identify where it kind of gave up and it was at this line: conn = mysql.connector.connect(**db_config, connect_timeout=5) Commented Mar 16 at 9:50
  • Are you able to connect from shell (mysql -u root -p salon) ? Commented Mar 16 at 16:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.