Why I am unable to create connection with MySQL using python on Window 11 OS

The program terminates automatically without throwing any error or output

The port and connector working fine and details are given below

C:\Users\jsr>pip list
Package                Version
---------------------- -------
mysql-connector-python 9.1.0
pip                    24.3.1

C:\Users\jsr>netstat -an | findstr 3306
  TCP    0.0.0.0:3306           0.0.0.0:0              LISTENING
  TCP    0.0.0.0:33060          0.0.0.0:0              LISTENING
  TCP    [::]:3306              [::]:0                 LISTENING
  TCP    [::]:33060             [::]:0                 LISTENING
import mysql.connector
from mysql.connector import Error

HOST = "localhost"
PORT = 3306
USER = "sud0077"
PASSWORD = "Admin@123"
DATABASE = "QuizzApp"

try:
    mydb = mysql.connector.connect(
        user=USER,
        password=PASSWORD,
        host=HOST,
        port=PORT,
        database=DATABASE
    )
    if mydb.is_connected():
        print("Connection successful")
except Error as e:
    print(f"Error: {e}")
finally:
    try:
        if mydb.is_connected():
            mydb.close()
            print("Connection closed")
    except NameError:
        print("Connection was never established")

Server is also running on port 3306. Id and pass is also correct but still no output terminal just gets closed while connecting and IDLE restarts automatically.

for more details I have shared i video link it will gives you clear picture that I am facing

form my end I tried everny possible scenario to fixe this, but in last NO LUCK slightly_frowning_face :frowning:
Please help to clear this issue, I dont know how this will solve

If mydb is formed without raising an error, but nonetheless mydb.is_Connected() == False, the behaviour will be exactly wht you’re seeing.

Do you have to call a connect method on mydb?

Here comes an interesting part:
After debugging we found out that the program terminates at
mydb = mysql.connector.connect(
user=USER,
password=PASSWORD,
host=HOST,
port=PORT,
database=DATABASE

)

while executing this line whole terminal gets closed. And if running in IDLE it just restarts. No output, no error all blank.

As shown in image if connection was made, then why Python interpreter closed. It should have either thrown error, or remain opened.

This image is from Virtual Box, I tried to check the same in Virtual box same bahaviour is noticed.

1 Like

What happens when you run that code with the database is turned off completely? Or with the wrong password or the wrong user name?

First When Connector were not installed -
Compiler shows Error -
**import mysql.connector **
NotfoundError: No module named ‘mysql’

Second When connector installed and I stopped mySQL services
I got this output which I shared as Picture

Great. The second one with the DB stopped is different to the weird behaviour. Try it again with a password that is definitely wrong.

When incorrect credentials are provided it runs as expected throws exception connection was not made… but with correct info provided terminates…

If you want to verify just install windows 11 in virtualbox and python and then try to establish connection.

Good. That’s ruled out bad credentials, and an obvious connection error then (a less obvious connection error is still possible).

Maybe it is a bug. You’re both very welcome to raise that with the project team. I can’t quite figure out how that should be done, but this link is on the project page: https://bugs.mysql.com/

Networking issues can be particularly tricky, but there must be hundreds if not thousands of people out there, who can connect to MySQL from Python successfully.

It would be much better for you guys to reproduce a way to connect to MySQL that actually works for someone else (than it would for myself or others trying to reproduce something that may or may not be a bug, or that may instead be something you’re doing wrong, or have not done correctly).

There’s nothing wrong with picking the most popular choice, or anything that’s more tried and tested. Especially when running into issues with something else.

With all respect to their devs, I highly recommend you drop mysql.connector entirely, and do what Django recommends for MySQL users, and use this instead: