Timed out error connecting backend to MySQL database

Hey guys I'm at the last hurdle for this website if anyone could help I'd really appreciate it https://stackoverflow.com/questions/77220612/timedout-error-while-trying-connect-backend-to-railway-mysql-database
Stack Overflow
TimedOut Error while trying connect backend to railway MySQL database
Hey so basically I'm having trouble connecting my backend to a new mySQL database I created on railway. I have it as a service connected to a website I have deployed using github. I'm going to drop...
162 Replies
Percy
Percy14mo ago
Project ID: N/A
Wannabe SWE
Wannabe SWE14mo ago
Project ID:
bd779870-1b99-4af3-b0da-20a63fb58772
bd779870-1b99-4af3-b0da-20a63fb58772
Adam
Adam14mo ago
Please send your repo/the section of your code that attempts to connect to your db
Brody
Brody14mo ago
in your mysql.createConnection just use a database url, much easier, since you left out the port variable
Wannabe SWE
Wannabe SWE14mo ago
I’m not exactly sure how to do that I was looking for examples online I’m kind of a noob with back end stuff right now but I’ll check out mysqls documentation later and see if I can make that happen I believe it’s on the post but I’ll post the specific snippet in here once I get home
Brody
Brody14mo ago
can you show us your raw service variables, and make sure to hide anything sensitive please
Wannabe SWE
Wannabe SWE14mo ago
const connection = mysql.createConnection({
host: process.env.HOST,
user: process.env.USER,
password: process.env.PASSWORD,
database: process.env.DATABASE,
});

connection.connect((err) => {
if (err) {
console.error('Error connecting to mySQL:', err);
return;
} else {
console.log('Connected to mySQL database');
}
});
const connection = mysql.createConnection({
host: process.env.HOST,
user: process.env.USER,
password: process.env.PASSWORD,
database: process.env.DATABASE,
});

connection.connect((err) => {
if (err) {
console.error('Error connecting to mySQL:', err);
return;
} else {
console.log('Connected to mySQL database');
}
});
HOST = containers-us-west-71.railway.app
USER = root
PASSWORD = (some SHA password)
DATABASE = railway
PORT = 5???
HOST = containers-us-west-71.railway.app
USER = root
PASSWORD = (some SHA password)
DATABASE = railway
PORT = 5???
I took a look at the mysql documentation and it left me more confused than before
Brody
Brody14mo ago
this is still applicable
Wannabe SWE
Wannabe SWE14mo ago
Okay I’ll try it out I'm still very much a beginner so I can't figure out what the property name is for the createConnection function. I got this error and from guess attempting. I'm trying to figure it out right now though
Ignoring invalid configuration option passed to Connection: url. This is currently a warning, but in future versions of MySQL2, an error will be thrown if you pass an invalid configuration option to a Connection

Server is running on port 7698

Error connecting to mySQL: Error: connect ETIMEDOUT

at Connection._handleTimeoutError (/app/node_modules/mysql2/lib/connection.js:205:17)

at listOnTimeout (node:internal/timers:569:17)

at process.processTimers (node:internal/timers:512:7) {

errorno: 'ETIMEDOUT',

code: 'ETIMEDOUT',

syscall: 'connect',

fatal: true

}
Ignoring invalid configuration option passed to Connection: url. This is currently a warning, but in future versions of MySQL2, an error will be thrown if you pass an invalid configuration option to a Connection

Server is running on port 7698

Error connecting to mySQL: Error: connect ETIMEDOUT

at Connection._handleTimeoutError (/app/node_modules/mysql2/lib/connection.js:205:17)

at listOnTimeout (node:internal/timers:569:17)

