mysql is one of the most popular dialects of SQL. And prisma is an ORM tool that helps developer interact with database safer and better.
In this post, we will discuss 1. how to set up a local mysql server 2. how to use it with prisma
Set up a local mysql server
Download mysql
Go to mysql website and choose downloads
Choose community version
Choose community server
download the version matching your Operation system
Click No thanks, just start my downloads
Install mysql
Create a new Database
Finish install guide.
Now, open terminal and type
mysql -u root
hit enter, you should see:
Now, create a database with commands CREATE DATABASES
, here, we use myFirstDB
Check if database is created successfully: SHOW DATABASES
Now that we have successfully created the database, it's time to connect to it with prisma
Connect to DB with prisma
I assume you have already installed prisma in your project. If not, go to setup-prisma and set it up.
Now, what remains is to connect to our local database.
mysql connection string should be of format:
mysql://USER:PASSWORD@HOST:PORT/DATABASE
3306 is the default port. In our case, we don't have a password, we are using root
, connect to db myFirstDB
. So the connection string is simply mysql://root@localhost:3306/myFirstDB
In schema, prisma, for datasource, use
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
}
In .env
file, add
DATABASE_URL="mysql://root@localhost:3306/myFirstDB"
Now you are good to go! To check if it actually works, open the database with sql db visualization tools like Beekeeper Studio
:
connect
Now, you see no table, which is expected.
To test, we add a dummy schema to schema.prisma
model User {
id String @id @default(cuid())
name String
}
run
npx prisma migrate dev --name init
in command line, this should generate db schema for you.
Check Beekeeper studio
We have successfully initialized the local database with prisma!
That's all for today, happy hacking!
Top comments (0)