2002, "Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2)" i
Posted
技术标签:
【中文标题】2002, "Can\'t connect to local MySQL server through socket \'/run/mysqld/mysqld.sock\' (2)" in docker-compose up【英文标题】:2002, "Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2)" in docker-compose up2002, "Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2)" in docker-compose up 【发布时间】:2020-01-21 13:39:20 【问题描述】:我正在尝试通过docker-compose up
启动服务器
我收到一个错误:
2002, "Can't connect to local mysql server through socket '/run/mysqld/mysqld.sock' (2)"
docker-compose.yml
version: '3'
services:
db:
image: mysql:5.7
environment:
MYSQL_DATABASE: 'slack_bot1'
MYSQL_USER: 'root'
MYSQL_PASSWORD: ''
MYSQL_ROOT_PASSWORD: '****'
volumes:
- /opt/slack_bot/mysql_data:/var/lib/mysql
redis:
image: "redis:alpine"
volumes:
- /opt/slack_bot/redis_data:/var/lib/redis
web:
build: .
command: python manage.py runserver 0.0.0.0:8001
ports:
- "8001:8001"
depends_on:
- db
Dockerfile
FROM python:3.7-alpine
ENV PYTHONUNBUFFERED 1
WORKDIR /home/slack_bot
ADD requirements.txt /home/slack_bot/
RUN set -e; \
apk add --no-cache --virtual .build-deps \
gcc \
libc-dev \
linux-headers \
mariadb-dev \
python3-dev \
;
RUN pip install -r requirements.txt
ADD . /home/slack_bot/
EXPOSE 8001
CMD ["python", "manage.py", "runserver", "0.0.0.0:8001"]
docker ps日志
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
68b61ca0ce74 slack_bot_web "python manage.py ru…" 8 minutes ago Up 32 seconds 0.0.0.0:8001->8001/tcp slack_bot_web_1
c5f254a527b0 mysql:5.7 "docker-entrypoint.s…" 8 minutes ago Up 34 seconds 3306/tcp, 33060/tcp slack_bot_db_1
4cbc1fa3765e redis:alpine "docker-entrypoint.s…" 15 minutes ago Up 33 seconds 6379/tcp slack_bot_redis_1
Django 设置数据库
'ENGINE': 'django.db.backends.mysql',
'NAME': 'slack_bot1',
'USER': 'root',
'PASSWORD': '',
'HOST': '',
'PORT': '',
【问题讨论】:
在你的应用程序代码中哪里配置数据库的位置? 更新代码... 您是尝试在 django 设置中设置'HOST': 'db'
还是真的想通过 unix sock 进行设置?
@Stefano Can't connect to MySQL server on 'db' (115)"
保持 HOST 设置为 db,您可以尝试以下操作吗:docker-compose up -d db
,等一下然后执行 docker-compose up web
【参考方案1】:
在您的 django 设置中,您将数据库主机保留为 EMPTY,则默认值为 localhost
。当使用localhost
时,mysql 客户端驱动程序会将mysql 服务器连接到unix socket
而不是tcp
。
因此,对于您的情况,您需要将 mysql 容器中的 unix socket
导出到卷,然后您的 django 应用程序容器利用此卷共享 unix socket
。
接下来是你需要做的:
手动新建一个文件夹,稍后将其作为共享您的 unix 套接字文件的卷
(重要提示:你不能依赖 docker-compose 来新建这个文件夹,你会遇到权限错误)
mkdir -p /tmp/slack_bot/mysqld && chmod -R 777 /tmp/slack_bot/mysqld
在您的docker-compose.yaml
中,再向mysql
服务添加一个卷:
db:
image: mysql:5.7
environment:
MYSQL_DATABASE: 'slack_bot1'
MYSQL_USER: 'root'
MYSQL_PASSWORD: ''
MYSQL_ROOT_PASSWORD: '****'
volumes:
- /opt/slack_bot/mysql_data:/var/lib/mysql
- /tmp/slack_bot/mysqld:/var/run/mysqld
在您的docker-compose.yaml
中,再向web
服务添加一个卷:
web:
build: .
command: python manage.py runserver 0.0.0.0:8001
ports:
- "8001:8001"
depends_on:
- db
volumes:
- /tmp/slack_bot/mysqld:/run/mysqld
注意:在这里你说你得到了错误/run/mysqld/mysqld.sock
作为下一个,我不确定你是否粘贴所有日志,因为大多数情况下它可能是/var/run/mysqld/mysqld.sock
,nertheless,如果错误是/var/run/mysqld/mysqld.sock
,那么您应该将上面的示例音量修改为- /tmp/slack_bot/mysqld:/var/run/mysqld
2002, "Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2)"
有了上面,现在你的 web 服务可以共享 mysql 容器中的 unix 套接字了。
另外,将HOST
配置为db
我认为也可以是另一种解决方案,只是unix socket
比使用tcp
快得多。
【讨论】:
以上是关于2002, "Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2)" i的主要内容,如果未能解决你的问题,请参考以下文章
OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/m
Mac 上的 Django + XAMPP (2002, "Can't connect to local MySQL server through socket '/tmp/mysql.so
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) when I
“ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'
Can't access Phpmyadmin in ubuntu error mysqli_real_connect(): (HY000/2002)
Can't connect to database with error (HY000/2002): No connection could be made because the target ma