Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
609 views
in Technique[技术] by (71.8m points)

python - Missing mysql.sock; yielding OperationalError: (2002, "Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)")

Firstly, I swear that I have looked at every single question that references this error. Nearly every solution someone offers is different, and no one seems to understand the systemic reasons for the error. What I, and many people on the web who encounter this common problem, need, is an explanation of what is actually going wrong.

Basically, when I try to run:

python manage.py shell
from django.db import connection
cursor = connection.cursor()

with Django 1.4 and python 2.7.3, I get the following error: OperationalError: (2002, "Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)")

I believe that my settings.py file is correct. And everything I need installed is installed. Here is the settings.py:

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
    'NAME': 'database_name.mwb',                      # Or path to database file if using sqlite3.
    'USER': 'user',                      # Not used with sqlite3.
    'PASSWORD': 'PASS',                  # Not used with sqlite3.
    'HOST': '/tmp/mysql.sock',                      # Set to empty string for localhost. Not used with sqlite3.
    'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
}}

After suffering through weeks of scouring the web for answers, including every one on stackoverflow, I have come to the conclusion that the real issue I am experiencing is that there no mysql.sock file on my computer, even though mysql is installed. So, the real question is how do I get one? The answer may be embarrassingly simple.

When I run:

cd /
sudo find . -name ".sock"

I don't get anything. And yes, I have installed mysql 5.25 on my mac osx lion. It's definitely installed:

sudo launchctl load -w /Library/LaunchDaemons/com.mysql.mysql.plist
com.mysql.mysqld: Already loaded

However, running:

mysql

yields:

-bash: mysql: command not found

After a long time, I found this bug: http://bugs.mysql.com/bug.php?id=4174 .. so what's the solution??

Full disclosure: I'm just a programmer. I really don't know anything about computers. So, I don't know anything about mysql, databases, or sockets. I think this question will require somebody who really understands computers to fix. As, though it seems obvious now, it has taken a long time to formulate this error as being from a missing socket. 99% of people on the web with this error do not seem to be approaching it this way.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As far as I can tell, it is either of the two things:

  1. You MySQL Server is not running, or
  2. It is running, but is not configured to use /tmp/mysql.sock as its socket

As far as 2 is concerned, I believe that is the default setting for a freshly installed MySQL, but it doesn't hurt to check. Try looking at the contents of /etc/my.cnf. Check whether there is a line that looks like this: socket=/path/to/socket - where /path/to/socket is, as it says the file path of the socket. If there is such an entry and the file path is different than /tmp/mysql.sock, you've found your problem. Change either that line, or your Django config so they match.

Note: In case you don't find /etc/my.cnf you can create it yourself and add the appropriate socket settings (i.e. just add the line socket=/tmp/mysql.sock) just to make sure it's properly configured.

As far as 1 is concerned, you can follow the instructions available here and make sure your MySql server is running.

Good luck!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...