OperationalError: (2002, “Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)”)

对着背影说爱祢 提交于 2019-11-27 13:56:15

问题


when connecting to mysql database in Django ,I get the error.

  1. I'm sure mysql server is running.

  2. /var/run/mysqld/mysqld.sock doesn't exist.

  3. When I run $ find / -name *.sock -type s, I only get /tmp/mysql.sock and some other irrelevant output.

  4. I added socket = /tmp/mysql.sock to /etc/my.cnf. And then restared mysql, exited django shell, and connected to mysql database. I still got the same error.

I searched a lot, but I still don't know how to do.

Any help is greate. Thanks in advance.

Well, I just tried some ways. And it works. I did as follows.

  1. Add socket = /tmp/mysql.sock .Restart the mysql server.
  2. ln -s /tmp/mysql.sock /var/lib/mysqld/mysqld.sock

I met an another problem today. I can't login to mysql. I'm newbie to mysql. So I guess mysql server and client use the same socket to communicate. I add socket = /var/mysqld/mysqld.sock to [mysqld] [client] block in my.cnf and it wokrs.


回答1:


Use "127.0.0.1", instead of "localhost"

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



回答2:


in flask, you may use that

app=Flask(__name__)

app.config["MYSQL_HOST"]="127.0.0.1
app.config["MYSQL_USER"]="root"...



来源:https://stackoverflow.com/questions/18150858/operationalerror-2002-cant-connect-to-local-mysql-server-through-socket-v

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!