Can't connect database to deployed service

anky131997
TRIAL

6 months ago

I'm new in Railway. I'm trying to use the free tier to deploy my small Laravel Project. I deployed my GitHub repo. I created a MySQL service, but I can't connect it to my repo. I added a reference variable MYSQLURL = ${{ MySQL.MYSQLURL }} to connect the database. But when I'm trying to deploy the variable
NIXPACKSBUILDCMD="
composer install &&
npm install --production &&
php artisan optimize &&
php artisan config:cache &&
php artisan route:cache &&
php artisan view:cache &&
php artisan migrate --force &&
php artisan db:seed
"
It's not working. Build is failing. And when I'm trying to use CMD to run "php artisan migrate" it's showing the error -- SQLSTATE[HY000] [2002] phpnetworkgetaddresses: getaddrinfo for mysql.railway.internal failed: No such host is known. (Connection: mysql, SQL: select exists (select 1 from informationschema.tables where tableschema = 'railway' and tablename = 'migrations' and tabletype
in ('BASE TABLE', 'SYSTEM VERSIONED')) as exists)

can anyone advise me what to do? I'm having a hardtime figuring it out.

Solved

0 Replies

anky131997
TRIAL

6 months ago

273d71ca-77e9-4819-a0cf-fe0d616f008a


6 months ago

Hey, you can't use the private network during build. You have a few options:

  1. perform migrations and other database operations during runtime,

  2. use public network during build and private network during runtime
    or

  3. enable the new builder in your service settings which allows private network usage during build


anky131997
TRIAL

6 months ago

I'm confused since I'm new at this, how do I use public network during build and private network during runtime? What exactly do I have to change?


6 months ago

honestly I think in your case the cleanest solution would be just to use the new builder


6 months ago

just need to toggle this

1322156270043664400


anky131997
TRIAL

6 months ago

Thanks. Let me try this.


anky131997
TRIAL

6 months ago

Well I tried it. The build process was running for a long time, then it failed and now it's showing this error.

1322161975589077000


6 months ago

hm! I guess it's a good thing it's still in beta huh


6 months ago

in that case i'd recommend just performing migrations during runtime for now 🤣


anky131997
TRIAL

6 months ago

I don't know how to do that actually🥹 💀


6 months ago

just do all this

php artisan optimize &&
php artisan config:cache && 
php artisan route:cache && 
php artisan view:cache &&
php artisan migrate --force &&
php artisan db:seed

in your start command instead of your build command


anky131997
TRIAL

6 months ago

Aye aye captain🫡


anky131997
TRIAL

6 months ago

Yup that worked it's deployed


6 months ago

Awesome!!! Glad you got it working


anky131997
TRIAL

6 months ago

Yeah it's deployed but it's showing this error. I'm now trying to figure out what exactly is going on. Although I don't see any error in deploy logs. They are clean.

1322175238922043400


anky131997
TRIAL

6 months ago

In the documentation it's said, maybe the target port is set to some other port. How do I figure out which port is the right port?


6 months ago

the right port is defined in the PORT environment variable that Railway automatically injects into your environment


anky131997
TRIAL

6 months ago

So it's not an issue with PORT. There are actually no issues with deployment. The deploy logs are clean. I'm getting the errors in HTTP logs

1322177634523676700


6 months ago

that's the HTTP logs you see when you're not listening on the correct port


anky131997
TRIAL

6 months ago

any idea where to get the right port? I have no idea where you get the right one.


6 months ago

yea, it's defined in the PORT environment variable


anky131997
TRIAL

6 months ago

Yeah I can't find that 💀


6 months ago

it's an environment variable so you can't see it anywhere, you need to access it within your code


6 months ago

I don't use PHP but from what I can tell, you'd need to do this

php artisan serve --host=0.0.0.0 --port=$PORT

anky131997
TRIAL

6 months ago

Alright let me check


anky131997
TRIAL

6 months ago

Thanks a lot for help brother you've been a big help


6 months ago

no problem at all


anky131997
TRIAL

6 months ago

well I did find the port, it's 8080. I switched the port. But it's still not working and showing the error.


6 months ago

you need to use the start command I gave you because I think it serves on 127.0.0.1 by default and it should serve on 0.0.0.0


anky131997
TRIAL

6 months ago

Fixed it


anky131997
TRIAL

6 months ago

It's not showing Application failed to respond anymore, but it's showing 500 Server error. I think I have given some wrong environment variables. Let me check.


6 months ago

awesome, yea i don't think Railway returns a 500 error ever so it's probably an issue with your app there


anky131997
TRIAL

6 months ago

to connect the database with the service, this ( MYSQLURL = ${{ MySQL.MYSQLURL }} ) reference variable works right?


6 months ago

yea, MYSQL_URL=${{MySQL.MYSQL_URL}} should work fine


6 months ago

artisan serve is a development server 🙂


6 months ago

