Handling connection to MySQL database problems in Python

35 views Asked by At

I am trying to write a programm that uses MySQL database and came upon a problem that programm got stuck. I found what the problem was - somone cut the power to computer where MySQL server was located. I tried to use try ... except method to make shure programm doesn't get stuck and outputs that connection to database failed, but programm still seems to be stuck in try block. Any ideas why that it is happening? Also is there a way to use timers, for example, if connection is not successful in 30 sec, programm outputs an error?

import mysql.connector

try:
    db = mysql.connector.connect(
        host = 'dbHost',
        user = 'dbUser',
        password = 'dbPassword',
        database = 'dbDatabase'
    )
except:
    print('connection to database failed')

I have looked on the internet for possible solutions, tried different try ... except ... else ... methods or variations, but so far programm still gets stuck. Even Ctrl + C doesn't work...

0

There are 0 answers