source ../environment.sh
if [ -z "$NETWORK" ]; then
    echo "错误: 未找到 NETWORK 环境变量"
    exit 1
fi

if [ -z "$MYSQL_ROOT_PASSWORD" ]; then
    echo "错误: 未找到 MYSQL_ROOT_PASSWORD 环境变量"
    exit 1
fi
if [ -z "$MYSQL_PORT" ]; then
    echo "错误: 未找到 MYSQL_PORT 环境变量"
    exit 1
fi

if docker ps -a | grep -q ${MYSQL_NAME}; then
    echo "停止并删除已存在的 MySQL 容器..."
    docker stop ${MYSQL_NAME} >/dev/null 2>&1
    docker rm ${MYSQL_NAME} >/dev/null 2>&1
fi

docker run -d \
--name ${MYSQL_NAME} \
--network $NETWORK \
-p ${MYSQL_PORT}:3306 \
-e MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD} \
-e MYSQL_CHARACTER_SET_SERVER=utf8mb4 \
-e MYSQL_COLLATION_SERVER=utf8mb4_unicode_ci \
-v ${MYSQL_DATA}:/var/lib/mysql \
--env TZ=Asia/Shanghai \
--mount type=bind,source=/etc/localtime,target=/etc/localtime,readonly \
--memory ${MYSQL_MEMORY} \
--restart unless-stopped \
${MYSQL_IMAGE}

docker ps