gpt4 book ai didi

node.js - MySQL Docker 错误 : connect ECONNREFUSED 127. 0.0.1:7201 到 mysql 容器

转载 作者:行者123 更新时间:2023-12-05 00:53:39 29 4
gpt4 key购买 nike

为什么拒绝连接到 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 进行 db 迁移后,没有任何效果,我只是不知道您为什么有任何想法。或者需要更多的代码来解决问题会应用它

你能澄清一下端口和主机名吗,因为在 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",
},
};

最佳答案

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

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

关于node.js - MySQL Docker 错误 : connect ECONNREFUSED 127. 0.0.1:7201 到 mysql 容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67457694/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com