Skip to content

Commit e7d8ef7

Browse files
dsentkerdunglasbkosunmaxhelias
authored
Add documentation on how to use MySQL instead of PostgreSQL. (#567)
Co-authored-by: Kévin Dunglas <[email protected]> Co-authored-by: Borislav Kosun <[email protected]> Co-authored-by: Maxime Helias <[email protected]>
1 parent 737029b commit e7d8ef7

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ with [FrankenPHP](https://frankenphp.dev) and [Caddy](https://caddyserver.com/)
3737
5. [Debugging with Xdebug](docs/xdebug.md)
3838
6. [TLS Certificates](docs/tls.md)
3939
7. [Using a Makefile](docs/makefile.md)
40-
8. [Troubleshooting](docs/troubleshooting.md)
41-
9. [Updating the template](docs/updating.md)
40+
8. [Using MySQL instead of PostgreSQL](docs/mysql.md)
41+
9. [Troubleshooting](docs/troubleshooting.md)
42+
10. [Updating the template](docs/updating.md)
4243

4344
## License
4445

docs/mysql.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Using MySQL
2+
3+
The Docker configuration of this repository is extensible thanks to Flex recipes. By default, the recipe installs PostgreSQL.
4+
If you prefer to work with MySQL, follow these steps:
5+
6+
First, install the `symfony/orm-pack` package as described: `docker compose exec php composer req symfony/orm-pack`
7+
8+
## Docker Configuration
9+
Change the database image to use MySQL instead of PostgreSQL in `compose.yaml`:
10+
11+
```diff
12+
###> doctrine/doctrine-bundle ###
13+
- image: postgres:${POSTGRES_VERSION:-15}-alpine
14+
+ image: mysql:${MYSQL_VERSION:-8}
15+
environment:
16+
- POSTGRES_DB: ${POSTGRES_DB:-app}
17+
+ MYSQL_DATABASE: ${MYSQL_DATABASE:-app}
18+
# You should definitely change the password in production
19+
+ MYSQL_RANDOM_ROOT_PASSWORD: "true"
20+
- POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-!ChangeMe!}
21+
+ MYSQL_PASSWORD: ${MYSQL_PASSWORD:-!ChangeMe!}
22+
- POSTGRES_USER: ${POSTGRES_USER:-app}
23+
+ MYSQL_USER: ${MYSQL_USER:-app}
24+
volumes:
25+
- - database_data:/var/lib/postgresql/data:rw
26+
+ - database_data:/var/lib/mysql:rw
27+
# You may use a bind-mounted host directory instead, so that it is harder to accidentally remove the volume and lose all your data!
28+
- # - ./docker/db/data:/var/lib/postgresql/data:rw
29+
+ # - ./docker/db/data:/var/lib/mysql:rw
30+
###< doctrine/doctrine-bundle ###
31+
```
32+
33+
Depending on the database configuration, modify the environment in the same file at `services.php.environment.DATABASE_URL`
34+
```
35+
DATABASE_URL: mysql://${MYSQL_USER:-app}:${MYSQL_PASSWORD:-!ChangeMe!}@database:3306/${MYSQL_DATABASE:-app}?serverVersion=${MYSQL_VERSION:-8}&charset=${MYSQL_CHARSET:-utf8mb4}
36+
```
37+
38+
Since we changed the port, we also have to define this in the `compose.override.yaml`:
39+
```diff
40+
###> doctrine/doctrine-bundle ###
41+
database:
42+
ports:
43+
- - "5432"
44+
+ - "3306"
45+
###< doctrine/doctrine-bundle ###
46+
```
47+
48+
Last but not least, we need to install the MySQL driver in `Dockerfile`:
49+
```diff
50+
###> doctrine/doctrine-bundle ###
51+
-RUN install-php-extensions pdo_pgsql
52+
+RUN install-php-extensions pdo_mysql
53+
###< doctrine/doctrine-bundle ###
54+
```
55+
56+
## Change Environment
57+
Change the database configuration in `.env`:
58+
59+
```dotenv
60+
DATABASE_URL=mysql://${MYSQL_USER:-app}:${MYSQL_PASSWORD:-!ChangeMe!}@database:3306/${MYSQL_DATABASE:-app}?serverVersion=${MYSQL_VERSION:-8}&charset=${MYSQL_CHARSET:-utf8mb4}
61+
```
62+
63+
## Final steps
64+
Rebuild the docker environment:
65+
```shell
66+
docker compose down --remove-orphans && docker compose build --pull --no-cache
67+
```
68+
69+
Test your setup:
70+
```shell
71+
docker compose exec php bin/console dbal:run-sql -q "SELECT 1" && echo "OK" || echo "Connection is not working"
72+
```

0 commit comments

Comments
 (0)