cursor() 引发错误.OperationalError(“MySQL 连接不可用.") OperationalError:MySQL 连接不可用 [英] cursor() raise errors.OperationalError("MySQL Connection not available.") OperationalError: MySQL Connection not available

查看:169
本文介绍了cursor() 引发错误.OperationalError(“MySQL 连接不可用.") OperationalError:MySQL 连接不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import requests
import time
import csv
import ast
import sys
import mysql.connector

config = {
'user': 'root',
'password': 'password',
'host': '127.0.0.1',
'port': '3306',
'database': 'dbname',
'raise_on_warnings': True,}

cnx = mysql.connector.connect(config)    
cursor = cnx.cursor()

跑步给予:

Traceback (most recent call last):
  File "/home/ubuntu/scrapers/xrp2.py", line 17, in <module>
    cursor = cnx.cursor()
  File "/usr/local/lib/python2.7/dist-packages/mysql/connector/connection.py", line 1383, in cursor
    raise errors.OperationalError("MySQL Connection not available.")
OperationalError: MySQL Connection not available.

有谁知道如何解决这个问题?其他论坛也有类似的错误,并通过没有打开太多游标来解决问题,但这是对 cursor() 的第一次调用,所以我不确定为什么它不可用.我是否需要从 Ubuntu 终端关闭 MySQL?

Does anyone know how to fix this? Other forums have had similar errors and fixed the problem by not having too many cursors open, but this is the first call to cursor(), so I'm not sure why it's unavailable. Do I need to close MySQL from the Ubuntu terminal?

我的配置文件通过 Sequel Pro 的 SSH 连接正常.

My config file works fine connecting via Sequel Pro's SSH.

已解决:将配置放入 .connect(statement) 而不是字典.

SOLVED: Put the configuration into the .connect(statement) instead of as a dictionary.

import requests
import mysql.connector

cnx = mysql.connector.connect(user ='root', password= 'p', host = '127.0.0.1',port='3306', database='coindb')

cursor = cnx.cursor()

推荐答案

如果您的连接已经关闭,则会发生此错误.在Try-Except-Else 块中,如果Except 没有捕获到错误,那么Else 总是会被执行.

This error will happen if your connection is already closed. In a Try-Except-Else block, it turns out Else is always executed if there is no error caught by the Except.

因此,此代码立即关闭了我的连接:

Therefore, this code was closing my connection immediately:

def mysql_get_mydb():
    '''Takes no args, and returns a connection to MYDB via MYSQL.'''

    creds = fixed_data.MYSQL_ENDPOINTS

    try:
        cnx = connector.connect(user='MYDB',
                              password='open_sesame',
                              host=creds['prod']['MYDB'][0],
                                port=3306,
                              database='MYDB')
    except connector.Error as err:
        if err.errno == connector.errorcode.ER_ACCESS_DENIED_ERROR:
            print("Something is wrong with your user name or password")
        elif err.errno == errorcode.ER_BAD_DV_ERROR:
            print("Database does not exist")
        else:
            print(err)
    # the else will happen if there was no error!
    else:
        cnx.close()

    return cnx

当我尝试执行 z = mysql_get_mydb()y = z.cursor() 时,y = z.cursor()<引发错误/代码>.这是您列出的确切错误.您还可以通过打开一个连接,关闭它,然后尝试在其上定义一个游标来测试这一点.希望此评论对某人有所帮助.这里的修复是最后一个 else 应该包含 return cnx(并且应该删除 cnx.close())

When I tried doing z = mysql_get_mydb() and y = z.cursor() an error is raised by y = z.cursor(). This is the exact error you've listed. You can also test this by opening a connection, closing it, then trying to define a cursor on it. Hopefully, this comment helps someone. The fix here is that the last else should contain return cnx (and the cnx.close() should be removed)

这篇关于cursor() 引发错误.OperationalError(“MySQL 连接不可用.") OperationalError:MySQL 连接不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