Can't connect to localhost using Python's MySQLdb

独自空忆成欢 提交于 2019-11-29 00:57:48

问题


I'm new to this so probably have the wrong terminology but when I run the following script:

import MySQLdb

conn = MySQLdb.connect (host = 'localhost',
   user = 'erin',
   passwd = 'erin',
          db = 'sec')

I get the error:

  File "/Library/Python/2.6/site-packages/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg/MySQLdb/__init__.py", line 81, in Connect
    return Connection(*args, **kwargs)
  File "/Library/Python/2.6/site-packages/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg/MySQLdb/connections.py", line 187, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)")

There is no file called mysql.sock in that directory but I don't know what a socket is or how to specify the correct location for it using the MySQLdb module.


回答1:


I've managed to solve this by setting my host to "127.0.0.1" rather than "localhost".




回答2:


if your mysql socket is not placed in /tmp/mysql.sock, you can specify it with

conn = MySQLdb.connect (unix_socket = 'path_to_your_socket', host = 'localhost', user = 'erin', passwd = 'erin', db = 'sec')

edit: for mamp on macosx the mysql socket path should be something like /Applications/MAMP/tmp/mysql/mysql.sock




回答3:


Your mysql installation probably put the socket somewhere else. You can configure this in your mysql conf files.

You might want to check out this similar post: Installing mysql on leopard: "Can't connect to local MySQL server through socket"



来源:https://stackoverflow.com/questions/4662364/cant-connect-to-localhost-using-pythons-mysqldb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!