MySQL Connector for Python

a 夏天 提交于 2020-01-06 07:29:09

问题


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

回答1:


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

To set a timeout value for connections, use connection_timeout.




回答2:


I found something here: http://dev.mysql.com/doc/refman/5.5/en/connector-python-connectargs.html I think you are looking for the parameter connection_timeout (connect_timeout*)




回答3:


This works for me:

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
)

If you use 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
)


来源:https://stackoverflow.com/questions/14167576/mysql-connector-for-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!