Set up a local mysql server on Mac with prisma
Published: 2023-10-21 14:00:22

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x78qqa7xvcwhityokbc9.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5dhb3crfai1swfnsgzur.png)

Choose community version 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dpgnmb8bi4qjdbugdzag.png)

Choose community server


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3jzlccpb8fdm9e93js8y.png)

download the version matching your Operation system 



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aols4ze8p5khyyyo97d0.png)

Click `No thanks, just start my downloads` 

### Install mysql 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3ya4dvy24o7p83zs51nx.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8btsucd19la0zxjio6g0.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xyb8m25hf8stq8ndk24l.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q2s218g8rf3oztrsidl5.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hiics7wpb6dtfpw6tuvl.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pvavkb39gko42pjyc5dx.png)

### Create a new Database 

Finish install guide. 

Now, open terminal and type 

```
mysql -u root
```

hit enter, you should see: 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l3nneurm49rixxnrmgih.png)


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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y0xnuxzisismcbiw6aqm.png)

Check if database is created successfully: `SHOW DATABASES`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2nfc0gqfay49iigsufcw.png)


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](https://www.prisma.io/docs/getting-started/setup-prisma/start-from-scratch/relational-databases-typescript-mysql) 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`: 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1hrk0fuybua86hwwneo6.png)
click `import from url` and 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4rfsihkxy52eex7hr3cy.png)

connect


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/93c6kchx7zwx0hv479sw.png)


Now, you see no table, which is expected. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2t102zcrwutlrnvflqr6.png)

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`


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/evjuh526j7i7zc4hb1ta.png)

We have successfully initialized the local database with prisma! 

That's all for today, happy hacking!