“无法连接到'localhost'(10061)上的MySQL服务器”错误在python中

时间:2013-01-12 02:11:30

标签: python mysql database-connection mysql-python

就在今天,我已经决定要学习python,因为我个人喜欢它的语法和感觉。我是一个非常年轻的开发人员,我真的想学习一门新的编程语言,因为我只精通JavaScript(特别是javascript)和PHP所以我决定Python。 Python似乎非常像javascript execpt,因为它们不使用花括号来定义新块。当然它的功能更强大。

我了解到您可以使用MySQL连接到MySQLdb个数据库。

但是当我使用以下代码时,我遇到了问题。

db = MySQLdb.connect(host="localhost", user="methodjs_postes", passwd="********", db="methodjs_postes");

我收到了这个大错误:

Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    db = MySQLdb.connect(host="localhost", user="methodjs_postes", passwd="********", db="methodjs_postes");
  File "C:\Python27\lib\site-packages\MySQLdb\__init__.py", line 81, in Connect
    return Connection(*args, **kwargs)
  File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 187, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
OperationalError: (2003, "Can't connect to MySQL server on 'localhost' (10061)")

我真正关注的主要部分是Can't connect to MySQL server on 'localhost' (10061)部分。 (但如果你解释其他部分也会很好)

我不明白为什么我会遇到这个问题。 我在localhost中使用了PHP,它运行得很好。我做了import MySQLdb,我认为这不是错误,因为它甚至不会尝试连接。

我正在使用python 2.7,我将使用3.3(已下载)但MySQLdb仅支持最多2.7。

使用select User,Host,Password from mysql.user where User like '%methodjs_postes%';时出现以下错误:

SELECT command denied to user 'methodjs'@'localhost' for table 'user'

2 个答案:

答案 0 :(得分:3)

只需将localhost替换为127.0.0.1

即可

答案 1 :(得分:0)

http://chat.stackoverflow.com/rooms/22618/discussion-between-shawn31313-and-suku

根据聊天,结论是: - “你的mysql主机是远程机器,不允许远程mysql连接”