MySQL Python连接器显示错误

时间:2014-11-04 05:13:02

标签: python mysql mysql-python

我刚刚在Windows 7机器上为Python 2.7安装了MySQL Python连接器2.0.2。我试图运行一个示例代码来创建与我的数据库的连接,但它正在抛出一条错误消息。 “解析握手失败;缓冲区中不存在结束字节”。我运行以下代码:

import mysql.connector
from mysql.connector import errorcode

try:
  cnx = mysql.connector.connect(user='scott',
                                database='testt')
except mysql.connector.Error as err:
  if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
    print("Something is wrong with your user name or password")
  elif err.errno == errorcode.ER_BAD_DB_ERROR:
    print("Database does not exists")
  else:
    print(err)
else:
  cnx.close()

我使用的是Python 2.7.3和MySQL服务器版本5.5.8。如果我错过了什么,请告诉我。

3 个答案:

答案 0 :(得分:0)

如果没有异常处理,连接到MySQL数据库的代码将是:

import mysql.connector

cnx = mysql.connector.connect(user='root', password='********',
                              host='127.0.0.1',
                              database='mysql')
print 'Connected'
cnx.close()

enter image description here

现在,您需要修改的内容:

  1. 将用户更改为root
  2. 输入MySQL root的密码
  3. 添加参数主机
  4. 您需要使用此连接器here

答案 1 :(得分:0)

此问题可能特定于MySQL 5.5.8版。尝试升级MySQL版本并重试。

答案 2 :(得分:0)

我遇到了同样的问题,尝试MySQLdb而不是mysql.connector应该解决这个问题。我不需要更改代码,这太棒了。