0

I'm trying to connect express to mysql that running in docker

FYI. both of them running in docker

It works when express runs in my local but not work when trying to run in docker

error code from docker : error connecting: Error: connect ECONNREFUSED 127.0.0.1:3306

app.js

var mysql = require('mysql')
var connection = mysql.createConnection({
  host: '127.0.0.1',
  user: 'admini',
  password: 'admini',
  database: 'cooper',
  port:'3306',
  connectionLimit: 10
})

docker-compose.yaml

services:

  db:
    image: mysql
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: example
    ports: 
      - 3306:3306

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

  mongo:
    image: mongo
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example

  mongo-express:
    image: mongo-express
    restart: always
    ports:
      - 8081:8081
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: root
      ME_CONFIG_MONGODB_ADMINPASSWORD: example
      ME_CONFIG_MONGODB_URL: mongodb://root:example@mongo:27017/

Marcello Romani
  • 2,967
  • 31
  • 40

1 Answers1

0

Try switching host from 127.0.0.1 to db (the name you set in the docker-compose) when trying to connect from other docker containers.