MySQL Docker Error: connect ECONNREFUSED 127.0.0.1:7201 to mysql container

Viewed 1984

Why is the connection refused to the mysql container?

(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"

in the 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;

please help .don't know what to do it worked but after db migration with typeorm nothing worked i just dont know why do you have any ideas. Or need more code to solve the provblem would appriciate it

can you clarify the port and host names a little bit because in the docker-compose.yml the

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'

and in the typeOrmConfig.ts file the host is defined as localhost as you can see

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 Answers

In that Docker-Compose constellation, your MySQL server isn't on localhost:7201 from the viewpoint of the application container(s), it's on users-service-db:7201.

Switch that in your database connection configuration.

You try changing 127.0.0.1:7201 to users-service-db:7201

You are using docker container, isolated environment, 127.0.0.1 cannot be accessed from other containers

Related