Connected to the db and then "ECONNREFUSED"
Hi I am using a QNAP NAS to Dockerize nodejs server and mariadb. Versions are node 22.17.1 and mariadb 11.8.2.
I am using sequelize and mariadb connector.
These are the relevant scripts:
> server.js
```const express = require('express');
const { connectDB } = require('./config/db');
........
const app = express();
// Connect Database
connectDB();
// Init Middleware
app.use(express.json({ extended: false }));
app.use(cors());
// Define Routes
app.use(............);
syncModels().then(() => {
const HTTP_PORT = process.env.PORT_LOCAL;
const HTTPS_PORT = process.env.PORT_SSL;
// Start HTTP Server
http.createServer(app).listen(HTTP_PORT, () => {
.............
});
});
```
> db.js
```const { Sequelize } = require('sequelize');
require('dotenv').config();
console.log(process.env.DB_NAME);
const sequelize = new Sequelize(process.env.DB_NAME, process.env.DB_USER, process.env.DB_PASS, {
host: process.env.DB_HOST,
port: process.env.DB_PORT,
dialect: process.env.DB_DIALECT,
logging: false,
pool: {
max: 10,
min: 0,
acquire: 20000,
idle: 10000
},
dialectOptions: {
connectTimeout: 5000
}
});
const connectDB = async (retries = 5, delay = 2000) => {
let attempts = 0;
while (attempts < retries) {
try {
await sequelize.authenticate();
console.log('✅ MySQL conectado exitosamente');
return;
} catch (err) {
attempts++;
console.error(`❌ Fallo al conectar a la base de datos (intento ${attempts}/${retries}): ${err.message}`);
if (attempts < retries) {
console.log(`Reintentando en ${delay / 1000} segundos...`);
await new Promise(res => setTimeout(res, delay));
} else {
if (process.env.NODE_ENV !== 'production') {
console.error(err); // Muestra stacktrace completo en desarrollo
}
process.exit(1);
}
}
}
};
module.exports = { connectDB, sequelize };
```
The dependencies are these:
```
"bcryptjs": "2.4.3",
"cors": "2.8.5",
"dotenv": "16.6.1",
"express": "4.21.2",
"jsonwebtoken": "9.0.2",
"mariadb": "2.5.6",
"sequelize": "6.32.1"
```
The issue is that when I start the server it starts normally, few seconds later the connection starts to crashing and I am getting "ECONNREFUSED" or "EHOSTUNREACH". I have checked to have connection to the db with DBeaver, successfully and I made net ping from nodejs db docker and successfully too.
Logs first connection OK:
```
16 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
___________________
✅ MySQL conectado exitosamente
Database & tables sync!
HTTP Server started on port 5001
HTTPS Server started on port 5000
/app/node_modules/sequelize/lib/dialects/mariadb/connection-manager.js:87
throw new SequelizeErrors.ConnectionRefusedError(err);
^
ConnectionRefusedError [SequelizeConnectionRefusedError]: connect ECONNREFUSED 192.168.0.40:3306
at ConnectionManager.connect (/app/node_modules/sequelize/lib/dialects/mariadb/connection-manager.js:87:17)
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
at async ConnectionManager._connect (/app/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:222:24) {
parent: Error: connect ECONNREFUSED 192.168.0.40:3306
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1637:16)
From event:
at _registerHandshakeCmd (/app/node_modules/mariadb/lib/connection.js:745:11)
at /app/node_modules/mariadb/lib/connection.js:57:11
at new Promise (<anonymous>)
at Connection.connect (/app/node_modules/mariadb/lib/connection.js:56:16)
at Object.createConnection (/app/node_modules/mariadb/promise.js:31:36)
at ConnectionManager.connect (/app/node_modules/sequelize/lib/dialects/mariadb/connection-manager.js:71:41)
at ConnectionManager._connect (/app/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:222:61)
at process.processTicksAndRejections (node:internal/process/task_queues:105:5) {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '192.168.0.40',
port: 3306,
fatal: true
},
original: Error: connect ECONNREFUSED 192.168.0.40:3306
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1637:16)
From event:
at _registerHandshakeCmd (/app/node_modules/mariadb/lib/connection.js:745:11)
at /app/node_modules/mariadb/lib/connection.js:57:11
at new Promise (<anonymous>)
at Connection.connect (/app/node_modules/mariadb/lib/connection.js:56:16)
at Object.createConnection (/app/node_modules/mariadb/promise.js:31:36)
at ConnectionManager.connect (/app/node_modules/sequelize/lib/dialects/mariadb/connection-manager.js:71:41)
at ConnectionManager._connect (/app/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:222:61)
at process.processTicksAndRejections (node:internal/process/task_queues:105:5) {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '192.168.0.40',
port: 3306,
fatal: true
}
}
```
Logs attempting restart the server after the log above:
```
16 packages are looking for funding
run `npm fund` for details
___________________
❌ Fallo al conectar a la base de datos (intento 1/5): connect EHOSTUNREACH 192.168.0.40:3306
Reintentando en 2 segundos...
Error syncing models: connect EHOSTUNREACH 192.168.0.40:3306
HTTP Server started on port 5001
HTTPS Server started on port 5000
```
MariaDB Docker Logs:
```
2025-07-29 11:59:11+00:00 [Note] [Entrypoint]: Entrypoint script for MariaDB Server 1:11.8.2+maria~ubu2404 started.
2025-07-29 11:59:12+00:00 [Warn] [Entrypoint]: /sys/fs/cgroup/ame=systemd:/docker/866a4a7a682c7dee0e7c335aba8a89eda3fc08aba595cb3fd1d84936759c0f1d
8:pids:/docker/866a4a7a682c7dee0e7c335aba8a89eda3fc08aba595cb3fd1d84936759c0f1d
7:freezer:/docker/866a4a7a682c7dee0e7c335aba8a89eda3fc08aba595cb3fd1d84936759c0f1d
6:devices:/docker/866a4a7a682c7dee0e7c335aba8a89eda3fc08aba595cb3fd1d84936759c0f1d
5:blkio:/docker/866a4a7a682c7dee0e7c335aba8a89eda3fc08aba595cb3fd1d84936759c0f1d
4:cpuacct:/docker/866a4a7a682c7dee0e7c335aba8a89eda3fc08aba595cb3fd1d84936759c0f1d
3:cpuset:/docker/866a4a7a682c7dee0e7c335aba8a89eda3fc08aba595cb3fd1d84936759c0f1d
2:cpu:/docker/866a4a7a682c7dee0e7c335aba8a89eda3fc08aba595cb3fd1d84936759c0f1d
1:memory:/docker/866a4a7a682c7dee0e7c335aba8a89eda3fc08aba595cb3fd1d84936759c0f1d/memory.pressure not writable, functionality unavailable to MariaDB
2025-07-29 11:59:12+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2025-07-29 11:59:12+00:00 [Note] [Entrypoint]: Entrypoint script for MariaDB Server 1:11.8.2+maria~ubu2404 started.
2025-07-29 11:59:12+00:00 [Note] [Entrypoint]: MariaDB upgrade not required
2025-07-29 11:59:12 0 [Note] Starting MariaDB 11.8.2-MariaDB-ubu2404 source revision 8d36cafe4fc700e6e577d5a36650c58707e76b92 server_uid C4AU8loddRTPBoRpCKIQ79a15go= as process 1
2025-07-29 11:59:13 0 [Note] InnoDB: Compressed tables use zlib 1.3
2025-07-29 11:59:13 0 [Note] InnoDB: Number of transaction pools: 1
2025-07-29 11:59:13 0 [Note] InnoDB: Using crc32 + pclmulqdq instructions
2025-07-29 11:59:13 0 [Note] mariadbd: O_TMPFILE is not supported on /tmp (disabling future attempts)
2025-07-29 11:59:13 0 [Warning] mariadbd: io_uring_queue_init() failed with EPERM: sysctl kernel.io_uring_disabled has the value 2, or 1 and the user of the process is not a member of sysctl kernel.io_uring_group. (see man 2 io_uring_setup).
2025-07-29 11:59:13 0 [Warning] InnoDB: liburing disabled: falling back to innodb_use_native_aio=OFF
2025-07-29 11:59:13 0 [Note] InnoDB: innodb_buffer_pool_size_max=128m, innodb_buffer_pool_size=128m
2025-07-29 11:59:13 0 [Note] InnoDB: Completed initialization of buffer pool
2025-07-29 11:59:13 0 [Note] InnoDB: File system buffers for log disabled (block size=4096 bytes)
2025-07-29 11:59:13 0 [Note] InnoDB: End of log at LSN=1859592
2025-07-29 11:59:13 0 [Note] InnoDB: Opened 3 undo tablespaces
2025-07-29 11:59:13 0 [Note] InnoDB: 128 rollback segments in 3 undo tablespaces are active.
2025-07-29 11:59:13 0 [Note] InnoDB: Setting file './ibtmp1' size to 12.000MiB. Physically writing the file full; Please wait ...
2025-07-29 11:59:13 0 [Note] InnoDB: File './ibtmp1' size is now 12.000MiB.
2025-07-29 11:59:13 0 [Note] InnoDB: log sequence number 1859592; transaction id 88
2025-07-29 11:59:13 0 [Note] Plugin 'FEEDBACK' is disabled.
2025-07-29 11:59:13 0 [Note] Plugin 'wsrep-provider' is disabled.
2025-07-29 11:59:13 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
2025-07-29 11:59:14 0 [Note] InnoDB: Buffer pool(s) load completed at 250729 11:59:14
2025-07-29 11:59:22 0 [Note] Server socket created on IP: '0.0.0.0'.
2025-07-29 11:59:22 0 [Note] Server socket created on IP: '::'.
2025-07-29 11:59:22 0 [Note] mariadbd: Event Scheduler: Loaded 0 events
2025-07-29 11:59:22 0 [Note] mariadbd: ready for connections.
Version: '11.8.2-MariaDB-ubu2404' socket: '/run/mysqld/mysqld.sock' port: 3306 mariadb.org binary distribution
```
I don't know if the cause is comming by MariaDB, the connector, sequelize...
Anybody can help me please?
关闭于 2025-07-29 0 条评论