[英]Django + XAMPP on Mac (2002, "Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)")
I am following this tutorial to build a Django app with MySQL.我正在按照本教程使用 MySQL 构建一个 Django 应用程序。
I am using XAMPP-VM for Mac.我正在为 Mac 使用 XAMPP-VM。 I set up my Database settings for Django as so:
我为 Django 设置了数据库设置,如下所示:
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'djangoproject', 'USER': 'root', 'PASSWORD': '', 'HOST': 'localhost', 'PORT': '8080', } }
And I have started MySQL on XAMPP but when I run python manage.py migrate I get:我已经在 XAMPP 上启动了 MySQL,但是当我运行 python manage.py migrate 时,我得到:
django.db.utils.OperationalError: (2002, "Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)")
django.db.utils.OperationalError: (2002, "无法通过套接字'/tmp/mysql.sock' (2) 连接到本地 MySQL 服务器")
Thank you for your help!感谢您的帮助!
Just change HOST
from localhost
to the ip 127.0.0.1
.只需将
HOST
从localhost
更改为 ip 127.0.0.1
。 This will resolve your problem.这将解决您的问题。
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'djangoproject',
'USER': 'root',
'PASSWORD': '',
'HOST': '127.0.0.1',
'PORT': '8080',
}
}
The mysql client tries to connect through the /tmp/mysql.sock by default.默认情况下,mysql 客户端尝试通过 /tmp/mysql.sock 进行连接。 You just have to point it to XAMPP's socket with a symbolic link.
您只需要使用符号链接将其指向 XAMPP 的套接字。
ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock
I would try changing your HOST from 'localhost' to '127.0.0.1'.我会尝试将您的主机从“本地主机”更改为“127.0.0.1”。 This usually rectifies it on my end.
这通常会在我结束时纠正它。
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'djangoproject',
'USER': 'root',
'PASSWORD': '',
'HOST': '127.0.0.1',
'PORT': '8080',
}
}
我在 mysql 最新版本中遇到了同样的错误,所以我降级到 mysql 5.7.0 并且它工作正常。
This is probably due to XAMPP-VM being installed and not the local XAMPP.这可能是由于安装了 XAMPP-VM 而不是本地 XAMPP。 Uninstall XAMPP-VM and install XAMPP.
卸载 XAMPP-VM 并安装 XAMPP。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.