Python管理.py运行服务器_mysql_exceptions.OperationalError:(2002,“无法通过套接字'/ tmp / mysql.sock'(2)连接到本地MySQL服务器”)

4

我想运行"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.
    }
}

2
你的MySQL服务器正在运行吗? - Blender
你的MySQL服务器是否配置为监听/tmp/mysql.sock - sarnold
@Sarnold 不,我该如何配置它? - user1467736
当settings.py数据库HOST配置为'''localhost'时,杀死mysql进程然后尝试启动开发服务器会产生一个错误,就像它在寻找“/tmp/mysql.sock”一样。这意味着sarnold所建议的,你的mysql正在特定的unix套接字上运行。 - jdi
问题在于我使用的是Python3,而Django目前不兼容。 - user1467736
显示剩余6条评论
2个回答

10

我也遇到了这个问题。你只需要将数据库主机从localhost更改为127.0.0.1就可以解决此问题。


我浪费了整整2个小时试图找到问题。谢天谢地我看到了这个! - nishant_boro

1
为了找到你的mysql套接字以进行配置,请输入以下命令。
mysql_config --socket

获取结果并应用它:
'HOST': '/Applications/MAMP/tmp/mysql/mysql.sock',

尽管我正在使用MAMP,但这应该适用于其他情况。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接