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
Project ID:
N/A
Project ID:
Please send your repo/the section of your code that attempts to connect to your db
in your
mysql.createConnection
just use a database url, much easier, since you left out the port variableI’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
can you show us your raw service variables, and make sure to hide anything sensitive please
I took a look at the mysql documentation and it left me more confused than before
this is still applicable
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
show me a screenshot of your railway service variables
^
those are the plugin variables, please show me the service variables
Sorry about that 😅
can you show me those in the raw editor, also hiding anything sensitive
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
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
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
no you weren't, please make sure you do not have that file in your repository
I have gitignore setup
and please show me the raw variable editor for your service, hiding anything important
For the sql service or the website service?
I'm not sure what you mean by raw variable editor?
for whatever service is using the database
Oh okay
see that raw editor button?
Ah
^
the mysql database, it is in the same railway project?
Yes. Is it not suppose to be setup that way?
Sorry this is my first time messing with back end stuff
nope that's good, that's how it should be
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?
delete all those, and add them back as reference variables
https://docs.railway.app/develop/variables#reference-variables
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..
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
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
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
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?
and actually, none of those are reference variables, please read the docs I linked
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?
idk let's see the raw variables
Is this correct?
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
will do
They are
atleast from what I see. My guess is to now put those values in my env file?
nope
Oh okay
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
I like the out of the box thinking, but that syntax is only for railway, you still have to use
process.env
in your codelol
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
and those are the wrong object values, youll want to have a look at this
https://github.com/mysqljs/mysql#connection-options
Yeah the way it was set up before the screenshot was simiilar
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
Done
and show me your new connection code once you've done that
Well nothings changed except I reverted the copy paste oh okay I saw your edit one sec
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
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
the object field names are correct, but you need to use the variable names from railway
Okay will do rn
I said the object field names are correct, you did the exact opposite of what I'd thought you'd do lol
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
host: process.env.MYSQLHOST
Oh okay
makes sense?
yessir
looks correct
do you have the railway cli installed?
yup
oh i see whats next
cause i tried literally everything
what's next?
use the cli to link the database then railway up?
if i remember correctly
nope, you deploy from github, no railway up
lol so why ask if i have the cli installed?
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?
Okay so Ima commit push this
yeah the websites live already
nope
oh okay i wont commit push this
slow your horses, we are tackling the .env thing, or the lack there of .env thing
Right I forgot about that
do you have the cli linked to your railway project and service from the folder you are currently in?
I can access it from the terminal on vscode which has the repo opened locally
not what I asked
do you have the railway cli installed
On my laptop yeah
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
I believe no
I have the cli installed and I can access it from the terminal but its not linked to the railway project
link it
now link a service
railway service
run
railway variables
and do you see all the database variables properly printed?Yessir
It's set up on a 2 column table and accurate
okay now are you starting to see how the cli can replace your .env file
Yeah I am actually
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?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
well go and use
railway run
first, gotta check that your code does workokay
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🥳🥳🥳
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
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
Thank you man I'll only be a noob for so much longer but I'm glad this final hurdle is done
awesome, best of luck!
(you can push to github now lol)
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.
I think I should make a new support thread or whatever its called I will if it's not fit to put it here
what is that chrome theme 💀
The dark one cause yeah lol
Dark-10
A browser theme that blends in with Win10's dark theme.
this is where it's at
off topic though lol
this is express right?
Correct
Honestly there's no passwords to hide I may as well temporarily make the repo public
But yeah its express
sounds good
Okay I'll do that and link the github link real quick
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.
10/10 readme
hahahahahaa
I'm confused
in this screenshot you ran
npm run start
but the package.json in that repo does not have a start script?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
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?
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
what's it set to
npm run start
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
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
but you don't have a start script in the package.json
you can't run npm run start without a start script
Yeah I noticed this odd situation awhile ago and kind of just ignored it
I can't ignore it
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
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
Okay
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 scenesI 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?
delete the node_modules folder from the github repo
https://www.toptal.com/developers/gitignore/api/node
then use that as your .gitignore file
where it says process.env.PORT?
yes! good catch!
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?
the contents of that link
in the gitignore file got it okay
yep
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
what kind of frontend is it?
tailwind css
yes lol but what framework
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?
for the frontend? no react, Vue, etc?
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
very good take
how are you serving the frontend?
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
oh I see now, not the most performant but definitely adequate for a beginner
Yeah I'm not gonna learn C or anything but I can't start with a serverless react project lmao
oh no that's definitely not what I was getting at
Oh okay
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
True I'll learn more as I go on
sounds good!!
I wish you luck in your development adventures
Appreciate your help again Ima go take care of these bugs now 🙏🏽 peace
peace