R
Railway

✋|help

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

AArgon5/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?
AArgon5/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?
AArgon5/18/2023
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
}
TThallesComH5/18/2023
i remember that there's also DB_PORT
AArgon5/18/2023
Oh god I swear if it's because I forgot about that variable
IImLunaHey5/18/2023
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
TThallesComH5/18/2023
yeah i would do the same, i hate using separate parts
AArgon5/18/2023
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

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
How to use custom domain for PR environments?I'd like to use a custom domain and subdomains for PR environments - how to configure? Where does tHow to use custom domain for PR environments?I'd like to use a custom domain and subdomains for PR environments - how to configure? Where does tFile questionIs there any way for files generated by the app to persist? We currently have an app where the authUpgrading from developer to team plan?Hi there. I'm not seeing a way to upgrade from the developer plan to the team plan? Does this featurHello, i have a Django app and i want to deploy using Unirn, but y have this error:============== Using Nixpacks ============== context: 75d7645dc46f8909d7e9b5b5b8bb166f Nixpacks builDiscord bot crashed with no logsHi I have a problem Project ID: 0ea7d87c-dd6d-4b48-ad73-b4dcec1390ea (bad) code: https://github.com/Dual-purpose post: proxies & ultraviolet banned?@joeyygffffdf here's a threadWhat data does railway record for people hitting their proxy?I'm writing up a privacy policy for my site that'll be hosted on railway and just realised that therRailway TermsHello, there! I am using railway to develop an inventory application for the church I attend, is thigetEnv in Nim not returning valueAttempting to read environment variables from a project using Nim doesn't seem to be working... ``Verify your accountWhy am I now seeing this when I try to create a new project? I have a developer planProblem on my front end login.Hi guys. I have problems on my front end when i try login on my application. I put user and passwordfailing to redeployHello, is anyone else having issues with deployment? I am failing to redeploy projects that were sucInstall FFmpegI have a project in railway and I'm hosting a **Django** app. For the purpose of my project, I need RAILWAY_{serviceName}_STATIC_URLI have a service in the same project named `mpc-api` - how do I set the env variable for another serPostgres ErrorHey, I have a Django project that uses a Postgres DataBase and I'm hosting those at Railway site, imPostgres ErrorHey, I have a Django project that uses a Postgres DataBase and I'm hosting those at Railway site, imHow to configure wildcard for PR environments?I've created a wildcard domain for the project and configured it correctly and it's passing correctlNext.js - "Cannot find module 'x'... " ONLY during build stageHi all, I have a Next.js yarn monolith project. I'm trying to deploy on Railway. The error I'm keeDefined railway.toml / railway.json file in sudirectoryMy usecase is that I'm using monorepo where root path is the root of monorepo / and not sub-director