Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Connector for Python

I am using mysql.connector for Python. Below are my connection parameters. How can I set a timeout or increase the default timeout?

class Config(object):
    """Configure me so examples work

    Use me like this:

        mysql.connector.Connect(**Config.dbinfo())
    """

    HOST = dbhost
    DATABASE = dbdatabase
    USER = dbusername
    PASSWORD = dbpassword
    PORT = 3306

    CHARSET = 'utf8'
    UNICODE = True
    WARNINGS = True

    @classmethod
    def dbinfo(cls):
        return {
            'host': cls.HOST,
            'port': cls.PORT,
            'database': cls.DATABASE,
            'user': cls.USER,
            'password': cls.PASSWORD,
            'charset': cls.CHARSET,
            'use_unicode': cls.UNICODE,
            'get_warnings': cls.WARNINGS,
            }
like image 831
Tampa Avatar asked Aug 11 '26 01:08

Tampa


2 Answers

According to MySQL Connector/Python :: 6 Connector/Python Connection Arguments in the official documentation:

To set a timeout value for connections, use connection_timeout.

like image 124
Johnsyweb Avatar answered Aug 13 '26 14:08

Johnsyweb


If you are using mysql.connector.connect to connect with DB you can simply use connection_timeout parameter to define a timeout externally make sure value given will be in seconds instead of milliseconds.

conn = mysql.connector.connect(
    pool_name=pool['name'], 
    pool_size=pool['size'],
    host=config['host'], 
    port=config['port'], 
    user=config['user'],
    passwd=config['passwd'],
    db=config['db'],
    charset=config['charset'], 
    use_unicode=True,
    connection_timeout=10
)
like image 40
officialrahulmandal Avatar answered Aug 13 '26 14:08

officialrahulmandal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!