Sequelize Connection Refused to MySQL in Dockerized Environment
Unanswered
Chub mackerel posted this in #help-forum
Chub mackerelOP
Hi everyone,
I’m facing an issue with Sequelize when trying to connect to my MySQL database in a Dockerized environment. Here’s a breakdown of the setup:
• Backend: Node.js using Sequelize ORM.
• Database: MySQL.
• Containers: I’m using Docker Compose to set up both the Node.js server and MySQL.
However, when I attempt to connect to the MySQL database, I keep getting a Connection Refused error from Sequelize. Here are some key details:
this is my .yaml
this is my node.js .env:
I’m facing an issue with Sequelize when trying to connect to my MySQL database in a Dockerized environment. Here’s a breakdown of the setup:
• Backend: Node.js using Sequelize ORM.
• Database: MySQL.
• Containers: I’m using Docker Compose to set up both the Node.js server and MySQL.
However, when I attempt to connect to the MySQL database, I keep getting a Connection Refused error from Sequelize. Here are some key details:
this is my .yaml
networks:
app-network:
driver: bridge
services:
backend:
networks:
- app-network
build:
context: ./server
dockerfile: dockerfile
ports:
- 8080:8080
expose:
- 8080
depends_on:
- tradingquizdb # Use the service name
environment:
- MYSQL_HOST=tradingquizdb # Use the service name
- MYSQL_USER=root
- MYSQL_PASSWORD=Mk@#1013
- MYSQL_DATABASE=trading_quiz
- MYSQL_PORT=3300
develop:
watch:
- path: ./server/package.json
action: rebuild
- path: ./server/package-lock.json
action: rebuild
- path: ./server
target: .
action: sync
tradingquizdb:
networks:
- app-network
image: mysql:latest
restart: unless-stopped
ports:
- 3307:3300
environment:
- MYSQL_ROOT_PASSWORD=Mk@#1013
- MYSQL_DATABASE=trading_quiz
volumes:
- db-data:/var/lib/mysql
volumes:
db-data:this is my node.js .env:
MYSQL_HOST=tradingquizdb
MYSQL_USER=root
MYSQL_PASSWORD="Mk@#1013"
MYSQL_DATABASE=trading_quiz
MYSQL_PORT=330020 Replies
@Chub mackerel Hi everyone,
I’m facing an issue with Sequelize when trying to connect to my MySQL database in a Dockerized environment. Here’s a breakdown of the setup:
• Backend: Node.js using Sequelize ORM.
• Database: MySQL.
• Containers: I’m using Docker Compose to set up both the Node.js server and MySQL.
However, when I attempt to connect to the MySQL database, I keep getting a Connection Refused error from Sequelize. Here are some key details:
this is my .yaml
networks:
app-network:
driver: bridge
services:
backend:
networks:
- app-network
build:
context: ./server
dockerfile: dockerfile
ports:
- 8080:8080
expose:
- 8080
depends_on:
- tradingquizdb # Use the service name
environment:
- MYSQL_HOST=tradingquizdb # Use the service name
- MYSQL_USER=root
- MYSQL_PASSWORD=Mk@#1013
- MYSQL_DATABASE=trading_quiz
- MYSQL_PORT=3300
develop:
watch:
- path: ./server/package.json
action: rebuild
- path: ./server/package-lock.json
action: rebuild
- path: ./server
target: .
action: sync
tradingquizdb:
networks:
- app-network
image: mysql:latest
restart: unless-stopped
ports:
- 3307:3300
environment:
- MYSQL_ROOT_PASSWORD=Mk@#1013
- MYSQL_DATABASE=trading_quiz
volumes:
- db-data:/var/lib/mysql
volumes:
db-data:
this is my node.js .env:
MYSQL_HOST=tradingquizdb
MYSQL_USER=root
MYSQL_PASSWORD="Mk@#1013"
MYSQL_DATABASE=trading_quiz
MYSQL_PORT=3300
if I am not wrong, the container itself starts first but the mysql service starts after sometime. You need to add a health check and wait until it resolves then start your backend
restart: unless-stopped
I added this to resolve this problem, but nothing change
I added this to resolve this problem, but nothing change
that
restart is to restart the container incase it fails@Chub mackerel how do that?
I have never done it with mysql container but with postgres
let me find something on internet
@@ts-ignore https://numericaideas.com/blog/docker-compose-nodejs-mysql/#docker-compose-spring-boot-and-mysql
Chub mackerelOP
Error response from daemon: Ports are not available: exposing port TCP 0.0.0.0:3306 -> 0.0.0.0:0: listen tcp 0.0.0.0:3306: bind: address already in use
I don't have any running container but get this error
I don't have any running container but get this error
should I change port?
something like 3307
something like 3307
you should check for all the services on your system and find which one is using this port
Chub mackerelOP
I delete every connection that I have in workbench, but still get this error
@@ts-ignore you should check for all the services on your system and find which one is using this port
Chub mackerelOP
should the environment var names in compose be same as var names in .env in project?
@@ts-ignore yes
Chub mackerelOP
ok
backend-1 | ConnectionRefusedError [SequelizeConnectionRefusedError]
backend-1 | at ConnectionManager.connect (/app/node_modules/sequelize/lib/dialects/mysql/connection-manager.js:92:17)
backend-1 | at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
backend-1 | at async ConnectionManager._connect (/app/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:222:24)
backend-1 | at async /app/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:174:32
backend-1 | at async ConnectionManager.getConnection (/app/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:197:7)
backend-1 | at async /app/node_modules/sequelize/lib/sequelize.js:305:26
backend-1 | at async MySQLQueryInterface.tableExists (/app/node_modules/sequelize/lib/dialects/abstract/query-interface.js:102:17)
backend-1 | at async user.sync (/app/node_modules/sequelize/lib/model.js:939:21)
backend-1 | at async Sequelize.sync (/app/node_modules/sequelize/lib/sequelize.js:377:9) {
backend-1 | parent: AggregateError [ECONNREFUSED]:
backend-1 | at internalConnectMultiple (node:net:1118:18)
backend-1 | at afterConnectMultiple (node:net:1685:7) {
backend-1 | code: 'ECONNREFUSED',
backend-1 | fatal: true,
backend-1 | [errors]: [ [Error], [Error] ]
backend-1 | },
backend-1 | original: AggregateError [ECONNREFUSED]:
backend-1 | at internalConnectMultiple (node:net:1118:18)
backend-1 | at afterConnectMultiple (node:net:1685:7) {
backend-1 | code: 'ECONNREFUSED',
backend-1 | fatal: true,
backend-1 | [errors]: [ [Error], [Error] ]
backend-1 | }
backend-1 | }got different error
@@ts-ignore https://stackoverflow.com/questions/29866133/cant-connect-to-mysql-with-sequelize
Chub mackerelOP
I did what he did on this post, but still get same error