Skip to content

Commit 82e81fc

Browse files
committed
docs(database): add switch form PostgreSQL to MySQL
1 parent f922f78 commit 82e81fc

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

docs/database.md

+103
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
- [Max connections](#max-connections)
2525
- [Performance optimization (MongoDB + Mongoose)](#performance-optimization-mongodb--mongoose)
2626
- [Design schema](#design-schema)
27+
- [Switch PostgreSQL to MySQL](#switch-postgresql-to-mysql)
2728

2829
---
2930

@@ -291,6 +292,108 @@ Designing schema for MongoDB is completely different from designing schema for r
291292
1. [MongoDB Schema Design Anti-Patterns](https://www.mongodb.com/developer/products/mongodb/schema-design-anti-pattern-massive-arrays)
292293
1. [MongoDB Schema Design Best Practices](https://www.mongodb.com/developer/products/mongodb/mongodb-schema-design-best-practices/)
293294
295+
## Switch PostgreSQL to MySQL
296+
297+
If you want to use MySQL instead of `PostgreSQL`, you can make the changes after following the complete guide given [here](installing-and-running.md).
298+
299+
Once you have completed all the steps, you should have a running app.
300+
![image](https://github.com/user-attachments/assets/ec60b61a-65e6-43e2-9bcf-72dad4c8a9fa)
301+
302+
If you've made it this far, it only requires a few changes to switch from `PostgreSQL` to `MySQL`.
303+
304+
**Change the `docker-compose.yml` to the following:**
305+
306+
`.env`
307+
308+
```env
309+
DATABASE_TYPE=mysql
310+
# set "localhost" if you are running app on local machine
311+
# set "mysql" if you are running app on docker
312+
DATABASE_HOST=localhost
313+
DATABASE_PORT=3306
314+
DATABASE_USERNAME=root
315+
DATABASE_PASSWORD=secret
316+
DATABASE_NAME=app
317+
```
318+
319+
`docker-compose.yml`
320+
321+
```yml
322+
services:
323+
mysql:
324+
image: mysql:9.2.0
325+
ports:
326+
- ${DATABASE_PORT}:3306
327+
volumes:
328+
- mysql-boilerplate-db:/var/lib/mysql
329+
environment:
330+
MYSQL_USER: ${DATABASE_USERNAME}
331+
MYSQL_PASSWORD: ${DATABASE_PASSWORD}
332+
MYSQL_ROOT_PASSWORD: ${DATABASE_PASSWORD}
333+
MYSQL_DATABASE: ${DATABASE_NAME}
334+
335+
# other services here...
336+
337+
volumes:
338+
# other volumes here...
339+
mysql-boilerplate-db:
340+
```
341+
342+
After completing the above setup, run Docker with the following command:
343+
344+
```bash
345+
docker compose up -d mysql adminer maildev
346+
```
347+
348+
All three services should be running as shown below:
349+
350+
![image](https://github.com/user-attachments/assets/73e10325-66ed-46ca-a0c5-45791ef0750f)
351+
352+
Once your services are up and running, you're almost halfway through.
353+
354+
Now install the MySQL client:
355+
356+
```bash
357+
npm i mysql2 --save
358+
```
359+
360+
**Delete the existing migration file and generate a new one with the following script:**
361+
362+
```bash
363+
npm run migration:generate -- src/database/migrations/newMigration --pretty=true
364+
```
365+
366+
Run migrations:
367+
368+
```bash
369+
npm run migration:run
370+
```
371+
372+
Run seeds:
373+
374+
```bash
375+
npm run seed:run:relational
376+
```
377+
378+
Run the app in dev mode:
379+
380+
```bash
381+
npm run start:dev
382+
```
383+
384+
Open <http://localhost:3000>
385+
386+
To set up Adminer:
387+
388+
Open the running port in your browser.
389+
Open <http://localhost:8080>
390+
391+
![image](https://github.com/user-attachments/assets/f4b86daa-d93f-4ae9-a9e3-3c29bb3bba9d)
392+
393+
Running App:
394+
![image](https://github.com/user-attachments/assets/5dc0609d-5f6d-4176-918d-1744906f4f88)
395+
![image](https://github.com/user-attachments/assets/ff2201a6-d834-4c8b-9ab7-b9413a0a95c1)
396+
294397
---
295398
296399
Previous: [Command Line Interface](cli.md)

0 commit comments

Comments
 (0)