Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python manage.py runserver _mysql_exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)")

Tags:

mysql

django

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.
    }
}
like image 256
user1467736 Avatar asked Dec 21 '22 20:12

user1467736


2 Answers

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

like image 51
Afshin Mehrabani Avatar answered May 03 '23 13:05

Afshin Mehrabani


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

like image 33
Jonatas CD Avatar answered May 03 '23 11:05

Jonatas CD