Docker error : MySQLdb._exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")
Docker error : MySQLdb._exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")
我正在尝试通过 docker-compose up
启动服务器
我收到一个错误:
MySQLdb._exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")
但我可以使用以下命令连接到 mysql
docker exec -it containername mysql -uroot -p
docker-compose.yml
version: '3.1'
services:
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
volumes:
- ./db:/docker-entrypoint-initdb.d/:ro
restart: always
environment:
MYSQL_ROOT_PASSWORD: ****
MYSQL_DATABASE: emp
MYSQL_PASSWORD: ****
ports:
- "33061:3306"
emp:
build: .
restart: always
ports:
- 5005:5000
volumes:
my-db:
docker文件
FROM python:3.6-buster
LABEL image for a very management application
# We copy just the requirements.txt first to leverage Docker cache
COPY ./requirements.txt /app/requirements.txt
WORKDIR /app
RUN apt-get install -y default-libmysqlclient-dev
RUN pip3 install -r requirements.txt
RUN touch /var/log/cron.log
COPY crontab /etc/cron.d/cjob
RUN chmod 0644 /etc/cron.d/cjob
ENV PYTHONUNBUFFERED 1
CMD cron -f
EXPOSE 5000
COPY . .
CMD [ "python3", "app.py" ]
DE 配置:
from flask_mysqldb import MySQL,MySQLdb
app = Flask(__name__)
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = '*****'
app.config['MYSQL_PASSWORD'] = '****'
app.config['MYSQL_DB'] = 'emp'
app.config['MYSQL_CURSORCLASS'] = 'DictCursor'
mysql = MySQL(app)
谁能帮我解决这个问题。谢谢大家
app.config['MYSQL_HOST'] = 'db'
我正在尝试通过 docker-compose up
启动服务器我收到一个错误:
MySQLdb._exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")
但我可以使用以下命令连接到 mysql
docker exec -it containername mysql -uroot -p
docker-compose.yml
version: '3.1'
services:
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
volumes:
- ./db:/docker-entrypoint-initdb.d/:ro
restart: always
environment:
MYSQL_ROOT_PASSWORD: ****
MYSQL_DATABASE: emp
MYSQL_PASSWORD: ****
ports:
- "33061:3306"
emp:
build: .
restart: always
ports:
- 5005:5000
volumes:
my-db:
docker文件
FROM python:3.6-buster
LABEL image for a very management application
# We copy just the requirements.txt first to leverage Docker cache
COPY ./requirements.txt /app/requirements.txt
WORKDIR /app
RUN apt-get install -y default-libmysqlclient-dev
RUN pip3 install -r requirements.txt
RUN touch /var/log/cron.log
COPY crontab /etc/cron.d/cjob
RUN chmod 0644 /etc/cron.d/cjob
ENV PYTHONUNBUFFERED 1
CMD cron -f
EXPOSE 5000
COPY . .
CMD [ "python3", "app.py" ]
DE 配置:
from flask_mysqldb import MySQL,MySQLdb
app = Flask(__name__)
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = '*****'
app.config['MYSQL_PASSWORD'] = '****'
app.config['MYSQL_DB'] = 'emp'
app.config['MYSQL_CURSORCLASS'] = 'DictCursor'
mysql = MySQL(app)
谁能帮我解决这个问题。谢谢大家
app.config['MYSQL_HOST'] = 'db'