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

**URL:** <https://discuss.python.org/t/why-i-am-unable-to-create-connection-with-mysql-using-python-on-window-11-os/76107>\
**Category:** Python Help\
**Tags:** help\
**Created:** [January 4, 2025, 8:19am UTC](https://discuss.python.org/t/why-i-am-unable-to-create-connection-with-mysql-using-python-on-window-11-os/76107 "2025-01-04T08:19:25Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![sud0074](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/sud0074/32/24992_2.png) [@sud0074](https://discuss.python.org/u/sud0074)\
**Post date:** [January 4, 2025, 8:19am UTC](https://discuss.python.org/t/why-i-am-unable-to-create-connection-with-mysql-using-python-on-window-11-os/76107/1 "2025-01-04T08:19:25Z")

</div>

The program terminates automatically **without throwing any error or output**

The **port** and **connector** working fine and details are given below

```python
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

```

```python
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

[![](https://us1.discourse-cdn.com/flex002/uploads/python1/original/3X/6/8/68b8a22b76d53ec3c888b37ff4530f8d34eb8862.jpeg "Unable to create connection with MySQL using python") ](https://www.youtube.com/watch?v=3vFn4vrDJEM)

form my end I tried everny possible scenario to fixe this, but in last NO LUCK slightly\_frowning\_face ☹  
Please help to clear this issue, I dont know how this will solve

---

<div class="post-metadata">

**Author:** ![JamesParrott](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamesparrott/32/10534_2.png) [@JamesParrott](https://discuss.python.org/u/JamesParrott)\
**Post date:** [January 4, 2025, 10:29am UTC](https://discuss.python.org/t/why-i-am-unable-to-create-connection-with-mysql-using-python-on-window-11-os/76107/2 "2025-01-04T10:29:00Z")

</div>

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`?

---

<div class="post-metadata">

**Author:** ![shubhamchauhan8881](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/shubhamchauhan8881/32/24997_2.png) [@shubhamchauhan8881](https://discuss.python.org/u/shubhamchauhan8881)\
**Post date:** [January 4, 2025, 11:13am UTC](https://discuss.python.org/t/why-i-am-unable-to-create-connection-with-mysql-using-python-on-window-11-os/76107/3 "2025-01-04T11:13:04Z")

</div>

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

 ![WhatsApp Image 2025-01-04 at 15.12.55_3f3f207c](https://us1.discourse-cdn.com/flex002/uploads/python1/original/3X/6/1/6196438f9d1244b9d5286517d94230492ce19207.jpeg)

```
)

```

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.

---

<div class="post-metadata">

**Author:** ![JamesParrott](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamesparrott/32/10534_2.png) [@JamesParrott](https://discuss.python.org/u/JamesParrott)\
**Post date:** [January 4, 2025, 7:33pm UTC](https://discuss.python.org/t/why-i-am-unable-to-create-connection-with-mysql-using-python-on-window-11-os/76107/4 "2025-01-04T19:33:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sud0074](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/sud0074/32/24992_2.png) [@sud0074](https://discuss.python.org/u/sud0074)\
**Post date:** [January 5, 2025, 9:14am UTC](https://discuss.python.org/t/why-i-am-unable-to-create-connection-with-mysql-using-python-on-window-11-os/76107/5 "2025-01-05T09:14:21Z")

</div>

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

 ![withoutMySQL](https://us1.discourse-cdn.com/flex002/uploads/python1/original/3X/8/d/8d857a6ebccd9c4c96483fdc9ef566449097d4f3.jpeg)

---

<div class="post-metadata">

**Author:** ![JamesParrott](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamesparrott/32/10534_2.png) [@JamesParrott](https://discuss.python.org/u/JamesParrott)\
**Post date:** [January 5, 2025, 9:27am UTC](https://discuss.python.org/t/why-i-am-unable-to-create-connection-with-mysql-using-python-on-window-11-os/76107/6 "2025-01-05T09:27:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![shubhamchauhan8881](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/shubhamchauhan8881/32/24997_2.png) [@shubhamchauhan8881](https://discuss.python.org/u/shubhamchauhan8881)\
**Post date:** [January 5, 2025, 9:32am UTC](https://discuss.python.org/t/why-i-am-unable-to-create-connection-with-mysql-using-python-on-window-11-os/76107/7 "2025-01-05T09:32:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![JamesParrott](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamesparrott/32/10534_2.png) [@JamesParrott](https://discuss.python.org/u/JamesParrott)\
**Post date:** [January 5, 2025, 10:35am UTC](https://discuss.python.org/t/why-i-am-unable-to-create-connection-with-mysql-using-python-on-window-11-os/76107/8 "2025-01-05T10:35:25Z")

</div>

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/](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](https://docs.djangoproject.com/en/5.1/ref/databases/#mysql-db-api-drivers), and use this instead:

> **[GitHub - PyMySQL/mysqlclient: MySQL/MariaDB connector for Python](https://github.com/PyMySQL/mysqlclient)**
>
> MySQL/MariaDB connector for Python

---

<div class="post-metadata">

**Author:** ![walktalkcontact](https://avatars.discourse-cdn.com/v4/letter/w/48db29/32.png) [@walktalkcontact](https://discuss.python.org/u/walktalkcontact)\
**Post date:** [June 18, 2025, 4:11pm UTC](https://discuss.python.org/t/why-i-am-unable-to-create-connection-with-mysql-using-python-on-window-11-os/76107/9 "2025-06-18T16:11:08Z")

</div>

I tried using “use\_pure=True”. It means pure Python implementation instead of C. By default, it is false.

```python
db = mysql.connector.connect(
    host="localhost",    
    user="myuser",
    password="mypass",
    database="mydb",
    use_pure=True
    )

```

---

<div class="post-metadata">

**Author:** ![ajin](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/ajin/32/30181_2.png) [@ajin](https://discuss.python.org/u/ajin)\
**Post date:** [August 7, 2025, 9:15am UTC](https://discuss.python.org/t/why-i-am-unable-to-create-connection-with-mysql-using-python-on-window-11-os/76107/10 "2025-08-07T09:15:36Z")

</div>

t looks like you’re narrowing it down well. If you’re still getting connection issues even after verifying credentials and network settings, switching the connector as suggested (like using `mysqlclient` instead of `mysql.connector`) is definitely a good move. It’s more stable, widely adopted, and works well with Django and other frameworks. Also, setting `use_pure=True` in `mysql.connector` can sometimes resolve strange bugs since it avoids the C extension — worth a try if you’re sticking with it. But if you’re still facing issues after that, and especially if the database isn’t behaving properly or seems corrupted, tools like **Stellar Repair for MySQL** can help restore damaged tables or recover inaccessible data. Might save you from deeper headaches if it’s not just a code issue.
