无法使用 Python 的 MySQLdb 连接到本地主机
当我运行以下脚本时:
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 模块为其指定正确的位置。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我设法通过将主机设置为“127.0.0.1”而不是“localhost”来解决此问题。
I've managed to solve this by setting my host to "127.0.0.1" rather than "localhost".
如果您的 mysql 套接字未放置在 /tmp/mysql.sock 中,您可以通过
编辑指定它:对于 macosx 上的 mamp,mysql 套接字路径应该类似于
/Applications/MAMP/tmp/mysql/mysql.sock< /代码>
if your mysql socket is not placed in /tmp/mysql.sock, you can specify it with
edit: for mamp on macosx the mysql socket path should be something like
/Applications/MAMP/tmp/mysql/mysql.sock
您的 mysql 安装可能将套接字放在其他地方。您可以在 mysql conf 文件中配置它。
您可能想查看类似的帖子:在 leopard 上安装 mysql:“无法通过套接字连接到本地 MySQL 服务器”
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"