After moving MySQL to metal, queries from NodeJS are 2-3x slower
christwardministry
HOBBYOP

7 months ago

Project ID: b855592c-9156-4020-9543-d41a4e939fde

I just moved both my NodeJS and MySQL to metal, and noticed queries are slower. I assume that's because NodeJS is hitting a public proxy to hit MySQL.

Trying to move to private network to talk to MySQL, but I can't seem to get the variables quite right, because now my NodeJS deploys with failures. I've tried port 3306 and 10797 (well, 3306 and RAILWAYTCPPROXY_PORT)

Can't reach database server at mysql.railway.internal:10797 (I've tried 3306 as well)

Inside my MySQL service,
MYSQLPRIVATEURL = mysql://${{MYSQLUSER}}:${{MYSQLROOTPASSWORD}}@${{RAILWAYPRIVATEDOMAIN}}:3306/${{MYSQLDATABASE}} MYSQLPORT = ${{RAILWAYTCPPROXYPORT}}

Inside my NodeJS app:
MYSQLURL = ${{MySQL.MYSQLPRIVATE_URL}}
MYSQLHOST = ${{MySQL.MYSQLHOST}}
MYSQLPORT = ${{MySQL.MYSQLPORT}}

Solved

0 Replies

christwardministry
HOBBYOP

7 months ago

[0] Can't reach database server at mysql.railway.internal:3306


brody
EMPLOYEE

7 months ago

do you get that error during build or deploy?


christwardministry
HOBBYOP

7 months ago

Deploy logs are empty. It's at the end of the Build logs, if I'm reading this correctly


brody
EMPLOYEE

7 months ago

the private network is not available during build, you would want to do any database actions during pre-deploy


christwardministry
HOBBYOP

7 months ago

Hmm, it's a SvelteKit NodeJS app; I wasn't intentionally doing anything during build.

Ahh, I see this in package.json:

"build": "prisma generate && npm run tailwind:build && npm run build:only",

christwardministry
HOBBYOP

7 months ago

I'm guessing prisma generate is doing something


brody
EMPLOYEE

7 months ago

does prisma generate create files on disk?


christwardministry
HOBBYOP

7 months ago

It might; not sure; it's an ORM, so it needs to parse a pseudo-database schema, which in turn helps with runtime database queries and VS Code autocomplete


brody
EMPLOYEE

7 months ago

try running it as a pre-deploy command


brody
EMPLOYEE

7 months ago


christwardministry
HOBBYOP

7 months ago

Thx, I'll read up


christwardministry
HOBBYOP

7 months ago

Looks like I should add a pre-deply command "prisma generate", and then remove prisma from my package.json?

"build": "<> npm run tailwind:build && npm run build:only",

brody
EMPLOYEE

7 months ago

sounds correct


christwardministry
HOBBYOP

7 months ago

Same error, even when I remove prisma entirely from pre-deploy step and package.json's build step.

This works if I use public network access to MySQL. Prisma isn't that unusual; I wonder if other people have managed to get this working? Are there recipes somewhere?


brody
EMPLOYEE

7 months ago

are you still getting the same error during build?


christwardministry
HOBBYOP

7 months ago

Yes. I wonder if there's a way to get prisma to use the public MySQL endpoints during build


brody
EMPLOYEE

7 months ago

I'd say just don't connect to the database during build, that's what the pre-deploy command is for


christwardministry
HOBBYOP

7 months ago

I guess I'm not sure how to prevent it.

I have a pre-deploy step now "npm run predeploy", and "railway up" sends package.json with these scripts:

"scripts": { "dev:only": "vite dev", "build:only": "vite build", "start": "sleep 2 && node build/index.js", "preview": "vite preview", "check": "svelte-kit sync && svelte-check --tsconfig ./jsconfig.json", "check:watch": "svelte-kit sync && svelte-check --tsconfig ./jsconfig.json --watch", "tailwind:watch": "cross-env TAILWIND_MODE=watch cross-env NODE_ENV=development postcss src/styles/tailwind.css -o src/styles/tailwind-output.css -w", "tailwind:build": "cross-env TAILWIND_MODE=build cross-env NODE_ENV=production postcss src/styles/tailwind.css -o src/styles/tailwind-output.css", "dev": "concurrently \"npm run dev:only\" \"npm run tailwind:watch\"", "build": "npm run tailwind:build && npm run build:only", "predeploy": "prisma generate", "migrate:dev": "prisma migrate dev", "migrate:deploy": "prisma migrate deploy", "migrate:status": "prisma migrate status", "generate": "prisma generate", "storybook": "storybook dev -p 6006", "build-storybook": "storybook build" },


christwardministry
HOBBYOP

7 months ago

OK, looks like Prisma is a chicken-and-egg: it reads the database so it can generate source code files inside node_modules/.prisma/client, which are then used by the app.

So it needs to run during the build step. I'll see what Grok says about workarounds


brody
EMPLOYEE

7 months ago

it needs to run during start (not pre-deploy) since it puts files on disk


christwardministry
HOBBYOP

7 months ago

The rest of the app won't compile without the source files it generates.

Does this workaround makes sense? https://station.railway.com/questions/private-networking-failing-to-provide-db-39a6d56e


brody
EMPLOYEE

7 months ago

then yes if you absolutely need the files during build you would have to use that proposed fix


christwardministry
HOBBYOP

7 months ago

OK, this change to package.json works, both when running from localhost as well as deploying to railway.

I'm not sure how to mark this thread as solved.

"scripts": {
"dev": "MYSQLURL=$MYSQLPUBLICURL; concurrently \"npm run dev:only\" \"npm run tailwind:watch\"", "build": "MYSQLURL=$MYSQLPUBLICURL; prisma generate && npm run tailwind:build && npm run build:only",
}



Status changed to Solved angelo-railway 7 months ago



Loading...