无法使用 Python 的 MySQLdb 连接到本地主机

发布于 2024-10-11 08:43:44 字数 762 浏览 2 评论 0原文

当我运行以下脚本时:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

末骤雨初歇 2024-10-18 08:43:44

我设法通过将主机设置为“127.0.0.1”而不是“localhost”来解决此问题。

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

眼眸印温柔 2024-10-18 08:43:44

如果您的 mysql 套接字未放置在 /tmp/mysql.sock 中,您可以通过

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

编辑指定它:对于 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

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

dawn曙光 2024-10-18 08:43:44

您的 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"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文