Django无法在Docker+Django+MySQL中连接到MySQL

sr4lhrrt  于 2023-03-31  发布在  Go
关注(0)|答案(1)|浏览(120)

我正在尝试将我的打工学生应用程序打包到容器中。下面是我的 docker-compose.yaml

version: "3.8"

services:
  mysql_db:
    build:
      context: .
      dockerfile: ./docker/mysql/Dockerfile
    restart: unless-stopped
    env_file: env.dev
    ports:
      - "33061:3306"
    networks:
      - autopark_network

  backend:
    build:
      context: .
      dockerfile: docker/django/Dockerfile
    env_file: env.dev
    restart: on-failure
    depends_on:
      - mysql_db
    working_dir: "/app/autopark"
    expose:
      - 800
    ports:
      - "8001:8000"
    command: ["python3", "manage.py", "runserver", "0.0.0.0:8000"]
    networks:
      - autopark_network

networks:
    autopark_network:

这里是env.dev:

DEBUG=1
SECRET_KEY=<...>
DJANGO_ALLOWED_HOSTS="localhost 127.0.0.1 [::1]"
MYSQL_ENGINE=django.contrib.gis.db.backends.mysql
MYSQL_DATABASE=autopark
MYSQL_DATABASE_HOST=mysql_db 
MYSQL_DATABASE_PORT=33061
MYSQL_ROOT_PASSWORD=<...>
MYSQL_USER=pavel
MYSQL_PASSWORD=<...>

我认为我的两个服务都将获得这些环境变量,这样我就可以确保一切正常。
我的项目需要GDAL,这就是为什么我启动 backend 不是用python而是用ubuntu(这是我的后端Dockerfile):

FROM ubuntu:22.04
MAINTAINER Pavel Vasilev <pvlvslv@yandex.ru>

RUN apt -y update
RUN apt -y install python3-dev python3-pip gdal-bin gdal-data

WORKDIR /app/autopark
ADD . /app/autopark

RUN pip install -r requirements.txt

然后我需要初始化我的mysql_db(这里是我的mysql_db的Dockerfile):

FROM mysql/mysql-server:latest

COPY ./docker/mysql/init_db.sh /docker-entrypoint-initdb.d/
COPY ./docker/mysql/init_db.sql /usr/local/
RUN chmod +x /docker-entrypoint-initdb.d/init_db.sh

下面是 init_db.sh

#!/bin/bash

mysql -u root --password="$MYSQL_ROOT_PASSWORD"  << EOF
SOURCE /usr/local/init_db.sql
USE ${MYSQL_DATABASE};
GRANT ALL PRIVILEGES ON ${MYSQL_DATABASE}.* TO '${MYSQL_USER}';
EOF

init_db. sql是用mysqldump这样做的。
当我执行docker-compose build时,一切都很顺利。当我执行docker-compose up时,我有:

