Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock

I have made a virtual environment and installed following:

sudo apt-get install libmysqlclient-dev
sudo pip install MySQL-python

and my setting is :

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.mysql', 
    'NAME': 'DB_NAME',
    'USER': 'DB_USER',
    'PASSWORD': 'DB_PASSWORD',
    'HOST': 'localhost', 
    'PORT': '3306',

}

}

and when I do manage.py syncdb it gives the error saying 2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")

I dont know whats wrong in here... I searched for it... Mostly I found if I use virtualenvironment then the above setting is enough... Whats wrong in here ???

like image 726
gamer Avatar asked Dec 21 '14 13:12

gamer


2 Answers

Install mysql server

sudo apt-get install mysql-server

then create a database for your purposes:

mysql -u root -p --execute "create database DB_NAME; grant all on DB_NAME.* to DB_USER@localhost identified by 'DB_PASSWORD';"

then you should be fine.

I should point out that the confusion of mysql searching for a socket, even if specifying a port, is because mysql defaults to a socket when specifying localhost as an address, if you want to use tcp/ip then you should put an address like 127.0.0.1 there.

like image 185
DRC Avatar answered Sep 30 '22 05:09

DRC


Your HOST should change to '127.0.0.1', you must start service, especially if you are using xamp or lamp.

    DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'DB_NAME',
    'USER': 'DB_USER',
    'PASSWORD': 'DB_PASSWORD',
    'HOST': '127.0.0.1',
    'PORT': '3306',

} #Your HOST should change to '127.0.0.1', you must start service, especially if you are using xamp or lamp
like image 40
Nathyrix Avatar answered Sep 30 '22 06:09

Nathyrix