3

我正在使用mysql.connectorPython。以下是我的连接参数。如何设置超时或增加默认超时?

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,
            }
4

4 回答 4

4

根据官方文档中的MySQL Connector/Python :: 6 Connector/Python Connection Arguments :

要为连接设置超时值,请使用connection_timeout.

于 2013-01-05T01:46:29.543 回答
2

如果您mysql.connector.connect用于连接 DB,您可以简单地使用connection_timeout参数在外部定义超时,确保给定的值将是 inseconds而不是毫秒。

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
)
于 2020-09-30T21:56:27.553 回答
1

我在这里找到了一些东西:http: //dev.mysql.com/doc/refman/5.5/en/connector-python-connectargs.html 我认为您正在寻找参数 connection_timeout (connect_timeout*)

于 2013-01-05T01:49:12.607 回答
0

这对我有用:

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,
    connect_timeout=1000
)

如果你使用 MySQLdb:

conn = MySQLdb.connect(
    host=config['host'], 
    port=config['port'], 
    user=config['user'], 
    passwd=config['passwd'],
    db=config['db'], 
    charset=config['charset'], 
    use_unicode=True,
    connect_timeout=1000
)
于 2017-09-23T09:39:37.830 回答