【问题标题】:Problems with MySQL in PythonPython中的MySQL问题
【发布时间】:2021-10-14 08:54:43
【问题描述】:

我目前正在构建一个 python 烧瓶网络应用程序。当我运行它时,我收到几个关于丢失与 MySQL 服务器的连接的错误。有人可以帮帮我吗?

mysql.connector.errors.InterfaceError: 2013: Lost connection to MySQL server during query mysql.connector.errors.OperationalError: MySQL Connection not available.

mysql.connector.errors.OperationalError: 2055: Lost connection to MySQL server at 'eu-cdbr-west-01.cleardb.com:3306', system error: 1 [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:2570)

当我将它上传到 Heroku 以及我的本地计算机上时,我得到了这些错误。它有时有效,有时无效,这也很奇怪。

【问题讨论】:

    标签: python mysql mysql-python mysql-connector-python


    【解决方案1】:

    你在使用图书馆吗?我之前使用flask sql的经验是通过pymysql

    这是我上次设置时使用的连接和返回函数,但是这个可能已经过时了,因为新版本的pymysql被添加了。

    def connect_to_db():
        '''this function connects the database to the website'''
        return pymysql.connect(  host = db_host(),
                                 user = db_username,
                                 password = my_secrets.hunter2,
                                 db = 'some_db',
                                 charset = 'utf8mb4',
                                 cursorclass = pymysql.cursors.DictCursor,
                                 )
    def return_db():
        '''Opens a new database connection per request.'''
        if not hasattr(g, 'db'):
            g.db = connect_to_db()
        return g.db
    

    注意这里返回函数需要导入flask g object

    看起来 SSL 也是您遇到的问题之一。更改版本时 SSL 可能会很痛苦,尤其是在 OSX 上。

    【讨论】:

      猜你喜欢
      • 2011-12-16
      • 2017-05-08
      • 2011-09-01
      • 1970-01-01
      • 2018-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多