Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

1.3K
Views
docker mysql con error: conectar ECONNREFUSED

Intentando conectar la base de datos desde dentro del microservicio, fallando al conectar la base de datos

Error: conectar ECONNREFUSED 172.18.0.2:3306

servicio/index.js

 var http = require('http'); //create a server object: http.createServer(function (req, res) { res.write('Hello World!'); //write a response to the client res.end(); //end the response }).listen(8080); //the server object listens on port 8080 console.log("Listening at 8080"); var mysql = require('mysql'); var con = mysql.createConnection({ host: "database", user: "root", password: "password" }); con.connect(function(err) { if (err) throw err; console.log("Database Connected!"); });

docker-compose.yml

 version: '3' services: database: build: ./database ports: - "6603:3306" image: "test-mysql" container_name: "test-mysql" service: build: ./service ports: - "8080:8080" depends_on: - database image: "test-nodejs" container_name: "test-nodejs" restart: on-failure

Intenté conectarme a la base de datos con diferentes configuraciones.

1) Sin puerto

 var con = mysql.createConnection({ host: "database", user: "root", password: "password" });

2) puerto especificado 3306

 var con = mysql.createConnection({ host: "database", user: "root", password: "password" port: 3306 });

3) puerto especificado 6603

 var con = mysql.createConnection({ host: "database", user: "root", password: "password", port: 6603 });

base de datos/Dockerfile

 FROM mysql ENV MYSQL_DATABASE=test ENV MYSQL_ROOT_PASSWORD=password EXPOSE 6603:3306 COPY ./schema.sql /docker-entrypoint-initdb.d/

Básicamente, ¿cómo mi microservicio node.js puede descubrir el servicio de la base de datos?


Editar

Sospeché que la base de datos no estaba lista cuando se activa nodejs, así que agregué un poco de retraso antes de conectarme a la base de datos y el error cambió

Código actualizado

 setTimeout(function(){ var mysql = require('mysql'); var con = mysql.createConnection({ host: "database", user: "root", password: "password" }); con.connect(function(err) { if (err) throw err; console.log("Database Connected!"); }); }, 20 * 1000);

producción

 Error: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Echas de menos el orden:

Primero conéctese a la base de datos, luego escuche el puerto.

 var http = require('http'); var mysql = require('mysql'); var con = mysql.createConnection({ host: "database", user: "root", password: "password" } ); con.connect(function(err) { if (err) throw err; console.log("Database Connected!"); }); //create a server object: http.createServer(function (req, res) { res.write('Hello World!'); //write a response to the client res.end(); //end the response }).listen(8080); //the server object listens on port 8080 console.log("Listening at 8080");
over 4 years ago · Santiago Trujillo Report

0

Probablemente esté utilizando una versión de MySQL que no admite el inicio de sesión que está intentando. Prueba con mysql v5.7:

 docker run -d -p 6603:3306 --name mysql-container -e MYSQL_ROOT_PASSWORD=password mysql:5.7
over 4 years ago · Santiago Trujillo Report

0

el puerto de la base de datos no se traduce porque está dentro del contenedor. solo usa 3306 en tu aplicación

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!