Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

1.3K
Visualizações
TCPConnectWrap.afterConnect - node can't get data from mysql when are in docker compose

I'm trying to connect node with mysql through dokcer-compose. I can access mysql with workbench but when I try to connect with node I get the error:

Error: connect ECONNREFUSED 127.0.0.1:3306
     at TCPConnectWrap.afterConnect [as oncomplete] (net.js: 1054: 14))

Does anyone know what is going on?

docker-compose.yml

version: '3'

services:

  #App Service
  app:
    image: "node:alpine"
    container_name: cms-node
    restart: unless-stopped
    tty: true
    working_dir: /app
    environment:
      - NODE_ENV=production
    ports:
      - 1234:1234
    volumes:
      - ./:/app
    links:
      - db
    depends_on:
      - db

  #Mysql Service
  db:
    image: mysql:latest
    container_name: cms-mysql
    restart: unless-stopped
    # command: --default-authentication-plugin=mysql_native_password
    environment:
      MYSQL_DATABASE: CMS
      MYSQL_USER: root
      MYSQL_PASSWORD: root
      MYSQL_ROOT_PASSWORD: root
    ports:
      - 3306:3306

node create connection

const db: Connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: 'root',
    database: 'CMS',
});

export default db;

node try to connect

db.connect((err) => {
            if (err) {
                throw err;
            }
            console.log('Connected!');
        });
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You should use the link name (by default, the name of the linked service) as the hostname to connect to. See the docker-compose.yml documentation for details.

So just use host: 'db' instead of host: 'localhost'

over 4 years ago · Santiago Trujillo Relatório

0

Yes, we have to use service name from docker-compose.yml. I faced a similar issue when there was a custom front end API Gateway server (working as proxy) written in node.js connecting to other two services. So replacing localhost:3001 with serviceName:30001 fixed the issue.

Example code:

...
const proxy = require('express-http-proxy');
...

app.use('/json-parser', proxy('json_parsing_api:3001'));
app.get('/json-parser/json-to-person', proxy('json-to-person'));
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda