Files
schoolNews/docker/Dockerfile.mysql
2025-11-24 11:50:15 +08:00

102 lines
3.2 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# - MySQL数据库镜像
# reInit.sh的数据库初始化方案
FROM mysql:8.0
#
ENV LANG=C.UTF-8 \
TZ=Asia/Shanghai
# MySQL配置文件
COPY docker/mysql/my.cnf /etc/mysql/conf.d/my.cnf
# SQL目录
RUN mkdir -p /docker-entrypoint-initdb.d /opt/sql
# SQL文件
COPY schoolNewsServ/.bin/mysql/sql/ /opt/sql/
# reInit.sh为Docker环境
COPY schoolNewsServ/.bin/mysql/sql/reInit.sh /opt/sql/
RUN sed -i 's/DB_HOST="localhost"/DB_HOST="localhost"/' /opt/sql/reInit.sh && \
sed -i 's/DB_PORT="3306"/DB_PORT="3306"/' /opt/sql/reInit.sh && \
sed -i 's/DB_USER="root"/DB_USER="root"/' /opt/sql/reInit.sh && \
sed -i 's/DB_PASSWORD="123456"/DB_PASSWORD="${MYSQL_ROOT_PASSWORD}"/' /opt/sql/reInit.sh && \
sed -i 's/DB_NAME="school_news"/DB_NAME="${MYSQL_DATABASE}"/' /opt/sql/reInit.sh && \
chmod +x /opt/sql/reInit.sh
# Docker初始化适配脚本
RUN cat > /docker-entrypoint-initdb.d/01-init-database.sh <<'EOF'
#!/bin/bash
set -e
echo "=========================================="
echo "校园新闻管理系统 - 数据库初始化"
echo "使用 reInit.sh + Docker配置更新"
echo "=========================================="
# MySQL完全启动
echo "等待MySQL启动..."
until mysql -uroot -p"${MYSQL_ROOT_PASSWORD}" -e "SELECT 1" >/dev/null 2>&1; do
sleep 1
done
echo "MySQL已就绪"
# SQL目录
cd /opt/sql
# reInit.sh使用
export DB_HOST="localhost"
export DB_PORT="3306"
export DB_USER="root"
export DB_PASSWORD="${MYSQL_ROOT_PASSWORD}"
export DB_NAME="${MYSQL_DATABASE}"
export MYSQL_PWD="${MYSQL_ROOT_PASSWORD}"
# reInit.sh的初始化函数
echo "执行数据库初始化..."
# Source reInit.sh并调用其初始化函数
source reInit.sh
# reInit.sh的核心函数
execute_init_script # initAll.sql
import_sensitive_words #
# Docker环境特定配置
echo "更新Docker环境配置..."
mysql -uroot "${MYSQL_DATABASE}" <<EOSQL
-- 更新爬虫配置为Docker容器内路径
UPDATE tb_sys_config
SET config_value = '/usr/bin/python3'
WHERE config_key = 'crawler.pythonPath';
UPDATE tb_sys_config
SET config_value = '/app/crawler'
WHERE config_key = 'crawler.basePath';
-- 如果配置不存在则插入
INSERT IGNORE INTO tb_sys_config (config_key, config_value, config_desc, created_at)
VALUES
('crawler.pythonPath', '/usr/bin/python3', 'Docker容器内Python路径', NOW()),
('crawler.basePath', '/app/crawler', 'Docker容器内爬虫脚本路径', NOW());
SELECT '✅ 数据库初始化完成!' AS message;
SELECT '默认用户: admin, 密码: admin123' AS tip;
SELECT '爬虫配置已更新为Docker容器路径' AS docker_config;
EOSQL
echo "=========================================="
echo "✅ 初始化完成!"
echo "=========================================="
EOF
#
RUN chmod +x /docker-entrypoint-initdb.d/01-init-database.sh
#
EXPOSE 3306
#
HEALTHCHECK --interval=10s --timeout=5s --retries=5 --start-period=30s \
CMD mysqladmin ping -h localhost -p${MYSQL_ROOT_PASSWORD} || exit 1