Recreating autopark_docker_mysql_db_1 ... done
Recreating autopark_docker_backend_1  ... done
Attaching to autopark_docker_mysql_db_1, autopark_docker_backend_1
mysql_db_1  | [Entrypoint] MySQL Docker Image 8.0.32-1.2.11-server
mysql_db_1  | [Entrypoint] Initializing database
mysql_db_1  | 2023-03-29T05:11:30.618219Z 0 [Warning] [MY-011068] [Server] The syntax '--skip-host-cache' is deprecated and will be removed in a future release. Please use SET GLOBAL host_cache_size=0 instead.
mysql_db_1  | 2023-03-29T05:11:30.622277Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.32) initializing of server in progress as process 17
mysql_db_1  | 2023-03-29T05:11:30.653228Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
mysql_db_1  | 2023-03-29T05:11:31.643747Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
backend_1   | Watching for file changes with StatReloader
backend_1   | Exception in thread django-main-thread:
backend_1   | Traceback (most recent call last):
<... skip ...>
backend_1   | ConnectionRefusedError: [Errno 111] Connection refused
backend_1   | 
backend_1   | During handling of the above exception, another exception occurred:
backend_1   | 
backend_1   | Traceback (most recent call last):
<... skip ...>
backend_1   | pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'mysql_db' ([Errno 111] Connection refused)")
backend_1   | 
backend_1   | The above exception was the direct cause of the following exception:
backend_1   | 
backend_1   | Traceback (most recent call last):
<... skip ...>backend_1   | django.db.utils.OperationalError: (2003, "Can't connect to MySQL server on 'mysql_db' ([Errno 111] Connection refused)")
mysql_db_1  | 2023-03-29T05:11:33.028156Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
mysql_db_1  | [Entrypoint] Database initialized
mysql_db_1  | 2023-03-29T05:11:36.080939Z 0 [Warning] [MY-011068] [Server] The syntax '--skip-host-cache' is deprecated and will be removed in a future release. Please use SET GLOBAL host_cache_size=0 instead.
mysql_db_1  | 2023-03-29T05:11:36.084369Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.32) starting as process 58
mysql_db_1  | 2023-03-29T05:11:36.120288Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
mysql_db_1  | 2023-03-29T05:11:36.339612Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
mysql_db_1  | 2023-03-29T05:11:36.668690Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
mysql_db_1  | 2023-03-29T05:11:36.669242Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
mysql_db_1  | 2023-03-29T05:11:36.689594Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: /var/run/mysqld/mysqlx.sock
mysql_db_1  | 2023-03-29T05:11:36.690338Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.32'  socket: '/var/lib/mysql/mysql.sock'  port: 0  MySQL Community Server - GPL.
mysql_db_1  | Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
mysql_db_1  | Warning: Unable to load '/usr/share/zoneinfo/leapseconds' as time zone. Skipping it.
mysql_db_1  | Warning: Unable to load '/usr/share/zoneinfo/tzdata.zi' as time zone. Skipping it.
mysql_db_1  | Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
mysql_db_1  | Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.
mysql_db_1  | 
mysql_db_1  | [Entrypoint] running /docker-entrypoint-initdb.d/init_db.sh
mysql_db_1  | mysql: [Warning] Using a password on the command line interface can be insecure.
mysql_db_1  | ERROR 1046 (3D000) at line 22 in file: '/usr/local/init_db.sql': No database selected
mysql_db_1  | [Entrypoint] Starting MySQL 8.0.32-1.2.11-server
mysql_db_1  | 2023-03-29T05:11:40.431294Z 0 [Warning] [MY-011068] [Server] The syntax '--skip-host-cache' is deprecated and will be removed in a future release. Please use SET GLOBAL host_cache_size=0 instead.
mysql_db_1  | 2023-03-29T05:11:40.433961Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.32) starting as process 1
mysql_db_1  | 2023-03-29T05:11:40.443031Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
mysql_db_1  | 2023-03-29T05:11:41.798794Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
mysql_db_1  | 2023-03-29T05:11:41.939345Z 0 [System] [MY-010229] [Server] Starting XA crash recovery...
mysql_db_1  | 2023-03-29T05:11:41.958108Z 0 [System] [MY-010232] [Server] XA crash recovery finished.
mysql_db_1  | 2023-03-29T05:11:42.002608Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
mysql_db_1  | 2023-03-29T05:11:42.002949Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
mysql_db_1  | 2023-03-29T05:11:42.016603Z 0 [ERROR] [MY-010259] [Server] Another process with pid 60 is using unix socket file.
mysql_db_1  | 2023-03-29T05:11:42.016825Z 0 [ERROR] [MY-010268] [Server] Unable to setup unix socket lock file.
mysql_db_1  | 2023-03-29T05:11:42.016932Z 0 [ERROR] [MY-010119] [Server] Aborting
mysql_db_1  | 2023-03-29T05:11:43.579880Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.32)  MySQL Community Server - GPL.
autopark_docker_mysql_db_1 exited with code 1
mysql_db_1  | [Entrypoint] Starting MySQL 8.0.32-1.2.11-server
mysql_db_1  | 2023-03-29T05:11:45.384210Z 0 [Warning] [MY-011068] [Server] The syntax '--skip-host-cache' is deprecated and will be removed in a future release. Please use SET GLOBAL host_cache_size=0 instead.
mysql_db_1  | 2023-03-29T05:11:45.386889Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.32) starting as process 1
mysql_db_1  | 2023-03-29T05:11:45.398475Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
mysql_db_1  | 2023-03-29T05:11:45.602149Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
mysql_db_1  | 2023-03-29T05:11:45.832458Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
mysql_db_1  | 2023-03-29T05:11:45.833092Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
mysql_db_1  | 2023-03-29T05:11:45.834110Z 0 [ERROR] [MY-010259] [Server] Another process with pid 60 is using unix socket file.
mysql_db_1  | 2023-03-29T05:11:45.834490Z 0 [ERROR] [MY-010268] [Server] Unable to setup unix socket lock file.
mysql_db_1  | 2023-03-29T05:11:45.834832Z 0 [ERROR] [MY-010119] [Server] Aborting
mysql_db_1  | 2023-03-29T05:11:47.380031Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.32)  MySQL Community Server - GPL.

最后一部分继续重复。
我想我在安装后初始化mysql失败了,但我不知道在哪里。花了两天时间,所有的想法都在我的脑海里混合了。

r8xiu3jd

r8xiu3jd1#

我想我安装后初始化mysql失败了
我通常把我mysql相关的文件结构做成这样:

  • 例如MYSQL_DATABASE是***robot***
.
├── docker-compose.yml
└── mysql
    ├── 01_schema.sql
    ├── Dockerfile
    └── robot.sql

01_schema.sql 的内容为:

  • 它将创建MYSQL_DATABASE
create database `robot` default character set utf8mb4 collate utf8mb4_0900_ai_ci;

robot.sql * 的内容喜欢:

  • 它将指定docker compose使用的DATABASE。
-- i add this line ahead of the file
USE robot;

/*
 Navicat Premium Data Transfer

 Source Server         : D
 Source Server Type    : MySQL
*/
......

Dockerfile 的内容点赞数:*

  • 将sql脚本添加到图像并在启动时初始化它
......
COPY ./01_schema.sql /docker-entrypoint-initdb.d
COPY ./robot.sql /docker-entrypoint-initdb.d

mysql相关的 docker-compose.yml 文件内容如下:

version: '3.7'
services:
......
  mysql:
    build:
      context: ./mysql
      dockerfile: Dockerfile
    restart: unless-stopped
    container_name: mysql
    environment:
      MYSQL_ROOT_HOST: "%"
      MYSQL_ROOT_PASSWORD: "Hlxx@123prod"
      DB_USER: "root"
      DB_NAMES: "robot"

希望这能对你有所帮助。🤤

相关问题