DEV Community

jzfrank
jzfrank

Posted on

Set up a local mysql server on Mac with prisma

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

Image description

Image description

Choose community version

Image description

Choose community server

Image description

download the version matching your Operation system

Image description

Click No thanks, just start my downloads

Install mysql

Image description

Image description

Image description

Image description

Image description

Image description

Create a new Database

Finish install guide.

Now, open terminal and type

mysql -u root
Enter fullscreen mode Exit fullscreen mode

hit enter, you should see:

Image description

Now, create a database with commands CREATE DATABASES, here, we use myFirstDB

Image description

Check if database is created successfully: SHOW DATABASES

Image description

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")
}
Enter fullscreen mode Exit fullscreen mode

In .env file, add

DATABASE_URL="mysql://root@localhost:3306/myFirstDB" 
Enter fullscreen mode Exit fullscreen mode

Now you are good to go! To check if it actually works, open the database with sql db visualization tools like Beekeeper Studio:

Image description
click import from url and

Image description

connect

Image description

Now, you see no table, which is expected.

Image description

To test, we add a dummy schema to schema.prisma

model User {
  id   String @id @default(cuid())
  name String
}
Enter fullscreen mode Exit fullscreen mode

run

npx prisma migrate dev --name init
Enter fullscreen mode Exit fullscreen mode

in command line, this should generate db schema for you.

Check Beekeeper studio

Image description

We have successfully initialized the local database with prisma!

That's all for today, happy hacking!

Top comments (0)