oh, i didn't know that, my bad - i don't use php 😭
will come back to this thread when I'm awake again (going to bed gn)


anky131997
TRIAL

6 months ago

Hi there. I'm still having trouble with this. I configured everything as they are supposed to be (Or I think I did atleast). The "Application failed to respond" error is gone. But error 500-server error still persists. I checked Laravel Logs and it's showing it can't find the mysql.railway.internal host. I believe I configured the ENV variables correctly, here are they ---
APPNAME="SBStudios" APPENV="production"
APPKEY="base64:3D2UHHZ9OoEu/ZC9spxyFaZodtdzbdG3taphCg1W/Ak=" APPDEBUG="false"
APPTIMEZONE="UTC" APPURL="http://sbstudios-production.up.railway.app"
APPLOCALE="en" APPFALLBACKLOCALE="en" APPFAKERLOCALE="enUS"
APPMAINTENANCEDRIVER="file"
PHPCLISERVERWORKERS="4" BCRYPTROUNDS="12"
LOGCHANNEL="stack" LOGSTACK="single"
LOGDEPRECATIONSCHANNEL="null"
LOGLEVEL="debug" DBCONNECTION="mysql"
DBHOST="mysql.railway.internal" DBPORT="3306"
DBDATABASE="railway" DBUSERNAME="root"
DBPASSWORD="zXMGfzVvjgqzkeiheiusQvFOVSiVnCIv" SESSIONDRIVER="database"
SESSIONLIFETIME="120" SESSIONENCRYPT="false"
SESSIONPATH="/" SESSIONDOMAIN="null"
BROADCASTCONNECTION="log" FILESYSTEMDISK="local"
QUEUECONNECTION="database" CACHESTORE="database"
CACHEPREFIX="" MEMCACHEDHOST="127.0.0.1"
REDISCLIENT="phpredis" REDISHOST="127.0.0.1"
REDISPASSWORD="null" REDISPORT="6379"
MAILMAILER="smtp" MAILHOST="smtp.gmail.com"
MAILPORT="465" MAILUSERNAME="springbreakstudio@gmail.com"
MAILPASSWORD="zeoasteyrgstfadw" MAILENCRYPTION="tls"
MAILFROMADDRESS="springbreakstudio@gmail.com"
MAILFROMNAME="${APPNAME}" AWSACCESSKEYID=""
AWSSECRETACCESSKEY="" AWSDEFAULTREGION="us-east-1" AWSBUCKET=""
AWSUSEPATHSTYLEENDPOINT="false"
VITEAPPNAME="${APPNAME}" MYSQLURL="${{MySQL.MYSQLURL}}" NIXPACKSBUILD_CMD="
composer install --optimize-autoloader --no-dev &&
npm install --production
"

Kindly help me understand what exactly I'm doing wrong here? It will be a big help.


6 months ago

I don't think it's smart sharing your environment variables like that, it has things like your database password and api keys


6 months ago

that 500 error is an in-app error, it's NextJS's default error page, you should check logs and see what the issue is


maddsua
HOBBY

6 months ago

Nice, somebody for sure has made a copy of your db credentials now


6 months ago

I'll just delete it myself


anky131997
TRIAL

6 months ago

My bad


anky131997
TRIAL

6 months ago

Yeah I was AFK


anky131997
TRIAL

6 months ago

I did check log, I believe it's problem with the database. I can't seem to connect it


maddsua
HOBBY

6 months ago

Too late it's already in my telegram channel


maddsua
HOBBY

6 months ago

(kidding)


maddsua
HOBBY

6 months ago

Or am I 🤨


maddsua
HOBBY

6 months ago

Wait


maddsua
HOBBY

6 months ago

Bro even had their SMTP credentials there


maddsua
HOBBY

6 months ago

💀


maddsua
HOBBY

6 months ago

@AnKY change all the passwords IMMEDIATELY


maddsua
HOBBY

6 months ago

All of the secrets that you had exposed in that message


maddsua
HOBBY

6 months ago

ASAP


anky131997
TRIAL

6 months ago

I'm new to this dude. I didn't know how these things work. Well I'm still trying to resolve the issue I'm facing.


6 months ago

change your exposed passwords + keys first, then we’ll solve your problem. Madd didn’t intend to be rude, but leaking your keys is very bad


anky131997
TRIAL

6 months ago

No no it's fine I didn't find it rude. I did a dumb mistake I know. I actually panicked and nuked the whole project. I'm starting deploying from scratch.


6 months ago

@AnKY Am I clear to mark as solved? Your original question was answered after all
If you run into trouble again you'd of course be welcome to open another thread 🙂


anky131997
TRIAL

6 months ago

Sure mark it as resolved. I'm starting from scratch anyways. If I run into problem again I'll reach out.


6 months ago

Awesome, good luck!!!!


6 months ago

!s


Status changed to Solved dev 6 months ago


Can't connect database to deployed service - Railway Help Station