Node and Mysql connection error in Docker Compose: Error: connect ECONNREFUSED

Asked

Viewed 2,080 times

0

I created a file docker-compose.yml to load a Mysql database and a Nodejs server:

version: '3'
services:
  database:
    image: mysql:5.7
    container_name: 'database'
    restart: 'always'
    volumes:
      - ./src/database/data:/var/lib/mysql
    ports:
      - '3306:3306'
    expose:
      - '3306'
    networks:
      - backend
    environment:
      MYSQL_USER: 'root'
      MYSQL_ROOT_PASSWORD: 'root'
      MYSQL_DATABASE: 'data-mysql'
  api:
    image: node:10.16-slim
    container_name: 'api'
    ports:
      - '8000:8000'
    working_dir: '/home/node/app'
    volumes:
      - ./:/home/node/app
    command: 'yarn server'
    networks:
      - backend

networks:
  backend:
    driver: 'bridge'

In my terminal, when I give the command: docker-compose up -d, appears:

Creating network "backend_backend" with driver "bridge"
Creating node-api ... 
Creating my-database ... 
Creating my-database
Creating my-database ... done

Viewing active containers with the command docker ps, appears:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
fca4c78e70c5        node:10.16-slim     "docker-entrypoint.s…"   4 minutes ago       Up 4 minutes        0.0.0.0:8000->8000/tcp              node-api
f9702dbec8c5        mysql:5.7           "docker-entrypoint.s…"   4 minutes ago       Up 4 minutes        0.0.0.0:3306->3306/tcp, 33060/tcp   my-database

After all this, when will I see the container logs node-api, with the command docker logs -f fca4c78e70c5, appears:

[nodemon] starting `ts-node src/server.ts src/server.ts`
(node:41) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED 127.0.0.1:3306
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1106:14)
    --------------------
    at Protocol._enqueue (/home/node/app/node_modules/mysql/lib/protocol/Protocol.js:144:48)
    at Protocol.handshake (/home/node/app/node_modules/mysql/lib/protocol/Protocol.js:51:23)
    at PoolConnection.connect (/home/node/app/node_modules/mysql/lib/Connection.js

I’m doing a project with Typeorm, a Typescript ORM, the configuration file and connection to the database is:

{
  "type": "mysql",
  "host": "localhost",
  "port": 3306,
  "username": "root",
  "password": "root",
  "database": "data-mysql",
  "logging": true,
  "entities": [
    "src/models/**/*.ts"
  ],
  "migrations": [
    "src/database/migrations/**/*.ts"
  ],
  "cli": {
    "entitiesDir": "src/models",
    "migrationsDir": "src/database/migrations"
  }
}

I’ve done a lot of research and I can’t find anything to solve this problem, someone can help me?

Thanks for all your answers! :)

1 answer

2


Talk guys! Next, no one answered me.

So, like every good old programmer, I went to research plus to find the solution to my problem, and yes, I found the answer.

What was happening was that in my file ormconfig.json I was setting the host from my database as localhost, when in fact I should set as the container I created in the Docker Compose, which in that case is database.

Changing the file, it was like this:

{
  "type": "mysql",
  "host": "database",
  "port": 3306,
  "username": "root",
  "password": "root",
  "database": "data-mysql",
  "logging": true,
  "entities": [
    "src/models/**/*.ts"
  ],
  "migrations": [
    "src/database/migrations/**/*.ts"
  ],
  "cli": {
    "entitiesDir": "src/models",
    "migrationsDir": "src/database/migrations"
  }
}

So to run the containers, just run the command:

docker-compose up -d

I hope to help someone else who’s in the same trouble!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.