据我所知,我已经毫无问题地安装了 MySQL Connector/Python (v1.2.3) 模块。这是在 CentOS 5.4 上使用 Python 2.7.7(Anaconda 发行版,尽管在 vanilla 安装上也是如此)。我能够导入它并初始化与 MySQL 服务器(v4.1.20)的连接。该服务器通过 LAN 访问,而不是本地访问。该is_connected()
方法断言我已正确连接
>>> import mysql.connector
>>> cnx = mysql.connector.connect(<MySQL database information here>)
>>> cursor = cnx.cursor()
>>> cnx.is_connected()
True
但是,每当我尝试直接通过游标命令(例如cursor.execute("SELECT DATABASE();")
)或间接通过 mysql.connector 方法(例如,get_database()
)执行任何 SQL 查询时,我都会收到相同的错误,如下所示:
>>> cnx.get_database()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/bgrande/software/genesis/anaconda-2.0.1/lib/python2.7/site-packages/mysql/connector/connection.py", line 1200, in get_database
return self._info_query("SELECT DATABASE()")[0]
File "/home/bgrande/software/genesis/anaconda-2.0.1/lib/python2.7/site-packages/mysql/connector/connection.py", line 1434, in _info_query
cursor.execute(query)
File "/home/bgrande/software/genesis/anaconda-2.0.1/lib/python2.7/site-packages/mysql/connector/cursor.py", line 494, in execute
self._handle_result(self._connection.cmd_query(stmt))
File "/home/bgrande/software/genesis/anaconda-2.0.1/lib/python2.7/site-packages/mysql/connector/connection.py", line 683, in cmd_query
statement))
File "/home/bgrande/software/genesis/anaconda-2.0.1/lib/python2.7/site-packages/mysql/connector/connection.py", line 612, in _handle_result
eof = self._handle_eof(self._socket.recv())
File "/home/bgrande/software/genesis/anaconda-2.0.1/lib/python2.7/site-packages/mysql/connector/connection.py", line 554, in _handle_eof
eof = self._protocol.parse_eof(packet)
File "/home/bgrande/software/genesis/anaconda-2.0.1/lib/python2.7/site-packages/mysql/connector/protocol.py", line 252, in parse_eof
raise errors.InterfaceError(err_msg)
mysql.connector.errors.InterfaceError: Failed parsing EOF packet.
在此之后,似乎与 MySQL 服务器的连接丢失了。再次尝试运行get_database()
返回以下错误:
>>> cnx.get_database()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/bgrande/software/genesis/anaconda-2.0.1/lib/python2.7/site-packages/mysql/connector/connection.py", line 1200, in get_database
return self._info_query("SELECT DATABASE()")[0]
File "/home/bgrande/software/genesis/anaconda-2.0.1/lib/python2.7/site-packages/mysql/connector/connection.py", line 1433, in _info_query
cursor = self.cursor(buffered=True)
File "/home/bgrande/software/genesis/anaconda-2.0.1/lib/python2.7/site-packages/mysql/connector/connection.py", line 1328, in cursor
raise errors.OperationalError("MySQL Connection not available.")
mysql.connector.errors.OperationalError: MySQL Connection not available.
该方法证实了这一点is_connected()
。
>>> cnx.is_connected()
False
此外,我应该提到,在这个 LAN 上另一台具有类似设置的计算机上(CentOS 5.5;Python 2.7.6),我已经成功地使用 MySQLdb 模块与数据库进行交互。然而,与 mysql.connector 模块相同的 Python 安装不能(由于相同的错误)。我尝试在此处提到的第一台计算机上安装 MySQLdb,但遇到了问题。我认为 MySQL 连接器/Python 会更容易工作。
恐怕这很难重现,因为它可能取决于我的特定环境。尽管如此,对于我可以采取哪些步骤来缩小问题所在的范围,我将不胜感激。谢谢!