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

MySQL Docker容器自动建库、用户后SQL脚本未完成数据库填充的问题求助

Fix: Your MySQL Initialization Script Isn't Running (Here's Why & How to Fix It)

Alright, let's break down why your table creation and data population isn't working—you've got a small but critical misconfiguration here:

The Core Issue

You copied your SQL init script into the phpmyadmin container's /docker-entrypoint-initdb.d/ directory, but that directory is only used by the official MySQL Docker image to run initialization scripts on first startup. The phpmyadmin image doesn't care about that folder at all, so your script was never executed.

Step-by-Step Fix

Let's get your script running where it belongs—inside the MySQL container:

  1. Ditch the custom phpmyadmin Dockerfile (or modify it)
    You don't need to build a custom phpmyadmin image just for this init script. Either delete your Dockerfile entirely, or remove the COPY DockerCreateAllTablesDBwithData.sql /docker-entrypoint-initdb.d/initdb.sql line if you have other customizations for phpmyadmin.

  2. Mount your SQL script to the MySQL container
    Update the volumes section of your db service in docker-compose.yml to mount your local SQL file into the MySQL container's initialization directory:

    db:
      image: mysql:8-debian
      container_name: db
      restart: always
      networks:
        - network_app
      cap_add:
        - SYS_NICE
      environment:
        - MYSQL_ROOT_PASSWORD=password
        - MYSQL_USER=admin
        - MYSQL_PASSWORD=password
        - MYSQL_DATABASE=CantineTest
        - TZ='Europe/Paris'
      volumes:
        - mydatavolume:/var/lib/mysql
        # Add this line to mount your script
        - ./DockerCreateAllTablesDBwithData.sql:/docker-entrypoint-initdb.d/initdb.sql
    
  3. Reset your MySQL data volume (critical for existing setups)
    The MySQL image only runs init scripts once, when the data volume is first created. If you've already started the container before, your existing mydatavolume won't trigger the script again. Delete the old volume and restart:

    # Stop containers and delete volumes
    docker-compose down -v
    # Rebuild and start fresh
    docker-compose up -d
    

Extra Tips to Avoid Headaches

  • Remove redundant database creation: Your SQL script has create database CantineTest;, but you already set MYSQL_DATABASE=CantineTest in your docker-compose environment variables. This will cause a harmless but annoying "database already exists" error in logs—delete that line from your SQL script and just keep use CantineTest;.
  • Verify execution: Check the MySQL container logs to confirm the script ran successfully:
    docker logs db
    
    Look for lines like Executing /docker-entrypoint-initdb.d/initdb.sql—if there are errors, they'll show up here too.
  • Check script encoding: Make sure your SQL file is saved in UTF-8 or a MySQL-compatible encoding to avoid unexpected parsing issues.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.27 19:49:06