【问题标题】:python manage.py runserver _mysql_exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)")python manage.py runserver _mysql_exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)")
【发布时间】:2013-09-11 03:47:19
【问题描述】:

我正在尝试运行“python manage.py runserver”,我已经安装了 mysql、django 和适当的工具来一起使用它们:Mysql-django。尝试运行时出现此错误:

$ python manage.py runserver
Validating models...

Unhandled exception in thread started by <bound method Command.inner_run of <django.contrib.staticfiles.management.commands.runserver.Command object at 0x110251450>>
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/django/core/management/commands/runserver.py", line 91, in inner_run
    self.validate(display_num_errors=True)
  File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 266, in validate
    num_errors = get_validation_errors(s, app)
  File "/Library/Python/2.7/site-packages/django/core/management/validation.py", line 103, in get_validation_errors
    connection.validation.validate_field(e, opts, f)
  File "/Library/Python/2.7/site-packages/django/db/backends/mysql/validation.py", line 14, in validate_field
    db_version = self.connection.get_server_version()
  File "/Library/Python/2.7/site-packages/django/db/backends/mysql/base.py", line 411, in get_server_version
    self.cursor()
  File "/Library/Python/2.7/site-packages/django/db/backends/__init__.py", line 306, in cursor
    cursor = self.make_debug_cursor(self._cursor())
  File "/Library/Python/2.7/site-packages/django/db/backends/mysql/base.py", line 387, in _cursor
    self.connection = Database.connect(**kwargs)
  File "build/bdist.macosx-10.7-intel/egg/MySQLdb/__init__.py", line 81, in Connect
  File "build/bdist.macosx-10.7-intel/egg/MySQLdb/connections.py", line 187, in __init__
_mysql_exceptions.OperationalError: **(2002, "Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)")**

这是我的 settings.py 文件的内容

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'django',                      # Or path to database file if using sqlite3.
        'USER': 'root',                      # Not used with sqlite3.
        'PASSWORD': '',                  # Not used with sqlite3.
        'HOST': 'localhost',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
    }
}

【问题讨论】:

  • 你的 MySQL 服务器在运行吗?
  • 您的 MySQL 服务器是否配置为侦听 /tmp/mysql.sock
  • @Sarnold Nope,我该如何配置它?
  • 要使用的套接字将在MySQL configuration file中。
  • 将 settings.py 数据库 HOST 配置为 '''localhost',杀死 mysql 进程,然后尝试启动开发服务器会产生类似于它查找“/tmp”的错误/mysql.sock”。这意味着 sarnold 建议,您的 mysql 专门在不同的 unix 套接字上运行

标签: mysql django


【解决方案1】:

我也有这个问题。只需将 DB 主机从 localhost 更改为 127.0.0.1 即可解决此问题。

【讨论】:

  • 我确实浪费了 2 个小时试图找出问题所在。感谢上帝,我看到了这个!
【解决方案2】:

要找到你的mysql套接字在哪里,为了配置它输入

mysql_config --socket

得到结果并应用它:

'HOST': '/Applications/MAMP/tmp/mysql/mysql.sock',

虽然我使用的是 MAMP,但这应该适用于其他场景

【讨论】:

    猜你喜欢
    • 2014-10-16
    • 2023-03-20
    • 2020-08-11
    • 2012-07-11
    • 2017-08-01
    • 2019-04-18
    • 2018-05-15
    • 2013-10-06
    • 2017-05-03
    相关资源
    最近更新 更多