(2002,“无法通过socket连接到本地MySQL服务器'/var/lib/mysql/mysql.sock'(2)”)

时间:2012-10-24 11:47:07

标签: python mysql django apache

我正在使用apache运行基于django的Web应用程序。我能够从python和linux shell连接到mysql但是,当我运行服务器时,我收到以下错误。我在哪里做错了?

OperationalError at /

(2002, "Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)")

Request Method:     GET
Request URL:    http://dev.ls.co.uk/
Django Version:     1.3
Exception Type:     OperationalError
Exception Value:    

(2002, "Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'   (2)")

Exception Location:     build/bdist.linux-x86_64/egg/MySQLdb/connections.py in __init__, line 187
Python Executable:  /usr/bin/python
Python Version:     2.4.3

2 个答案:

答案 0 :(得分:1)

根据mysql.com上的这个帖子:

  

这里有两件事可能出错:

     
      
  1. 您无权访问目录/var/lib/mysql/whatever.sock,因为mysql是该文件夹的所有者   或
  2.   
  3. /path/whatever.sock不存在。
  4.   

首先,您应该知道用户apache的运行方式:try top命令以找出用户。然后,使用id thatusername命令来确定它所在的组。

然后,您可以阅读article about debugging this kind of problem

答案 1 :(得分:1)

套接字文件不存在。你需要重启mysqld.service 试试这个:

  

     

systemctl restart mysqld.service

您是否以不同模式启动了mysql? 如果是这样,你需要杀死mysql的进程并使用上面的命令。 grep for mysql进程在进程列表中查看进程id并终止进程

  

kill process_id

并发出上一个命令以重新启动服务