You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VirtualBox中Ubuntu系统Docker Compose部署MySQL无法创建数据库求助

Troubleshooting MySQL Database Not Creating on Docker Compose Redeployment

Hey there, let's break down why your MySQL init scripts aren't running reliably when you redeploy, and fix this for good!

Common Issues & Fixes

1. Your Data Volume Isn’t Actually Fully Deleted

The official MySQL image only runs scripts in docker-entrypoint-initdb.d if the database directory is completely empty — that means first-time setup, or full removal of the data volume. If you’re just deleting the image but leaving the volume intact, MySQL skips initialization entirely.

  • To fully clean up existing containers and volumes:
    docker-compose down -v
    
  • Double-check volumes are gone with docker volume ls — if you see any related to your project, delete them manually with docker volume rm <volume-name>.

2. Missing MYSQL_DATABASE Environment Variable

Looking at your compose config, you haven’t set the MYSQL_DATABASE variable! This tells the official MySQL image to automatically create a database during initialization. Without it, unless your init script explicitly runs CREATE DATABASE IF NOT EXISTS <db-name>;, no database will be created.

Add this to your environment section (make sure to define ENV_DB_NAME in your .env file):

- MYSQL_DATABASE=${ENV_DB_NAME}

You can also remove MYSQL_PORT_3306_TCP_ADDR and MYSQL_PORT_3306_TCP_PORT — these are for inter-container linking, not configuring the container’s own listening address/port, so they’re unnecessary here.

3. Permissions or Script Problems in docker-entrypoint-initdb.d

If your init scripts can’t be read by the container, or have syntax errors, they’ll fail (often silently unless you check logs):

  • Fix permissions on your local docker-entrypoint-initdb.d directory:
    chmod -R 755 ./docker-entrypoint-initdb.d
    
  • Name scripts in execution order (e.g., 01-create-db.sql, 02-seed-data.sql) — the image runs scripts alphabetically.
  • Validate your SQL syntax — even a missing semicolon can cause the script to fail partway through.

4. Check Container Logs for Errors

Always check the MySQL container logs to see exactly what’s happening during initialization:

docker logs <your-mysql-container-name>

Look for lines about "Initializing database" or script execution errors — this will pinpoint why the database isn’t being created.

5. Simplify Your command Section (Temporarily)

While your command parameters look reasonable, extra flags can sometimes interfere with initialization. Try removing the --user=root flag (the container runs as root by default anyway) to rule out any conflicts.

Modified Docker Compose Example

Here’s a cleaned-up version of your config with the fixes applied:

mysql:
  image: "mysql:5.7"
  network_mode: "bridge"
  volumes:
    - ${DATA_ROOT}/mysql:/var/lib/mysql
    - ./docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
  environment:
    - MYSQL_ROOT_PASSWORD=${ENV_PASSWORD}
    - MYSQL_DATABASE=${ENV_DB_NAME}
    - MYSQL_USER=${ENV_USER}
    - MYSQL_PASSWORD=${ENV_PASSWORD}
  command:
    - --max_allowed_packet=500M
    - --character-set-server=utf8mb4
    - --collation-server=utf8mb4_unicode_ci
    - --max_connections=250
    - --default-authentication-plugin=mysql_native_password
  ports:
    - "3306:3306"

Final Test Steps

  1. Run docker-compose down -v to fully clean up existing resources
  2. Update your .env file to include ENV_DB_NAME (e.g., ENV_DB_NAME=my_app_db)
  3. Verify your init scripts (if using) are syntax-correct and have proper permissions
  4. Deploy again with docker-compose up -d

This should ensure your database is created consistently, even after redeployments.

内容的提问来源于stack exchange,提问作者Dhwanil Patel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:37:48