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 with Error: connect ECONNREFUSED

Attempting to connect database from within the microservice, failing to the connect the database

Error: connect ECONNREFUSED 172.18.0.2:3306

service/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

I've attempted connecting to database with different settings.

1) Without port

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

2) specified port 3306

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

3) specified port 6603

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

database/Dockerfile

FROM mysql

ENV MYSQL_DATABASE=test
ENV MYSQL_ROOT_PASSWORD=password

EXPOSE 6603:3306

COPY ./schema.sql /docker-entrypoint-initdb.d/

Basically how my node.js microservice can discover the database service?


Edit

I suspected that database wasn't ready by the time nodejs kicks in, so I added some delay before connecting to database and error changed

Updated Code

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);

output

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

You miss order:

First connect to database, then listen the port.

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

Probably you are using a version of MySQL that doesnt support the login you are trying. Try with 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

the database port does not get translated as it's within the container. just use 3306 in your app

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!