App's DB connection timing out when trying to connect to Railway MySQL database

Aargon025/18/2023
This ticket can be closed

Hi, I'm trying to use MySQL with my Node.js (Express.js) app.

I'm not sure if I'm specifying the variables correctly, but they reflect correctly when I console.log them. My app involves creating a pool for my app to run queries in.

When it runs a query, it takes a few seconds before returning a Error: connect ETIMEDOUT error. Am I doing something wrong?
Aargon025/18/2023
95a2bc67-6c87-48fb-8d39-5d7dca258f75
IImLunaHey5/18/2023
Can you give an example of the code? Does it work correctly locally with a local copy of mysql?
Aargon025/18/2023
Yep, it works locally
Aargon025/18/2023
database.js
const mysql2 = require('mysql2/promise');

console.log(`Creating a MySQL pool with the following information:\nuser: ${process.env.DB_USER}\nhost: ${process.env.DB_HOST}\ndatabase: ${process.env.DB_DATABASE}`)
const pool = mysql2.createPool({
    user: process.env.DB_USER,
    password: process.env.DB_PASSWORD,
    host: process.env.DB_HOST,
    database: process.env.DB_DATABASE,
    connectionLimit: process.env.DB_CONNECTION_LIMIT,
    ssl: {
        rejectUnauthorized: false,
    },
});

// Monkey patch .query(...) method to console log all queries before executing it
// For debugging purpose
const oldQuery = pool.query;
pool.query = function (...args) {
    const [sql, params] = args;
    console.log(`EXECUTING QUERY`, sql, params)
    return oldQuery.apply(pool, args);
};

module.exports = pool;



This is where I run the query
const {query} = require('./database');

module.exports = {
    create_user: (customer) => {
        const { first_name, last_name, email, password } = customer;
        const sql = `INSERT INTO users(first_name, last_name, email, password) VALUES(?, ?, ?, ?)`
        return query(sql, [first_name, last_name, email, password])
    }
}


This is the stdout in my Deploy Logs:

EXECUTING QUERY INSERT INTO users(first_name, last_name, email, password) VALUES(?, ?, ?, ?) [ redacted ]
Error: connect ETIMEDOUT
at PromisePool.query (/app/node_modules/mysql2/promise.js:341:22)
at pool.query (/app/model/database.js:21:21)
at Object.create_user (/app/model/user.js:8:16)
at /app/router/user.js:13:10
at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
at next (/app/node_modules/express/lib/router/route.js:144:13)
at Route.dispatch (/app/node_modules/express/lib/router/route.js:114:3)
at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
at /app/node_modules/express/lib/router/index.js:284:15
at Function.process_params (/app/node_modules/express/lib/router/index.js:346:12) {
code: 'ETIMEDOUT',
errno: undefined,
sql: undefined,
sqlState: undefined,
sqlMessage: undefined
}
TThallesComH5/18/2023
i remember that there's also DB_PORT
Aargon025/18/2023
Oh god
Aargon025/18/2023
I swear if it's because I forgot about that variable
IImLunaHey5/18/2023
Does mysql2 not let you pass the url?
IImLunaHey5/18/2023
Avoids needing to pass each part by itself.
IImLunaHey5/18/2023
I'd just use MYSQL_URL
TThallesComH5/18/2023
yeah i would do the same, i hate using separate parts
Aargon025/18/2023
Project requirements 🥲
Aargon025/18/2023
Anyways turns out it was the port, when I forked it and worked on it I never notice it used the default port
Aargon025/18/2023
Edited it and now it works
Aargon025/18/2023
Thanks for the help! @ImLunaHey @ThallesComH