4

Im trying to run "python manage.py runserver", I already installed mysql, django and the appropriate tools to use them together: Mysql-django. When trying to run I got this error:

$ 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)")**

This is the content of my settings.py file

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.
    }
}
Blender
  • 289,723
  • 53
  • 439
  • 496
user1467736
  • 41
  • 1
  • 3
  • 2
    Is your MySQL server running? – Blender Jun 19 '12 at 23:11
  • Is your MySQL server configured to listen on `/tmp/mysql.sock`? – sarnold Jun 19 '12 at 23:17
  • @Sarnold Nope, how can I configured it? – user1467736 Jun 19 '12 at 23:27
  • The socket to use will be in the [MySQL configuration file](http://dev.mysql.com/doc/refman/5.6/en/server-options.html#option_mysqld_socket). – sarnold Jun 19 '12 at 23:33
  • With the settings.py database HOST configured for either `''` or `'localhost'`, killing the mysql process and then trying to start the dev server produces an error like that where it looks for "/tmp/mysql.sock". This would mean what sarnold suggests, that your mysql is specifically running on a different unix socket – jdi Jun 19 '12 at 23:42
  • @Sarnold I opened /usr/local/mysql/bin/mysql_config and founded this `version='5.5.25' socket='/tmp/mysql.sock' ldflags=''` Is this the correct socket? – user1467736 Jun 20 '12 at 00:05
  • Excellent; that either means that your mysqld isn't running, couldn't open the socket, or refused the connection. What's `perror 2` output on your system? – sarnold Jun 20 '12 at 00:25
  • @Sarnold I`m sorry exactly how can I output the perror 2? I tried running `mysql -u root -p` in the console and it worked, I´m able to see my databases etc – user1467736 Jun 20 '12 at 01:00
  • Just type `perror 2` at your shell.. – sarnold Jun 20 '12 at 01:01
  • The problem was that I was using python3, and django is currently no no compatible. – user1467736 Jun 28 '12 at 21:04
  • @user1467736 is this related to the version of Python really? I have this problem but I'm using Python 2.7. – Afshin Mehrabani Oct 04 '12 at 11:28

2 Answers2

10

I had this problem also. You can solve this problem simply by changing the DB host from localhost to 127.0.0.1.

Afshin Mehrabani
  • 33,262
  • 29
  • 136
  • 201
1

To find where is your mysql socket, in order to config it type

mysql_config --socket

get the result and apply it:

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

Even though I'm using MAMP, but this should work in other scenarios

Jonatas CD
  • 878
  • 2
  • 10
  • 19