docker mysql with Error: connect ECONNREFUSED
Asked Answered
P

5

6

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
Pluck answered 22/5, 2019 at 11:3 Comment(8)
Have you tried connecting to the MySQL service yourself using a regular client like dBeaver once its stood up?Seanseana
i can connect via mysqlworkbench via 127.0.0.1:3306 successfully.Pluck
please see my edit, the error changed after I added some delay before the connection.Pluck
not need to timeout just order fine your code. Look at my responseNatatorium
and to that ER_NOT_SUPPORTED_AUTH_MODE you have this to fix it #50093644Natatorium
Great thinking! Sounds like a much more solvable question nowSeanseana
solved by degrading the database to mysql:5.7 but it works only with the delay, depends_on in my docker-compose isn't helping me much. (re-ordering as suggested isn't helpful either)Pluck
Sharing an insight, as I'm using WebStorm which actually listens to all the docker events and from the logs, I can clearly see that nodejs is kicking in before mysql is fully initialized. so depends_on is not perfectPluck
T
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
Tipton answered 22/5, 2019 at 12:0 Comment(12)
Yes I downgraded the database and it worked, however, I still have to maintain the delay before the connection like 10seconds before i connect to database, depends_on isn't that helpful, any ideas?Pluck
Just noticed you comment from the other postTipton
no error, it's working now after the degrade, just this delay with settimeout before i connect to db is a nuisance.Pluck
Remove the delay and try?Tipton
I have done many apps with express and mysql and didnt need any delay. Tell me if you need help with that or if you finally solve itTipton
without delay i get this error -> Error: connect ECONNREFUSED 172.18.0.2:3306, did you try connecting to mysql right away in the start script index.js or the connection happens when an http request comes in? From the interactive logs I observe that while mysql is still initializing, the docker eagerly kicks in nodejs container.Pluck
I think i have this situation mentioned in here. dev.to/hugodias/wait-for-mongodb-to-start-on-docker-3h8bPluck
Maybe you are starting the mysql server from Node.js (is that even possible?) ? I start the mysql server manually and then start the execution of Node.js. Not sure what setup you are usingTipton
Oh yes, looks like so! Not sure how to handle that honestly. I dont have much experience with docker. But I suppose docker should start the node app once the database is up somehowTipton
not starting from nodejs, it's docker's responsibility. Docker starts the mysql container and then moves onto nodejs right way while mysql taking sometime to initialize fully compared to nodejs which just executes and tries to create a connection. the whole process is docker based.Pluck
you should get into docker, save this link dev.to/hugodias/wait-for-mongodb-to-start-on-docker-3h8bPluck
I understand now, you are right about the docker responsability. Yes, Im starting with docker now and I find it wonderful! :)Tipton
N
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");
Natatorium answered 22/5, 2019 at 11:33 Comment(2)
re-ordering didn't solve the problem, I feel both mysql and nodejs containers are getting kicked in simultaneously before even mysql is fully initialized, even though i've specified dependency for my service.Pluck
after I downgraded my database to mysql:5.7 the connection is successful but it only works after the delay.Pluck
T
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
Tipton answered 22/5, 2019 at 12:0 Comment(12)
Yes I downgraded the database and it worked, however, I still have to maintain the delay before the connection like 10seconds before i connect to database, depends_on isn't that helpful, any ideas?Pluck
Just noticed you comment from the other postTipton
no error, it's working now after the degrade, just this delay with settimeout before i connect to db is a nuisance.Pluck
Remove the delay and try?Tipton
I have done many apps with express and mysql and didnt need any delay. Tell me if you need help with that or if you finally solve itTipton
without delay i get this error -> Error: connect ECONNREFUSED 172.18.0.2:3306, did you try connecting to mysql right away in the start script index.js or the connection happens when an http request comes in? From the interactive logs I observe that while mysql is still initializing, the docker eagerly kicks in nodejs container.Pluck
I think i have this situation mentioned in here. dev.to/hugodias/wait-for-mongodb-to-start-on-docker-3h8bPluck
Maybe you are starting the mysql server from Node.js (is that even possible?) ? I start the mysql server manually and then start the execution of Node.js. Not sure what setup you are usingTipton
Oh yes, looks like so! Not sure how to handle that honestly. I dont have much experience with docker. But I suppose docker should start the node app once the database is up somehowTipton
not starting from nodejs, it's docker's responsibility. Docker starts the mysql container and then moves onto nodejs right way while mysql taking sometime to initialize fully compared to nodejs which just executes and tries to create a connection. the whole process is docker based.Pluck
you should get into docker, save this link dev.to/hugodias/wait-for-mongodb-to-start-on-docker-3h8bPluck
I understand now, you are right about the docker responsability. Yes, Im starting with docker now and I find it wonderful! :)Tipton
T
0

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

Trapezium answered 3/6, 2021 at 12:33 Comment(0)
H
0

if you are using Docker Apple Silicon Preview with Mac m1. you probably find this error while trying to downgrade to 5.7 version

ERROR: no matching manifest for linux/arm64/v8 in the manifest list entries

event if you change platform to linux/x86_64 or amd64 it will be installed but same error with connection.

try to add new service with adminer image as in doc

db:
    image: mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: password

adminer:
    image: adminer
    restart: always
    ports:
      - 8080:8080

Finally open adminer service with http://localhost:8080 login with

server : db //which is mysql service name that you just created
username: // your username
password: // your password
Hurry answered 1/8, 2023 at 10:13 Comment(0)
C
0

I faced this issue when pulling the latest version of mysql from dockerhub. When I was pulling the 9.0.0 version, I was facing this error, but when I down graded the version to 8.0.33, this error was fixed for me

Corson answered 19/7 at 5:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.