简体   繁体   English

Prisma 无法连接到 MySQL Docker 容器

[英]Prisma won't connect to MySQL Docker Container

I'm trying to use docker-compose to run a project with two Docker containers, MySQL and NodeJS.我正在尝试使用 docker-compose 来运行一个包含两个 Docker 容器、MySQL 和 NodeJS 的项目。 I'm having trouble connect to the SQL container from the backend container.我无法从后端容器连接到 SQL 容器。 My backend container uses Prisma as it's ORM and shows me the following error when npx prisma migrate dev is ran (during docker-compose up).我的后端容器使用 Prisma,因为它是 ORM 并在运行 npx npx prisma migrate dev时(在 docker-compose 期间)向我显示以下错误。

Error: P1001: Can't reach database server at `mysql`:`3306`
Please make sure your database server is running at `mysql`:`3306`.

After some research, I thought the issue was my database url. The host name should match the db container name (mysql in this case), so I updated that but still no luck.经过一番研究,我认为问题出在我的数据库 url 上。主机名应该与数据库容器名称(在本例中为 mysql)相匹配,所以我更新了它,但仍然没有成功。

DATABASE_URL=mysql://root:root@mysql:3306/myshowlist # .env file

I also tried the container service name (db) but that wasn't it either.我还尝试了容器服务名称 (db),但也不是。

DATABASE_URL=mysql://root:root@db:3306/myshowlist # .env file

What I'm finding puzzling is that my Prisma has no issue connecting to the MySQL container when it's not run inside the container.我感到困惑的是,当我的 Prisma 不在容器内运行时,它连接到 MySQL 容器没有问题。 When I start the MySQL container solo and run the Prisma migrate command, it works and I can use the backend service like normal and save/read from the database.当我单独启动 MySQL 容器并运行 Prisma migrate 命令时,它可以正常工作,我可以像平常一样使用后端服务并从数据库中保存/读取。 I do have to use localhost as the hostname though which makes sense.我确实必须使用 localhost 作为主机名,但这是有道理的。

I'm not sure what could be the issue, I would really appreciate a nudge in the right direction!我不确定可能是什么问题,我真的很感激朝着正确的方向轻推!

docker-compose.yml: docker-compose.yml:

version: "3.8"
services:
    api:
        build:
            context: ./api-old
        ports:
            - "5001:3200"
        container_name: api
        depends_on:
            - db
    
    db:
        image: mysql:5.7
        restart: always
        container_name: mysql
        ports:
            - "3306:3306"
        environment:
          MYSQL_DATABASE: myshowlist
          MYSQL_ROOT_PASSWORD: "root"

./api-old/Dockerfile: ./api-old/Dockerfile:

FROM node:latest

WORKDIR /usr/src/app

COPY package*.json ./
COPY .env ./
COPY prisma ./prisma/

RUN npm ci
RUN npm run db-prod

COPY . .

EXPOSE 5001

CMD ["npm", "start"]

schema.prisma:架构.棱镜:

datasource db {
    provider        = "mysql"
    url             = env("DATABASE_URL")
}

generator client {
    provider        = "prisma-client-js"
}

You have to create an docker.network first, example:您必须先创建一个 docker.network,例如:

docker network create demo-network

Then add this.network to your docker-compose :然后将 this.network 添加到您的docker-compose

version: "3.8"
services:
  api:
    build:
      context: ./api-old
    ports:
      - "5001:3200"
    container_name: api
    depends_on:
      - db
    networks:
      - demo-network

  db:
    image: mysql:5.7
    restart: always
    container_name: mysql
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE: myshowlist
      MYSQL_ROOT_PASSWORD: "root"
    networks:
      - demo-network
networks:
  demo-network:
    external: true

Then re-run the docker-compose and try this url again:然后重新运行docker-compose并再次尝试这个 url:

DATABASE_URL=mysql://root:root@mysql:3306/myshowlist

Hope it help!希望它有所帮助!

Yes so the mysql address needs to be changed to the name of the service that's correct.是的,因此需要将 mysql 地址更改为正确的服务名称。 You don't need to create a.network to access services launched via the same docker compose file, that's only helpful if you have multiple docker compose and you want your services to communicate with one another.您不需要创建一个 .network 来访问通过相同的 docker 撰写文件启动的服务,这仅在您有多个 docker 撰写并且您希望您的服务相互通信时才有用。

I believe the issue is that your mysql container is not fully started when the api tries to run the migration.我认为问题在于当 api 尝试运行迁移时,您的 mysql 容器未完全启动。 So a timing issue most likely.所以最有可能是时间问题。 Check this thread: Docker-compose check if mysql connection is ready检查此线程: Docker-compose 检查 mysql 连接是否准备就绪

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM