R
Railway•2y ago
Argon

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

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?
9 Replies
Percy
Percy•2y ago
Project ID: 95a2bc67-6c87-48fb-8d39-5d7dca258f75
Argon
ArgonOP•2y ago
95a2bc67-6c87-48fb-8d39-5d7dca258f75
luna
luna•2y ago
Can you give an example of the code? Does it work correctly locally with a local copy of mysql?
Argon
ArgonOP•2y ago
Yep, it works locally 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;
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])
}
}
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
}
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
}
ThallesComH
ThallesComH•2y ago
i remember that there's also DB_PORT
Argon
ArgonOP•2y ago
Oh god I swear if it's because I forgot about that variable
luna
luna•2y ago
https://docs.railway.app/databases/mysql Does mysql2 not let you pass the url? Avoids needing to pass each part by itself. I'd just use MYSQL_URL
ThallesComH
ThallesComH•2y ago
yeah i would do the same, i hate using separate parts
Argon
ArgonOP•2y ago
Project requirements 🥲 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 Thanks for the help! @ImLunaHey @ThallesComH
Want results from more Discord servers?
Add your server