据我所知,我已经安装了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
另外,我应该提到在这个局域网上的另一台计算机上使用类似的设置(CentOS 5.5; Python 2.7.6),我已成功使用MySQLdb模块与数据库进行交互。然而,与mysql.connector模块完全相同的Python安装不能(由于相同的错误)。我试图在这里提到的第一台计算机上安装MySQLdb,但我遇到了问题。我认为MySQL Connector / Python可以更容易地工作。
我担心这很难重现,因为它可能取决于我的具体环境。尽管如此,我还是希望能够采取措施缩小问题所在的范围。谢谢!