11

I am using sequelize, MySQL and docker together for the first time and cannot get sequelize to connect to the db.

I have tested the presence of the DB with DBeaver and I can connect. I can also see the MySQL container respond in the terminal when I connect via DBeaver. However, when attempting to connect with code I get an ECONNREFUSED error.

I have checked my code with the sequelize docs and double checked port settings but cannot seem to see where I am going wrong.

After the correct answer was provided below I noticed that I had a bug. I missed the following from the 'mysql' container in the docker-compose.yml file.

networks:
    - app-tier

To avoid requiring other users to read the comments in the answer to discover this, I have added this to the docker-compose.yml file so that the question and answer now align more clearly

docker-compose.yml file:

version: '3'

networks:
    app-tier:
        driver: bridge
services:
    server:
        image: bitnami/node
        networks:
        - app-tier
        command: "sh -c 'npm install && npm run dev'"
        volumes:
        - ./server:/app
        ports:
        - 5000:5000
        depends_on:
        - mysql
    mysql:
        image: 'bitnami/mysql:latest'
        environment:
        - MYSQL_USER=root
        - MYSQL_PASSWORD=password
        - MYSQL_ROOT_PASSWORD=password
        - MYSQL_DATABASE=school
        networks:
            - app-tier
        ports:
        - '3306:3306'
        volumes:
        - ./db:/bitnami/mysql/data

Sequelize connection:

const Sequelize = require('sequelize');

const sequelize = new Sequelize('school', 'root', 'password', {
dialect: 'mysql'
});

module.exports = sequelize;

Attempt to sync from app.js:

sequelize.sync()
    .then(result => console.log(result))
    .catch(err => console.log('EEEERRRROOOOOOR',err));

Terminal output:

server_1    |  { SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:3306
server_1    |     at Utils.Promise.tap.then.catch.err (/app/node_modules/sequelize/lib/dialects/mysql/connection-manager.js:139:19)
server_1    |     at tryCatcher (/app/node_modules/bluebird/js/release/util.js:16:23)
server_1    |     at Promise._settlePromiseFromHandler (/app/node_modules/bluebird/js/release/promise.js:512:31)
server_1    |     at Promise._settlePromise (/app/node_modules/bluebird/js/release/promise.js:569:18)
server_1    |     at Promise._settlePromise0 (/app/node_modules/bluebird/js/release/promise.js:614:10)
server_1    |     at Promise._settlePromises (/app/node_modules/bluebird/js/release/promise.js:689:18)
server_1    |     at Async._drainQueue (/app/node_modules/bluebird/js/release/async.js:133:16)
server_1    |     at Async._drainQueues (/app/node_modules/bluebird/js/release/async.js:143:10)
server_1    |     at Immediate.Async.drainQueues (/app/node_modules/bluebird/js/release/async.js:17:14)
server_1    |     at runCallback (timers.js:810:20)
server_1    |     at tryOnImmediate (timers.js:768:5)
server_1    |     at processImmediate [as _immediateCallback] (timers.js:745:5)
server_1    |   name: 'SequelizeConnectionRefusedError',
server_1    |   parent:
server_1    |    { Error: connect ECONNREFUSED 127.0.0.1:3306
server_1    |     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1191:14)
server_1    |      errno: 'ECONNREFUSED',
server_1    |      code: 'ECONNREFUSED',
server_1    |      syscall: 'connect',
server_1    |      address: '127.0.0.1',
server_1    |      port: 3306,
server_1    |      fatal: true },
server_1    |   original:
server_1    |    { Error: connect ECONNREFUSED 127.0.0.1:3306
server_1    |     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1191:14)
server_1    |      errno: 'ECONNREFUSED',
server_1    |      code: 'ECONNREFUSED',
server_1    |      syscall: 'connect',
server_1    |      address: '127.0.0.1',
server_1    |      port: 3306,
server_1    |      fatal: true } }

2 Answers 2

9

To connect to your mysql server from a docker container you need to pass a proper service address. So your initialization should look like this.

const Sequelize = require('sequelize');

const sequelize = new Sequelize('school', 'root', 'password', {
host: 'mysql',
dialect: 'mysql'
});

module.exports = sequelize;
4
  • Thanks for this. Error now changed from ECONNREFUSED to ETIMEDOUT. I thought docker mapped host to localhost no? Feb 11, 2019 at 21:37
  • 1
    It maps to your machine localhost if you expose port, but inside containers it got it own addresses. So now you get ETIMEDOUT because mysql is not ready to recieve connection. depend_on is waiting when container will start, not when finish setup. So you need to add in your app check when mysql is ready
    – ttomalak
    Feb 11, 2019 at 21:43
  • OK, so of course you are right. Having rechecked my docker-file I missed networks: - app-tier I forgot to add it to the named network hence it did not work on the first fix. Thanks for your help! Feb 11, 2019 at 22:09
  • 1
    Oh, I saw your app-tier declared in first service and I assumed that is also declared in mysql service to share network. Good that you resolved that!
    – ttomalak
    Feb 11, 2019 at 22:14
0

Work In Progress I'm fighting against similar issue, this is my steps for static addressing:

networks:
  internal_UJJigBWx:
    driver: bridge
    ipam:
      config:
      - subnet: 172.16.238.0/24
      driver: default
services:
  express:
    build:
      context: .
    depends_on:
    - sql
    environment:
      DB_DATABASE: sequebase
      DB_DIALECT: mysql
      DB_HOST: 172.16.238.2
      DB_PASSWORD: sequeword
      DB_USERNAME: sequeuser
    networks:
      internal_UJJigBWx:
        ipv4_address: 172.16.238.3
    ports:
    - 80:3000/tcp
  sql:
    environment:
      MYSQL_DATABASE: sequebase
      MYSQL_PASSWORD: sequeword
      MYSQL_RANDOM_ROOT_PASSWORD: "yes"
      MYSQL_USER: sequeuser
    image: bitnami/mysql:latest
    networks:
      internal_UJJigBWx:
        ipv4_address: 172.16.238.2
version: '3.0'

The main difference is the static addressing with ipam . You can use docker-compose config to check and see env expansions.

I'm using gulp tasks into the build stage to call sequelize sync. It's working well locally but I'm not sure if is a good approach to docker compose

The build image process use args rather than environment. and that phase looks like to happen before the database spinup. I'm don't know how to wait the sql to sync the sequelize...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.