无法使用Python的MySQLdb连接到本地主机 [英] Can't connect to localhost using Python's MySQLdb

查看:119
本文介绍了无法使用Python的MySQLdb连接到本地主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的新手,所以可能使用了错误的术语,但是当我运行以下脚本时:

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')

我得到了错误:

  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)")

在该目录中没有名为mysql.sock的文件,但我不知道什么是套接字或如何使用MySQLdb模块为其指定正确的位置.

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.

推荐答案

我已设法通过将主机设置为"127.0.0.1"而不是"localhost"来解决此问题.

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

这篇关于无法使用Python的MySQLdb连接到本地主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