简体   繁体   中英

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 ???

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM