2012-06-19 60 views
3

我试图运行“python manage.py runserver” ,我已经安装了mysql,django和合适的工具来将它们一起使用:Mysql-django。当试图运行我得到这个错误:python manage.py runserver _mysql_exceptions.OperationalError :(2002,“无法通过套接字连接到本地MySQL服务器'/tmp/mysql.sock'(2)”)

$ 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

+0

您的MySQL服务器是否配置为在'/ tmp/mysql.sock'上侦听? – sarnold

+0

@Blender是的,我正在使用XAMP – user1467736

回答

6

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

+1

只有这一个适合我! – milesma

1

要找到哪里是你的mysql插座,以配置其键入

mysql_config --socket 

得到的结果,并将其应用于:

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

即使我使用的是甲基苯丙胺,但这应该在其他情况下工作

相关问题