【问题标题】:MySQL Connector for Python用于 Python 的 MySQL 连接器
【发布时间】:2012-12-19 12:10:45
【问题描述】:

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

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

【问题讨论】:

    标签: python mysql mysql-connector-python


    【解决方案1】:

    如果您使用mysql.connector.connect 与数据库连接,您可以简单地使用connection_timeout 参数在外部定义超时,确保给定值以seconds 为单位而不是毫秒。

    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
    )
    

    【讨论】:

      【解决方案2】:

      这对我有用:

      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
      )
      

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          【解决方案4】:

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

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

          【讨论】:

          • Python 文档未指定。 MySQL documentation 指定“秒”,所以我相同,但会测量自己确定。
          猜你喜欢
          • 1970-01-01
          • 2019-06-15
          • 2014-12-10
          • 2018-09-12
          • 2018-06-27
          • 2016-01-16
          • 1970-01-01
          • 1970-01-01
          • 2021-05-01
          相关资源
          最近更新 更多