#creating mysql db for nextjs app with prisma
1 messages · Page 1 of 1 (latest)
huh
Hey Brody, I was able to create the database after all. and tried to delete this post.
oh well glad you where able to figure that out, feel free to open a new help thread for any future questions
while I have you here, can I use the MYSQL_PRIVATE_URL as DATABASE_URL in my nextjs app and push my prisma schema into the empty db?
yes as long as you run the needed command in the context of your railway deployment, as the private url would not be accessible publicly
having your app use the private url would also eliminate any database <--> service egress fees!
Did I hear less fees? I like the sound of that! But where would you point a person in my position, prisma schema and nextjs app are ready, I have an empty mysql db on railway, what should i do at this point?
any docs on that?
but I also want people to access the database publicly , so that private url is not usable
the only docs for that would be the prisma docs, as theres nothing too fancy going on with railways mysql database, essentially you would use it the same way you are using planetscale's database
you may want to access to database publicly, and you can, but when you deploy your app to railway you should have your app use the private url
my app is on vercel. Can I use your database only, or am I doing it wrong?
yes you can still use the database, but if your app is hosted outside of railway it would not be possible to use the private url, you would need to use the public url and would be subjecting yourself to egress fees on the database service
what environment variable(s) does your app use to connect to the database?
just a database_url for planetscale
egress fees are a big deal? sounds like I should stay with planetscale
just 10¢ per GB, isn't planetscale shutting down their free or hobby plan?
yes..that's why I'm moving away
and if anything you should bring your app on vercel over to railway!!
nextjs apps run okay on railway?
they run great, they probably make for the biggest portion of languages deployed to railway
It would be helpful if you put out a guide on how to do the vercel+planetscale transfer to railway. Many people like me looking for a new home
Can you give me a checklist of what to do? Since I have a built app already, the template app you have is confusing. I have my project on github and push it to vercel
it's quite simple, with railway, you have your app on github (check) and push to github (check) then railway does the deploying, you'd just have to do the initial setup with railway, and once done you will be amazed on how easy it is
wanna begin?
sure
can you send a screenshot of your current railway project, the one with the database
oh, I only created a database there. Shouldn't I create sth else?
see, you know what's up. I just needed to see where we were at.
go ahead and add a new empty service from that + New button
done!
open it up and add all your needed environment variables into it's service variables
that'll take some time. I don't want to keep you waiting. What should i do after adding the env vars?
I'm kinda making it up as I go along from experience
may I ask why it would take a long time? lots of variables?
no, I guess I can do it in a min or two
what did you say your app's stack was? next?
do you have the database url variable added?
you said it's private. so should I?
The database should be accessible to the public via the app
I can add the planetscale one for now
for right now we would want to use the public database url for mysql on railway
the variable would be DATABASE_URL=${{MySQL.MYSQL_URL}} assuming the variable you use in code is DATABASE_URL
thats okay, we arent going to deploy the app just yet, we need to run the migrations and the seed you mentioned first
When I switched to get the db url, all the env vars in the service are gone. there was no save button.
have to put them back in
where you using the raw editor?
didnt click the update variables button?
yes, never mind. I thought that form was just for entry, but the editor data is supposed to persist and I deleted them when adding the db url
ah gotcha
did you use the variable syntax i gave you?
like this:?
MYSQL_URL=blahblah
DATABASE_URL=${{MySQL.MYSQL_URL}}
or just the second line
just the second line, i would guess you arent using MYSQL_URL in your app, so you wouldnt need it in the service variables
ok. done.
deploy the changes
then go back into the service and give it a nice name, and generate yourself a domain
name done, but generate a domain how
from its settings page
ah done
can i see another screenshot of the project?
with the railway.internal
that is not applicable for a next app in this context
quick question, how much data would you say you have in your ps database?
it's basically empty except for a table with 4300 rows and 5 cols that I created using a seed
haha im not sure if i would call that empty
what command did you run to create the tables and add the seed data?
as soon as I set it up prisma should be able to see it
seed it
in my schema.prisma I have:
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
relationMode = "prisma"
}
generator client {
provider = "prisma-client-js"
seed = "ts-node prisma/seed.ts"
}
and that seed.ts is executed to seed the db
right but you would need to run a command to migrate (make the tables) and seed the database, its unfortunately not gonna happen magically
yes, as i showed above it's a seed script that is run by prisma that gets the data from a .csv file
but what command do you run to start the migration and seed?
like npx prisma migrate deploy for example
okay cool, thats what i was looking for
can you go ahead and install the railway cli please https://docs.railway.app/guides/cli#installing-the-cli
done.
then make sure you are on the latest with railway --version latest being 3.5.2 iirc
perfect
don't know what iirc means
if i remember correctly
pardon my ignorance!
yes
for now, can you remove the DATABASE_URL line from it
done
we will have the railway cli inject the new database url when we run migrations and seeds
we also dont want to really be storing database credentials in a plaintext file
run railway login if you havent already
and then run railway link in a terminal open inside of your apps project folder, follow the steps to link to your cooli service
ok
let me know when thats done!
yeah, but what error did you get?
never mind. I should select my service as the service right? not the database..
for linking
correct
ok. seems like linking was done
are you in your apps project folder? the same folder you are pushing to github
yes
and what command did you say you use to run migrations?
my vercel app is connected to github, so to update the website, I just push to github. I don't do any migrations. sorry if I'm misunderstanding. I'm in business not web dev.
im talking in the context of database migrations
npx prisma db push
is this the correct command?
with db push, I let prisam use the schema to create the db
then you would want to run railway run npx prisma db push
done
did the command go well?
yes
and now run your seed command through railway run in the same way
it's running
do you own a custom domain thats in use with vercel?
yes
who is the domain with?
it's with Google domains. I need your DNS values I guess
you would need to use cloudflare's nameservers first
should i create a cloudflare account?
yes please, but we can hold off on that if you want, because you can test your app with the railway public domain for now
yes, I'll hold off on that. make sure things run smoothly first
but why cloudflare. They have to be my cdn?
because they support root level cnames, google domains does not
nope, the domain can stay on google domains, you would only need to use cloudflare's nameservers
theres a guide for that made by cloudflare
I've used them as a CDN before.
then you already have an account right? so thats good!
I see my tables replicated on your end
sweet
the seeding is still going on.
I need to connect my github project to railway somehow, right?
it was connected to vercel before
yeah but first, does the service have a domain?
I have added my domain names to vercel if that's what you mean. not sure what you mena
mean
right now im talking in the context of your railway service
I haven't adde a domain on railway yet
do you have railway public domain added to the service?
if not, go ahead and generate one, from its settings page
generated. shows 'nothing here yet' when I navigate to it
thats normal, there isnt anything there yet
Will i need the private networking or should i disable it
you dont need to touch that
does the service have all the needed variables for your app?
I believe so./
i guess we will soon find out eh?
moment of truth
do you have the correct github linked to your railway account?
I don't think so, railway has access to my github but I have not linked the proejct to it yet
go into the service settings and connect your repo
connected
and it will now deploy
I hope so, but the public url doesn't show anything yet: https://cooli-production.up.railway.app/
thats normal, nothing is there yet
that's the last bit, I guess. What should i do?
and the seed is still ongoing. taking longer than I thought
as long as the tables are there lol
It says my project has no deploys. Should I manually do it?
have you connected your github repo to the service?
yes. should i do a git push to initiate it?
is your stuff still blue?
if so, you would need to click the deploy button after you make any changes
just did it. it's building now.
awsome
deployment failed. could it be due to the seeding being underway?
RROR: failed to solve: process "/bin/bash -ol pipefail -c npm ci" did not complete successfully: exit code: 1
Error: Docker build failed
please send your full build logs using this https://bookmarklets.up.railway.app/log-downloader/
how? copy the contents of that page and paste here?
follow the 3 steps listed here
your lock file is out of sync, i guess vercel doesnt care about that or is fixing it for you.
to fix it properly run npm i --package-lock-only and then push your changes, once you push your changes railway will auto deploy the new code for you
I think I'd better disconnect from vercel too at this point
good idea!!
seeding done
awsome
new git push triggered deployment. but that package lock line generated this:
railway is serverfull right?
if serverfull is the opposite of serverless, yes
did you push your changes to github?
yes, the new deployment failed
with your package.json
^
would it be recreated if i delete the package-lock.json?
and run that npm i --package-lock-only
?
it's not registering any changes. git status shows an up to date branch after I run the above command.
are you pushing to the correct branch?
delete your lock file and run that command I gave you again
somehow you arent syncing your lock file
what if i delete my node_modules folder and do an npm install?
instead lets just tell railway to not care about that.
add this nixpacks.toml file to your project.
[phases.install]
cmds = ['npm install']
so I create a file named nixpacks.toml and add this to it in my root directory?
yes
good sign
yeah. no error. deployed.
database working
I guess all is set ! wow! I can't believe it!
I can't thank you enough
one last step
now that you have ran migrations and done the seeding, we want to switch your app over to use the private url for the database
your DATABASE_URL service variable should now be ${{MySQL.MYSQL_PRIVATE_URL}}
oh, ok
one you change that, click deploy and let ne know if anything fails
You mentioned sth about not keeping the .env values in a text file. What's the alternative? I have added it to my gitignore though.
and the new deploy failed:
okay i think i know what to do for this error
we will come back to this question
add a new service variable DATABASE_URL_DIRECT=${{MySQL.MYSQL_URL}} but this time hold your alt key while clicking the deploy button
holding the alt key stops it from doing a new deployment, since we dont want a new deployment just yet
done
under your url field, add another field named directUrl with its value set to env("DATABASE_URL_DIRECT")
I have these now:
DATABASE_URL=${{MySQL.MYSQL_PRIVATE_URL}}
DATABASE_URL_DIRECT=${{MySQL.MYSQL_URL}}
perfect
Hi
I have these now:
DATABASE_URL=${{MySQL.MYSQL_PRIVATE_URL}}
DATABASE_URL_DIRECT=${{MySQL.MYSQL_URL}}
Hy
Brody had to leave?
do you two know each other?
No, he just joined the chat!
uh strange
I thought he replace you
i dont know them
i dont know why they repeated something you said either, sorry about that
Isn't he a member of railway support?
nope
his role is "support access"
that just means they they can chat in #1006629907067064482 forums
same as you
and for transparency i dont work for railway
you are not railway support either, right?
correct
thanks for your help man. you were saying
always happy to help
did you make this change?
yes
DATABASE_URL=${{MySQL.MYSQL_PRIVATE_URL}}
DATABASE_URL_DIRECT=${{MySQL.MYSQL_URL}}
seems equivalent to what we had earlier:
DATABASE_URL=${{MySQL.MYSQL_URL}}
okay can you show me your new shema.prisma file
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
relationMode = "prisma"
}
generator client {
provider = "prisma-client-js"
seed = "ts-node prisma/seed.ts" //shouldn't this be .js(mar 15, 24) No!!! it worked as-is in deploying to railway
}
please make these changes
I thought you were talking about the env variables in railway settings. made the change in the schema file now
lets see the new schema file please
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
directUrl = env("DATABASE_URL_DIRECT") //added for railway
relationMode = "prisma"
}
generator client {
provider = "prisma-client-js"
seed = "ts-node prisma/seed.ts" //shouldn't this be .js(mar 15, 24) No!!! it worked as-is in deploying to railway
}
looks good, push the changes
should i have these in my railway env vars?
DATABASE_URL=${{MySQL.MYSQL_PRIVATE_URL}}
DATABASE_URL_DIRECT=${{MySQL.MYSQL_URL}}
I misunderstood you, but you were ok with these.
yeah, those are what should be in the service variables
but isn't this circumventing the private_url? when we add the public one?
from your schema.prisma file -
url = env("DATABASE_URL")
directUrl = env("DATABASE_URL_DIRECT") //added for railway
the directUrl is used for generation, migrations, and seeding and such, but url will be used for all other in app database calls
heres the prisma docs for that https://www.prisma.io/docs/orm/reference/prisma-schema-reference#fields
can you remove the postinstall script in your package.json?
it seems like the architecture of your app prevents you from using the private url since the private network is not available during build
you will have to go back to using the public url
DATABASE_URL=${{MySQL.MYSQL_URL}}
ah. ok. I reversed the changes.
i know railway but i unfortunately dont have enough experience with prisma to be able to advise on the needed changes you would have to make so that the public url is used at build and the private url is used during runtime
no worries. I'll look into that.
sounds good
about the .env file, you said there's a better way of storing that information?
I didn't understand why, cause the .env file is in the dev environment only.
do you have different stuff in there as opposed to what you have in railway?
if thats true, then you can get rid of the local .env and use railway run <your command here> as that will run the given command with all your railway service variables now available to the command you ran
its better because everything is stored securely in railway instead of a plaintext file
Thank you for all your help. I'm in a comfortable place to figure out the rest of the details.
awsome, but dont feel shy if you have any more questions
i assume your latest deployment is working again?
yes
great!
Hey, I got you some coffee. not much but a small token of appreciation. I hadn't realized you weren't railway support.
wow thank you so much, that means a lot ❤️
Have a good one.
you too!!
Hi @woven blaze About that custom domain with cloudflare, I added the two DNS records from cloudflare to my Google Domains DNS list and then added the CNAME @ root value from railway to cloudflare with a Full DNS setup. Am I missing something 'cause it's not working.
show me what you have put into your nameservers in Google domains please
Here they are @woven blaze
yeah that looks good, what is the status of your custom domain in your railway service settings?
Record not yet detected
okay, show me your dns settings in cloudflare
can i see the public networking section of your railway service too
hmmm, something is not quite right with your dns
yeah. after refreshing, this is what I'm seeing. so my setup must have been done correctly:
Thank you. I guess I'll have to deal with cloudflare now. Can you please delete the last image you shared, the less publicly shared the better.
no problem
should I use the same address from railway for this?
railway doesnt have anything to do mail servers
my www address resolves now. I changed my custom domain to www.blahblah.com but blahblah.com is not resolving. Do I have to upgrade my railway plan or add a redirect rule on cloudflare?
show me your current cloudflare dns settings please
and your domains in railway
you dont have the non www domain there
seems like I can either have www or the one without. I have to upgrade for more.
I understand, but it's expected.
i would have to disagree with that
but to have two domains you would need to be on hobby
nope, different names, different plans
realistically, If 50 people are using the website, what kind of bill should i be expecting? I access the website without any data in it and spent 10c so far. It seems to me I need to get back to planetscale and accept their new plan.
billing isnt based on how many users your site has, its based on how much resources your services use
more users = more resources, right? bandwidth , database calls, etc
that is true, there is a lose correlation
it looks like planetscale's cheapest plan is now 39$ a month, your next app and mysql database should never come near 39$ with light to moderate usage
I'll give it a go anyway. Will see how things evolve.
whats the current estimated costs say?
not sure. I'd better start using it and decide later. I already am on hobby plan.
well then go check lol https://railway.app/account/usage
great! thanks. Can this name be changed?