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

17 messages · Page 1 of 1 (latest)

inland bison
#

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?

hexed ledgeBOT
#

Project ID: 95a2bc67-6c87-48fb-8d39-5d7dca258f75

inland bison
#

95a2bc67-6c87-48fb-8d39-5d7dca258f75

peak monolith
#

Can you give an example of the code? Does it work correctly locally with a local copy of mysql?

inland bison
# peak monolith Can you give an example of the code? Does it work correctly locally with a local...

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
}
fossil rain
inland bison
#

I swear if it's because I forgot about that variable

peak monolith
#

Does mysql2 not let you pass the url?

#

Avoids needing to pass each part by itself.

#

I'd just use MYSQL_URL

fossil rain
#

yeah i would do the same, i hate using separate parts

inland bison
#

Anyways turns out it was the port, when I forked it and worked on it I never notice it used the default port

#

Edited it and now it works