MySQL Docker 错误:将 ECONNREFUSED 127.0.0.1:7201 连接到 mysql 容器

时间:2021-05-09 12:29:34

标签: node.js docker-compose

为什么连接mysql容器会被拒绝?

(node:43) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED 127.0.0.1:7201
users-service_1     |     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
users-service_1     |     --------------------
users-service_1     |     at Protocol._enqueue (/opt/app/node_modules/mysql/lib/protocol/Protocol.js:144:48)
users-service_1     |     at Protocol.handshake (/opt/app/node_modules/mysql/lib/protocol/Protocol.js:51:23)
users-service_1     |     at PoolConnection.connect (/opt/app/node_modules/mysql/lib/Connection.js:116:18)
users-service_1     |     at Pool.getConnection (/opt/app/node_modules/mysql/lib/Pool.js:48:16)
users-service_1     |     at /opt/app/src/driver/mysql/MysqlDriver.ts:894:18
users-service_1     |     at new Promise (<anonymous>)
users-service_1     |     at MysqlDriver.createPool (/opt/app/src/driver/mysql/MysqlDriver.ts:891:16)
users-service_1     |     at MysqlDriver.<anonymous> (/opt/app/src/driver/mysql/MysqlDriver.ts:344:36)
users-service_1     |     at step (/opt/app/node_modules/tslib/tslib.js:143:27)
users-service_1     |     at Object.next (/opt/app/node_modules/tslib/tslib.js:124:57)
users-service_1     | (Use `node --trace-warnings ...` to show where the warning was created)
users-service_1     | (node:43) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
users-service_1     | (node:43) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
docker.compose.yml

version: "3"
services:
  api-gateway:
    build:
      context: "."
      dockerfile: "./api-gateway/Dockerfile"
    depends_on:
      - chat-service
      - users-service
    ports:
      - "7000:7000"
    volumes:
      - ./api-gateway:/opt/app

  chat-service:
    build:
      context: "."
      dockerfile: "./chat-service/Dockerfile"
    depends_on:
      - chat-service-db
    ports:
      - "7100:7100"
    volumes:
      - ./chat-service:/opt/app

  chat-service-db:
    environment:
      - MYSQL_ROOT_PASSWORD=password
      - MYSQL_DATABASE=db
    image: mysql:5.7.20
    ports:
      - "7200:3306"

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    ports:
      - "7300:80"
    volumes:
      - ./phpmyadmin/config.user.inc.php:/etc/phpmyadmin/config.user.inc.php

  users-service:
    build:
      context: "."
      dockerfile: "./users-service/Dockerfile"
    depends_on:
      - users-service-db
    ports:
      - "7101:7101"
    volumes:
      - ./users-service:/opt/app

  users-service-db:
    environment:
      - MYSQL_PASSWORD=password
      - MYSQL_DATABASE=db
      - MYSQL_USER=root
      - MYSQL_HOST=localhost
      - MYSQL_ROOT_PASSWORD=password 
    restart: always  
    image: mysql:5.7.20
    expose:
      - 7201
    ports:
      - "7201:3306"

在 connection.ts

import config from "config";
import { Connection, createConnection } from "typeorm";
let connection: Connection;

export const initConnection = async () => {
  connection = await createConnection({
    type: "mysql",
    host: "localhost",
    port: 7201,
    username: "root",
    password: "password",
    database: "db",
    synchronize: false,
    logging: false,
    entities: ["./src/db/entities/*.ts"],
    migrations: ["./src/db/migrations/*.ts"],
    cli: {
      entitiesDir: "./src/db/entities",
      migrationsDir: "./src/db/migrations",
    },
  });
};

const getConnection = () => connection;

export default getConnection;

请帮忙。不知道该怎么做,但在使用 typeorm 进行数据库迁移后,没有任何效果,我只是不知道您为什么有任何想法。或者需要更多的代码来解决问题会很高兴

你能否稍微澄清一下端口和主机名,因为在 docker-compose.yml 中

users-service-db is
  ports:
   # <Port exposed> : < MySQL Port 7201 running inside container>
  - '7201:3306' <===is the 7201 the port from outside of the container i don't get it?which port is what?
expose:
  # Opens port 7201on the container
  - '7201'

并且在 typeOrmConfig.ts 文件中,主机被定义为 localhost,如您所见

export = {
    type: "mysql",
    host: "localhost",
    port: 7201,
    username: "root",
    password: "password",
    database: "db",
    synchronize: false,
    logging: false,
    entities: ["./src/db/entities/**/*.ts"],
    migrations: ["./src/db/migrations/**/*.ts"],
    cli: {
        entitiesDir: "./src/db/entities",
        migrationsDir: "./src/db/migrations",
         },
     };

2 个答案:

答案 0 :(得分:2)

在那个 Docker-Compose 星座中,从应用程序容器的角度来看,您的 MySQL 服务器不在 localhost:7201 上,而是在 users-service-db:7201 上。

在您的数据库连接配置中切换它。

答案 1 :(得分:2)

您尝试将 127.0.0.1:7201 更改为 users-service-db:7201

您使用的是docker容器,隔离环境,其他容器无法访问127.0.0.1