Skip to content

Commit

Permalink
Mount data files of database to local
Browse files Browse the repository at this point in the history
  • Loading branch information
Lai-YT committed Apr 1, 2024
1 parent ee22a4f commit 9a46d66
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@

# Go workspace file
go.work

# Database file
*.db
.data/
30 changes: 30 additions & 0 deletions create-todolist-mysql.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env sh

set -eux

# Launch a MySQL container and expose the port 3306 to the localhost.
# A `todolist` database is created in the MySQL container if it does not exist.

IMAGE_NAME=mysql
CONTAINER_NAME=todolist-mysql
DATA_DIR=$PWD/.data
PORT=3306
MYSQL_ROOT_PASSWORD=root

# If the .data directory does not exist, create it.
if [ ! -d .data ]; then
mkdir .data
fi
# If the container does not exist, create it.
if ! docker ps -a | grep $CONTAINER_NAME; then
docker run \
-dp $PORT:3306 \
-v "$DATA_DIR":/var/lib/mysql \
--name "$CONTAINER_NAME" \
-e MYSQL_ROOT_PASSWORD="$MYSQL_ROOT_PASSWORD" \
"$IMAGE_NAME"
fi
# If the `todolist` database does not exist, create it.
docker exec -it "$CONTAINER_NAME" \
mysql -uroot -p$MYSQL_ROOT_PASSWORD \
-e "CREATE DATABASE todolist;"
15 changes: 0 additions & 15 deletions launch-mysql-container.sh

This file was deleted.

0 comments on commit 9a46d66

Please sign in to comment.