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.
networks:
- app-tier
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 } }
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With