31

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.

Erki M.
  • 5,022
  • 1
  • 48
  • 74
robert
  • 598
  • 1
  • 5
  • 12
  • 1
    Could you connect locally to your MySQL server using [MySQL CLI](http://dev.mysql.com/doc/refman/5.5/en/mysql.html)? (Something like that: `mysql -u you_user_name -p`) – Sylvain Leroux Aug 09 '13 at 16:01
  • What is your `DATABASES` settings? – twil Aug 09 '13 at 20:46
  • I can connect to mysql server using mysql -u user_name -p . I set database as django recomended – robert Aug 10 '13 at 02:15
  • Could you set `socket = /var/run/mysqld/mysqld.sock` directly? That way you don't have to link `/tmp/mysql.sock` to that path manually. – Hieu Nguyen Aug 10 '13 at 14:05
  • 4
    possible duplicate of [Can't connect to local MySQL server through socket '/var/mysql/mysql.sock' (38)](http://stackoverflow.com/questions/5376427/cant-connect-to-local-mysql-server-through-socket-var-mysql-mysql-sock-38) – Anto Oct 29 '13 at 14:14
  • Possible duplicate of [ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)](http://stackoverflow.com/questions/11657829/error-2002-hy000-cant-connect-to-local-mysql-server-through-socket-var-run) – Elzo Valugi Aug 02 '16 at 16:39

5 Answers5

71

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',
   }
}
Md. Ibrahim
  • 1,239
  • 14
  • 18
  • 6
    Any idea why that would make any difference? Isn't localhost the same as 127.0.0.1 in most cases? – Calimo Jul 23 '20 at 07:42
  • 4
    This is one of those bittersweet moments we all have in software development sometime: I'm glad it worked, but at the same time I have no idea WHY it did work. Thanks nonetheless. – Luis Milanese Nov 22 '21 at 04:34
1

For me this worked add OPTIONS attribute with read_default_file and give it the path of my.cnf file

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'dbname',
        'USER': 'root',
        'PASSWORD': '',
        'HOST': 'localhost',
        'PORT': '3306',
        'OPTIONS': {
            'read_default_file': '/opt/lampp/etc/my.cnf',
        }
    }
}
AkshayJ
  • 321
  • 1
  • 7
1

You need to change your HOST from 'localhost' to '127.0.0.1' and check your django app :)

Muhammadalive
  • 648
  • 5
  • 5
0

in flask, you may use that

app=Flask(__name__)

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

0

I faced this problem when connecting MySQL with Django when using Docker.

Try 'PORT':'0.0.0.0'.

Do not use 'PORT': 'db'. This will not work if you tried to run your app outside Docker.

Mahdy H.
  • 181
  • 9