at process.processTimers (node:internal/timers:512:7) {

errorno: 'ETIMEDOUT',

code: 'ETIMEDOUT',

syscall: 'connect',

fatal: true

}
const connection = mysql.createConnection({
host: process.env.HOST,
user: process.env.USER,
password: process.env.PASSWORD,
database: process.env.DATABASE,
url: process.env.URL
});
const connection = mysql.createConnection({
host: process.env.HOST,
user: process.env.USER,
password: process.env.PASSWORD,
database: process.env.DATABASE,
url: process.env.URL
});
Brody
Brody14mo ago
show me a screenshot of your railway service variables
Wannabe SWE
Wannabe SWE14mo ago
No description
Wannabe SWE
Wannabe SWE14mo ago
^
Brody
Brody14mo ago
those are the plugin variables, please show me the service variables
Wannabe SWE
Wannabe SWE14mo ago
Sorry about that 😅
No description
Brody
Brody14mo ago
can you show me those in the raw editor, also hiding anything sensitive
Wannabe SWE
Wannabe SWE14mo ago
by the way I used the plugin variables in my server.js file not the service variables in the env file was this my mistake? you got it
Brody
Brody14mo ago
yes, but it is not the only mistake, so let's take this one thing at a time so we can come to a total solution
Wannabe SWE
Wannabe SWE14mo ago
No description
Wannabe SWE
Wannabe SWE14mo ago
This is the env file it has the plugin variables from the sql service. I'm guessing I was suppose to put in the service variables from the website service but I haven't changed anything yet
Brody
Brody14mo ago
no you weren't, please make sure you do not have that file in your repository
Wannabe SWE
Wannabe SWE14mo ago
I have gitignore setup
Brody
Brody14mo ago
and please show me the raw variable editor for your service, hiding anything important
Wannabe SWE
Wannabe SWE14mo ago
For the sql service or the website service? I'm not sure what you mean by raw variable editor?
Brody
Brody14mo ago
for whatever service is using the database
Wannabe SWE
Wannabe SWE14mo ago
Oh okay
Brody
Brody14mo ago
see that raw editor button?
Wannabe SWE
Wannabe SWE14mo ago
Ah
Brody
Brody14mo ago
^
Wannabe SWE
Wannabe SWE14mo ago
No description
Brody
Brody14mo ago
the mysql database, it is in the same railway project?
Wannabe SWE
Wannabe SWE14mo ago
Yes. Is it not suppose to be setup that way? Sorry this is my first time messing with back end stuff
Brody
Brody14mo ago
nope that's good, that's how it should be
Wannabe SWE
Wannabe SWE14mo ago
Oh okay well the env file has the plugin vars from the sql service so should I be replacing them with the service vars from the website service?
Brody
Brody14mo ago
delete all those, and add them back as reference variables https://docs.railway.app/develop/variables#reference-variables
Wannabe SWE
Wannabe SWE14mo ago
Oh okay Hey I was wondering does the password matter? Because I basically lost it when I editted the image and I'm just realizing this..
Brody
Brody14mo ago
wdym lost it you should use reference variables, please read the docs page I linked and then show me the raw variables again when done please
Wannabe SWE
Wannabe SWE14mo ago
Basically I started deleting to put them back in as ref vars but then I realized the photo was edited with the password omitted so I just made my own password. The rest I did turn into ref vars and I got the same error again
No description
No description
Brody
Brody14mo ago
don't worry about the error yet, I knew it still wouldn't work and you can't just make up your own password, the password needs to be a reference too
Wannabe SWE
Wannabe SWE14mo ago
I'm fine I'm just sweating the lost SHA password 😂 Well it's set up like a ref var but how do i reset a password for the website service?
Brody
Brody14mo ago
and actually, none of those are reference variables, please read the docs I linked
Wannabe SWE
Wannabe SWE14mo ago
Okay I'll take a look again my bad I believe I did it correctly this time. It's basically what the dropdown shows for the value in accordance to what was set as the var name right? Which was also just a dropdown selection?
Brody
Brody14mo ago
idk let's see the raw variables
Wannabe SWE
Wannabe SWE14mo ago
No description
Wannabe SWE
Wannabe SWE14mo ago
Is this correct?
Brody
Brody14mo ago
okay I think that looks good, now you need to go and click the little eye icon on each of those variables to make sure they are displaying their proper values
Wannabe SWE
Wannabe SWE14mo ago
will do They are atleast from what I see. My guess is to now put those values in my env file?
Brody
Brody14mo ago
nope
Wannabe SWE
Wannabe SWE14mo ago
Oh okay
Brody
Brody14mo ago
and actually railway makes .env files obsolete but that's a bit out of the scope of this particular help thread now in your mysql.createconnection thing, use all those environment variables show me the code before you push it please
Wannabe SWE
Wannabe SWE14mo ago
No description
Brody
Brody14mo ago
I like the out of the box thinking, but that syntax is only for railway, you still have to use process.env in your code
Wannabe SWE
Wannabe SWE14mo ago
lol okay so i will use that but what do I put in the env file then? i was thinking this was the wrong teh second i screenshotted it
Brody
Brody14mo ago
and those are the wrong object values, youll want to have a look at this https://github.com/mysqljs/mysql#connection-options
Wannabe SWE
Wannabe SWE14mo ago
Yeah the way it was set up before the screenshot was simiilar
Brody
Brody14mo ago
okay you've changed my mind, let's talk about how railway makes your .env file obsolete correct, the way you had it before was more correct, but we needed to tackle the reference variables or else you would run into issues down the road we will come back to the .env file topic in a bit, for now, delete your .env file
Wannabe SWE
Wannabe SWE14mo ago
Done
Brody
Brody14mo ago
and show me your new connection code once you've done that
Wannabe SWE
Wannabe SWE14mo ago
Well nothings changed except I reverted the copy paste oh okay I saw your edit one sec
Wannabe SWE
Wannabe SWE14mo ago
No description
Brody
Brody14mo ago
you are not using all the needed variables, you must use all the database variables you had in railway with the exception of the database url variable, you don't need to use that
Wannabe SWE
Wannabe SWE14mo ago
No description
Wannabe SWE
Wannabe SWE14mo ago
It was actually at the top of my file as a separate const variable I just threw it in oh shit hold up i gotta delete the semi colon
Brody
Brody14mo ago
the object field names are correct, but you need to use the variable names from railway
Wannabe SWE
Wannabe SWE14mo ago
Okay will do rn
Wannabe SWE
Wannabe SWE14mo ago
No description
Brody
Brody14mo ago
I said the object field names are correct, you did the exact opposite of what I'd thought you'd do lol
Wannabe SWE
Wannabe SWE14mo ago
Oh so change the values not the properties? with the values from the ref vars on railway? wouldnt that expose the sensitive info if my repo was public? its private btw I'm sorry I'm literally a noob except for front end stuff not including js
Brody
Brody14mo ago
host: process.env.MYSQLHOST
Wannabe SWE
Wannabe SWE14mo ago
Oh okay
Brody
Brody14mo ago
makes sense?
Wannabe SWE
Wannabe SWE14mo ago
yessir
Wannabe SWE
Wannabe SWE14mo ago
No description
Brody
Brody14mo ago
looks correct do you have the railway cli installed?
Wannabe SWE
Wannabe SWE14mo ago
yup oh i see whats next cause i tried literally everything
Brody
Brody14mo ago
what's next?
Wannabe SWE
Wannabe SWE14mo ago
use the cli to link the database then railway up? if i remember correctly
Brody
Brody14mo ago
nope, you deploy from github, no railway up
Wannabe SWE
Wannabe SWE14mo ago
lol so why ask if i have the cli installed?
Brody
Brody14mo ago
well something comes next, but it's not what you said lol do you have your project linked to the service we have been adding reference variables to?
Wannabe SWE
Wannabe SWE14mo ago
Okay so Ima commit push this yeah the websites live already
Brody
Brody14mo ago
nope
Wannabe SWE
Wannabe SWE14mo ago
oh okay i wont commit push this
Brody
Brody14mo ago
slow your horses, we are tackling the .env thing, or the lack there of .env thing
Wannabe SWE
Wannabe SWE14mo ago
Right I forgot about that
Brody
Brody14mo ago
do you have the cli linked to your railway project and service from the folder you are currently in?
Wannabe SWE
Wannabe SWE14mo ago
I can access it from the terminal on vscode which has the repo opened locally
Brody
Brody14mo ago
not what I asked do you have the railway cli installed
Wannabe SWE
Wannabe SWE14mo ago
On my laptop yeah
Brody
Brody14mo ago
do you have the cli linked to your railway project and service from the folder you are currently in? this has nothing to do with github
Wannabe SWE
Wannabe SWE14mo ago
I believe no I have the cli installed and I can access it from the terminal but its not linked to the railway project
Brody
Brody14mo ago
link it
Wannabe SWE
Wannabe SWE14mo ago
No description
Brody
Brody14mo ago
now link a service railway service
Wannabe SWE
Wannabe SWE14mo ago
No description
Brody
Brody14mo ago
run railway variables and do you see all the database variables properly printed?
Wannabe SWE
Wannabe SWE14mo ago
Yessir It's set up on a 2 column table and accurate
Brody
Brody14mo ago
okay now are you starting to see how the cli can replace your .env file
Wannabe SWE
Wannabe SWE14mo ago
Yeah I am actually
Brody
Brody14mo ago
the final touch is using railway run <the command you normally use to start your app locally> this will run your app with the environment variables available, no variables will be written to disk see how that's better than a plaintext file with your database credentials in it?
Wannabe SWE
Wannabe SWE14mo ago
Yeah it's a smoother process maybe longer but smoother. Should I push the changes I made to github first or just leave as is? Actually its only longer cause this is the first time I'm doing it lol
Brody
Brody14mo ago
well go and use railway run first, gotta check that your code does work
Wannabe SWE
Wannabe SWE14mo ago
okay
Brody
Brody14mo ago
yes exactly I'd like to note that if the cli ever tells you to do railway up do not do it, ignore that message, that message is a bug and should never be printed
Wannabe SWE
Wannabe SWE14mo ago
No description
Brody
Brody14mo ago
🥳🥳🥳
Wannabe SWE
Wannabe SWE14mo ago
Thank you so much man there's no way I would've been able to pull this off I had no idea about the ref vars THANK YOU! Cheers mate
Brody
Brody14mo ago
yeah I told the team they need better docs on that, reference variables catch even experienced devs off guard, so don't feel bad for not understanding
Wannabe SWE
Wannabe SWE14mo ago
Thank you man I'll only be a noob for so much longer but I'm glad this final hurdle is done
Brody
Brody14mo ago
awesome, best of luck! (you can push to github now lol)
Wannabe SWE
Wannabe SWE14mo ago
Hey I'm sorry for bothering again but I don't get any error messages in the deploy log I actually get a success message about the database making a connection yet the server fails to load the website now.
No description
No description
No description
Wannabe SWE
Wannabe SWE14mo ago
I think I should make a new support thread or whatever its called I will if it's not fit to put it here
Brody
Brody14mo ago
what is that chrome theme 💀
Wannabe SWE
Wannabe SWE14mo ago
The dark one cause yeah lol
Brody
Brody14mo ago
Brody
Brody14mo ago
this is where it's at off topic though lol this is express right?
Wannabe SWE
Wannabe SWE14mo ago
Correct Honestly there's no passwords to hide I may as well temporarily make the repo public But yeah its express
Brody
Brody14mo ago
sounds good
Wannabe SWE
Wannabe SWE14mo ago
Okay I'll do that and link the github link real quick
Wannabe SWE
Wannabe SWE14mo ago
I gotta update the readme but this is it https://github.com/hiphiphorhayy/montoyamasonry
GitHub
GitHub - hiphiphorhayy/montoyamasonry
Contribute to hiphiphorhayy/montoyamasonry development by creating an account on GitHub.
Brody
Brody14mo ago
10/10 readme
Wannabe SWE
Wannabe SWE14mo ago
hahahahahaa
Brody
Brody14mo ago
I'm confused in this screenshot you ran npm run start but the package.json in that repo does not have a start script?
Wannabe SWE
Wannabe SWE14mo ago
Yeah I'm confused about that too honestly Also I have no idea if this new problem is big or tiny cause it just doesnt give an error msg
Brody
Brody14mo ago
and railway would error out by default if you don't have a start script in your package.json check the service settings, do you have a start command set in there?
Wannabe SWE
Wannabe SWE14mo ago
It's worked for a couple days now I have npm run start in the settings for the website service on railway as the start command
Brody
Brody14mo ago
what's it set to
Wannabe SWE
Wannabe SWE14mo ago
npm run start
Brody
Brody14mo ago
but you don't have a start script in your package.json are you sure this is the same repo that your service is set to deploy from
Wannabe SWE
Wannabe SWE14mo ago
Yeah I think I'm gonna make it I believe it starts up the express server I'm 100% sure If the pages could atleast load you could see for yourself
Brody
Brody14mo ago
but you don't have a start script in the package.json you can't run npm run start without a start script
Wannabe SWE
Wannabe SWE14mo ago
Yeah I noticed this odd situation awhile ago and kind of just ignored it
Brody
Brody14mo ago
I can't ignore it
Wannabe SWE
Wannabe SWE14mo ago
No I know it's an issue now I can make a start script but not sure exactly what the value would be. I believe it would be node server.js
Brody
Brody14mo ago
that's correct do that, then remove the start command from the service settings make sure you click the checkmark when you remove the start command
Wannabe SWE
Wannabe SWE14mo ago
Okay
Brody
Brody14mo ago
https://github.com/hiphiphorhayy/montoyamasonry/blob/master/server.js#L57 use process.env.PORT || 3000 instead you do not need to add a PORT service variable yourself since railway makes one for you behind the scenes
Wannabe SWE
Wannabe SWE14mo ago
I deleted it in railway settings and now I'm gonna commit push the addition of it in my package json file Oh okay I'll do that too then commit push Same change for the console log under what you highlighted?
Brody
Brody14mo ago
delete the node_modules folder from the github repo https://www.toptal.com/developers/gitignore/api/node then use that as your .gitignore file
Wannabe SWE
Wannabe SWE14mo ago
where it says process.env.PORT?
Brody
Brody14mo ago
yes! good catch!
Wannabe SWE
Wannabe SWE14mo ago
Wait so that link I copy paste it into the gitignore file or put it in a type of file then link the git ignore file to that file?
Brody
Brody14mo ago
the contents of that link
Wannabe SWE
Wannabe SWE14mo ago
in the gitignore file got it okay
Brody
Brody14mo ago
yep
Wannabe SWE
Wannabe SWE14mo ago
Okay I'm boutta push this to the repo Thanks again it works now I just have to deal with some bugs on the front end with the contact form but I got somewhat far on my own so Ima try to tackle them on my own
Brody
Brody14mo ago
what kind of frontend is it?
Wannabe SWE
Wannabe SWE14mo ago
tailwind css
Brody
Brody14mo ago
yes lol but what framework
Wannabe SWE
Wannabe SWE14mo ago
Just small things like before I had the index.js file with a fetch method to send form data to the back end so it can be sent as an automated email but i quit that for a database approach because i couldn't figure it out and didn't want to deal with azure (outlook email) but the code stayed the same so i gotta update it for this database along with the app.post method i gotta update that too a bit none just tailwind css, express, node?
Brody
Brody14mo ago
for the frontend? no react, Vue, etc?
Wannabe SWE
Wannabe SWE14mo ago
Nope I kind of want to learn vanilla so I know whats going on cause things are abstracted as it is and I'm still learning
Brody
Brody14mo ago
very good take how are you serving the frontend?
Wannabe SWE
Wannabe SWE14mo ago
express static middleware I believe I sort of don't know what I'm saying just saying lol It's in the repo if you want to look
Brody
Brody14mo ago
oh I see now, not the most performant but definitely adequate for a beginner
Wannabe SWE
Wannabe SWE14mo ago
Yeah I'm not gonna learn C or anything but I can't start with a serverless react project lmao
Brody
Brody14mo ago
oh no that's definitely not what I was getting at
Wannabe SWE
Wannabe SWE14mo ago
Oh okay
Brody
Brody14mo ago
I'm talking about just serving your frontend with an actual web server and not express middleware, but don't worry about that the middleware is perfectly fine for now
Wannabe SWE
Wannabe SWE14mo ago
True I'll learn more as I go on
Brody
Brody14mo ago
sounds good!! I wish you luck in your development adventures
Wannabe SWE
Wannabe SWE14mo ago
Appreciate your help again Ima go take care of these bugs now 🙏🏽 peace
Brody
Brody14mo ago
peace
Want results from more Discord servers?
Add your server