Skip to content

Commit 1b6afc2

Browse files
committed
docs: improve mysql using docker
1 parent 41afb4d commit 1b6afc2

File tree

1 file changed

+40
-24
lines changed

1 file changed

+40
-24
lines changed

content/blog/how-to-set-up-mysql-using-docker.md

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ description: 'Set up a local MySQL database with Docker.'
1111
<HintBlock type="info">
1212

1313
If you are using Mac, you can use following tools which include MySQL docker images across many versions:
14-
- [DBngin](https://dbngin.com/) - A free all-in-one database version manager
15-
- [Homebrew](https://brew.sh/) - Package manager for macOS
14+
15+
- [StackBricks](https://stackbricks.app/)
16+
- [DBngin](https://dbngin.com/)
1617

1718
</HintBlock>
1819

@@ -39,9 +40,10 @@ docker run --name mysql-container -e MYSQL_ROOT_PASSWORD=your_password -p 3306:3
3940
```
4041

4142
This command:
43+
4244
- Creates a container named `mysql-container`
4345
- Sets the root password to `your_password` (change this!)
44-
- Maps port 3306 from the container to your host
46+
- Maps MySQL default port 3306 from the container to your host
4547
- Runs MySQL in detached mode
4648

4749
### Step 3: Verify the Container is Running
@@ -66,12 +68,16 @@ This command opens a MySQL shell inside the running container, giving you direct
6668

6769
Alternatively, if you have the MySQL client installed on your host machine:
6870

71+
<HintBlock type="info">
72+
73+
To install MySQL client, you can refer to [this post](/blog/how-to-install-mysql-client-on-mac-ubuntu-centos-windows).
74+
75+
</HintBlock>
76+
6977
```bash
7078
mysql -h 127.0.0.1 -P 3306 -uroot -p
7179
```
7280

73-
When prompted, enter the password you specified.
74-
7581
### Step 5: Create a Database and User
7682

7783
Once connected to MySQL, you can create a database and user:
@@ -85,7 +91,7 @@ FLUSH PRIVILEGES;
8591

8692
You can exit MySQL server by `EXIT;`.
8793

88-
## More Optional Operations
94+
## Other Operations
8995

9096
### Persisting Data
9197

@@ -100,51 +106,61 @@ This creates a Docker volume named `mysql-data` that persists your database file
100106
### Common Docker MySQL Commands
101107

102108
Stop the container:
109+
103110
```bash
104111
docker stop mysql-container
105112
```
106113

107114
Start an existing container:
115+
108116
```bash
109117
docker start mysql-container
110118
```
111119

112120
Remove the container:
121+
113122
```bash
114123
docker rm mysql-container
115124
```
116125

117126
### Using Docker Compose (Recommended for Development)
118127

119-
For a more manageable setup, create a `docker-compose.yml` file:
128+
A bare minimum `docker-compose.yml` file with MySQL and an application:
120129

121130
```yaml
122-
version: '3'
131+
version: '1'
132+
123133
services:
124134
mysql:
125-
image: mysql:8.0
126-
container_name: mysql-container
135+
image: mysql:8
127136
environment:
128-
MYSQL_ROOT_PASSWORD: your_password
129-
MYSQL_DATABASE: my_database
130-
MYSQL_USER: my_user
131-
MYSQL_PASSWORD: my_password
132-
ports:
133-
- "3306:3306"
137+
MYSQL_ROOT_PASSWORD: rootpassword
138+
MYSQL_DATABASE: mydatabase
139+
MYSQL_USER: user
140+
MYSQL_PASSWORD: password
134141
volumes:
135-
- mysql-data:/var/lib/mysql
142+
- mysql_data:/var/lib/mysql
143+
ports:
144+
- '3306:3306'
145+
146+
app:
147+
image: my-application:latest
148+
depends_on:
149+
- mysql
150+
environment:
151+
DB_HOST: mysql
152+
DB_NAME: mydatabase
153+
DB_USER: user
154+
DB_PASSWORD: password
155+
ports:
156+
- '8080:8080'
136157

137158
volumes:
138-
mysql-data:
159+
mysql_data:
139160
```
140161
141162
Run with:
163+
142164
```bash
143165
docker-compose up -d
144166
```
145-
146-
## Conclusion
147-
148-
You now have a MySQL database running in Docker! This setup is ideal for development environments as it's isolated, reproducible, and easy to manage.
149-
150-
For production deployments, consider additional security configurations and backup strategies.

0 commit comments

Comments
 (0)