From 9a46d669ddfcd69f5fe8b5511ab6c2025897f28d Mon Sep 17 00:00:00 2001 From: Lai-YT <381xvmvbib@gmail.com> Date: Mon, 1 Apr 2024 21:26:43 +0800 Subject: [PATCH] Mount data files of database to local --- .gitignore | 4 ++++ create-todolist-mysql.sh | 30 ++++++++++++++++++++++++++++++ launch-mysql-container.sh | 15 --------------- 3 files changed, 34 insertions(+), 15 deletions(-) create mode 100755 create-todolist-mysql.sh delete mode 100755 launch-mysql-container.sh diff --git a/.gitignore b/.gitignore index 3b735ec..63996b9 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,7 @@ # Go workspace file go.work + +# Database file +*.db +.data/ diff --git a/create-todolist-mysql.sh b/create-todolist-mysql.sh new file mode 100755 index 0000000..f579149 --- /dev/null +++ b/create-todolist-mysql.sh @@ -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;" diff --git a/launch-mysql-container.sh b/launch-mysql-container.sh deleted file mode 100755 index 9f20159..0000000 --- a/launch-mysql-container.sh +++ /dev/null @@ -1,15 +0,0 @@ -#!/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 - -if ! docker ps -a | grep $CONTAINER_NAME; then - docker run -dp 3306:3306 --name "$CONTAINER_NAME" -e MYSQL_ROOT_PASSWORD=root "$IMAGE_NAME" -fi -docker exec -it "$CONTAINER_NAME" \ - mysql -uroot -proot -e "CREATE DATABASE todolist;"