5 months ago
Hello, apologies for the trouble but I'm a new Railway user trying to connect a Node/Express API to a MySQL DB via Prisma, but the service always says
Error: P1001: Can't reach database server at `mysql.railway.internal:3306`
I've deployed the API service from my github repository, and created a MySQL service running in the same project. I believe the DATABASE_URL
variable is the relevant one.
When I run the project locally it works fine and the value is
DATABASE_URL="mysql://root:@localhost:3306/db_name"
And in the deployed environment I set it using a reference
${{MySQL.MYSQL_URL}}
which evaluates tomysql://root:P........O@mysql.railway.internal:3306/railway
When I try to hit an API endpoint that interacts with the DB I get a 500 error, so using the Railway CLI I used railway shell
and ran this command with this output:
bash-3.2$ npx prisma db push
Environment variables loaded from .env
Prisma schema loaded from prisma/schema
Datasource "db": MySQL database "railway" at "mysql.railway.internal:3306"
Error: P1001: Can't reach database server at mysql.railway.internal:3306
If anyone can see the mistake I'm making I'd really appreciate any help! Thank you
7 Replies
5 months ago
I saw a similar thread that recommended adding a 3-second sleep delay when starting the express server, so I tried deploying this change but unfortunately I get the same error
5 months ago
railway shell
runs on your device, and thus doesn't have access to the private network. I'd recommend either running migrations when you app starts (and adding a healthcheck) or adding a TCP proxy to your database and using that public URL for your local migration command.
5 months ago
Thanks very much Aleks, I didn't realize it wasn't executing inside the deployed service itself. I've tried the command railway run npx prisma generate
which seems to succeed.
Do you know of any documentation that would help point out how to add that step to the automated build/deploy flow? Thanks
5 months ago
You want
db push
instead ofgenerate
, you were right there originally -generate
just generates the JS library & TS types, it doesn't actually do anything to the database.Take a look at the Nixpacks Node start command docs for more info on where you can put things to have them run.
5 months ago
Thank you Aleks, I appreciate the help, but unfortunately I'm seeing the same error inside the build step itself now. (I also changed the db to PostgreSQL in case that was related to the issue somehow)
I changed my build script in package.json
to include the database commands, and now the builds fail with this output
#12 9.015 Datasource "db": PostgreSQL database "railway", schema "public" at "postgres.railway.internal:5432"
#12 9.177 Error: P1001: Can't reach database server at postgres.railway.internal:5432
#12 9.177 Please make sure your database server is running at postgres.railway.internal:5432
.
I understand why I couldn't connect to the remote database from my local machine, but I don't understand why these services can't seem to see eachother. Is this possibly related to me still being on a "Trial" plan? Thanks
5 months ago
Sorry if I was unclear - the build environment isn't attached to the private network either. You're going to want to run your migrations when your app starts and add a healthcheck to wait until they've finished.
5 months ago
Ah okay no problem. Is there an automated way to trigger those migrations inside the Railways framework then? It looks to me like nixpacks just handles the build step. I see the ability to add a custom start command, but that doesn't seem like it (and a healthcheck seems like the step after the migrations are run).
Thanks and sorry for all the questions