Can't connect to MySQL server on 'localhost:3306' (10061) During handling of the above exception in python

Viewed 190

i wrote a program to check if program can make connection to MySQL server and if cant connect to server print a error message and this is the program that i wrote

import mysql.connector

is_connected = False

def check_connection():
global is_connected
try:
    db = mysql.connector.connect(
        host="localhost",
        user="root",
        passwd=""
    )

    if db.is_connected():
        print("Connected to MySQL database")
        is_connected = True
except:
    print("Unable to connect to MySQL database check if mysql server is running on defaul port which is 3306")
return is_connected

the whole objective here is print a error message when MySQL server is not running so i stop the server and run the file it works fine when in its own file but when it import to another file and call the check_cconnection method its give this error

Traceback (most recent call last):
File "C:\Users\thevi\AppData\Local\Programs\Python\Python310\lib\site-packages\mysql\connector\connection_cext.py", line 236, in _open_connection
    self._cmysql.connect(**cnx_kwargs)
_mysql_connector.MySQLInterfaceError: Can't connect to MySQL server on 'localhost:3306' (10061)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "t:\Projects\Python_Projects\assignment\anatha.py", line 2, in <module>
    from create_db import createDb
  File "t:\Projects\Python_Projects\assignment\create_db.py", line 3, in <module>
    db = mysql.connector.connect(
  File "C:\Users\thevi\AppData\Local\Programs\Python\Python310\lib\site-packages\mysql\connector\__init__.py", line 272, in connect
    return CMySQLConnection(*args, **kwargs)
  File "C:\Users\thevi\AppData\Local\Programs\Python\Python310\lib\site-packages\mysql\connector\connection_cext.py", line 85, in __init__
    self.connect(**kwargs)
  File "C:\Users\thevi\AppData\Local\Programs\Python\Python310\lib\site-packages\mysql\connector\abstracts.py", line 1028, in connect
    self._open_connection()
  File "C:\Users\thevi\AppData\Local\Programs\Python\Python310\lib\site-packages\mysql\connector\connection_cext.py", line 241, in _open_connection
    raise errors.get_mysql_exception(msg=exc.msg, errno=exc.errno,
mysql.connector.errors.DatabaseError: 2003 (HY000): Can't connect to MySQL server on 'localhost:3306' (10061)

how to ger rid of this error

0 Answers
Related