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

rm -fr ./mysql-config
# 创建MySQL配置目录
mkdir -p ./mysql-config


# 创建MySQL配置文件
cat > ./mysql-config/my.cnf << EOF
[mysqld]
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
EOF

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} \
-v ${MYSQL_DATA}:/var/lib/mysql \
-v $(pwd)/mysql-config/my.cnf:/etc/mysql/conf.d/my.cnf \
--env TZ=Asia/Shanghai \
--mount type=bind,source=/etc/localtime,target=/etc/localtime,readonly \
--memory ${MYSQL_MEMORY} \
--restart unless-stopped \
${MYSQL_IMAGE}

# 等待MySQL启动
echo "等待MySQL启动..."
sleep 10

# 验证SQL_MODE设置
docker exec ${MYSQL_NAME} mysql -uroot -p${MYSQL_ROOT_PASSWORD} -e "SELECT @@sql_mode;"

docker ps