2012-11-07 101 views
3

我已經使用MySQL建立了一個基本的事務數據庫。使用python/MySQLdb的,這工作得很好:MySQLdb無法解析本地主機

connection = MySQLdb.connect (host = "127.0.0.1", port = 3306, user = "root", passwd = "password", db = "test") 

但這並不:

connection = MySQLdb.connect (host = "localhost", port = 3306, user = "root", passwd = "password", db = "test") 

使用後會生成以下錯誤:

Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "path\to\virtualenv\lib\site-packages\MySQLdb\__init__.py", line 81, in Connect 
    return Connection(*args, **kwargs) 
    File "path\to\virtualenv\lib\site-packages\MySQLdb\connections.py", line 187, in __init__ 
    super(Connection, self).__init__(*args, **kwargs2) 
_mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' (10061)") 

我不得不承認,我」有點困惑,爲什麼MySQLdb無法解析localhost。 C:\ Windows \ System32 \ drivers \ etc \ hosts文件未被修改。使用常規的Windows命令提示符,ping localhosttelnet localhost 3306都可以正常工作。

環境:

  • 視窗7(64位)
  • 的Python 2.7.2(32位)
  • MySQLdb的1.2.3
  • 的MySQL 5.5.28(64位)
+0

http://stackoverflow.com/questions/4662364/cant-connect-to-localhost-using-pythons-mysqldb –

+0

@RaunakAgarwal他的問題的可能的複製來解決了通過恢復他的主機文件解決。但是,我的修改沒有改變! – RedsChineseFood

+0

你的主機文件沒有被修改,但它是否包含像127.0.0.1 localhost這樣的行取消註釋該行爲我解決了這個問題。 – fastmultiplication

回答

1

來自MySQLdb docs,關於使用localhost

This creates a connection to the MySQL server running on the local machine via a UNIX socket. UNIX sockets and named pipes don't work over a network, so if you specify a host other than localhost, TCP will be used, and you can specify an odd port if you need to.

您的服務器可能偵聽TCP套接字。

+1

我無法看到這是如何回答我的問題。我錯過了什麼嗎? – RedsChineseFood