node-mysql2
node-mysql2 copied to clipboard
Connection Timeout on ubuntu 22.04 (works fine on windows)
Hello,
I'm encountering a connection timeout issue on Ubuntu, the exact same code works flawlessly on Windows
I'm new to ExpressJS and am trying to create an API using mysql2
I have checked all firewalls, ports, etc., but I'm still facing this problem. i mean It's working on Windows for some reason but not on Ubuntu.
Node.js Version: v20.15.0 on both systems mysql2: v3.10.2 Database: MariaDB (connecting to the same database on both Windows and Ubuntu)
Error:
Listening on port: 4300
Error connecting to the database: Error: connect ETIMEDOUT
at PoolConnection._handleTimeoutError (/root/api/node_modules/mysql2/lib/connection.js:205:17)
at listOnTimeout (node:internal/timers:573:17)
at process.processTimers (node:internal/timers:514:7) {
errorno: 'ETIMEDOUT',
code: 'ETIMEDOUT',
syscall: 'connect',
fatal: true
}
Connection code
(async () => {
try {
const connection = await pool.getConnection();
console.log("Connected to the database");
connection.release();
} catch (err) {
console.error("Error connecting to the database:", err);
process.exit(1);
}
})();
database file
import dotenv from 'dotenv';
import mysql from 'mysql2/promise';
dotenv.config();
const pool = mysql.createPool({
host: process.env.HOST,
user: process.env.USER,
password: process.env.PASSWORD,
database: process.env.DATABASE
});
export default pool;
The code is straightforward, I don't understand why it will have problems on Ubuntu.
what is the transport ( TCP, named pipe, etc )? ( I assume TCP )
if the host is localhost - are you using localhost, 127.0.0.1 or : :1?
what is the transport ( TCP, named pipe, etc )? ( I assume TCP ) if the host is localhost - are you using
localhost,127.0.0.1or: :1?
I am using a public IP address. I have changed the default bind address to 0.0.0.0 in the MariaDB config file to allow connections from all sources., I have also used user@'%' when creating a user in MariaDB to allow connections from any IP address. I am also using AWS security groups to allow connections only from specific IP addresses. And yes, it's TCP.
I have also discovered that using dotenv to load MySQL login credentials from a .env file doesn't work in Ubuntu. However, if I put the credentials directly into the database file, it works.
So, I guess the problem is with dotenv? If that's the case, you can close this issue. again everything works fine in Windows.