{"id":22729698,"url":"https://github.com/perfectstorm88/mysql_docker_init","last_synced_at":"2026-04-19T14:31:15.405Z","repository":{"id":89736031,"uuid":"237137083","full_name":"perfectstorm88/mysql_docker_init","owner":"perfectstorm88","description":"mysql Create database on docker-compose startup","archived":false,"fork":false,"pushed_at":"2020-01-30T04:18:07.000Z","size":2,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-30T00:44:52.959Z","etag":null,"topics":["docker","docker-compose","init","initializer","mysql"],"latest_commit_sha":null,"homepage":null,"language":"TSQL","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/perfectstorm88.png","metadata":{"files":{"readme":"README.MD","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-01-30T04:14:53.000Z","updated_at":"2020-01-30T04:20:57.000Z","dependencies_parsed_at":null,"dependency_job_id":"f4fdcfa4-c5d9-45de-b5fe-692ea074c54f","html_url":"https://github.com/perfectstorm88/mysql_docker_init","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/perfectstorm88/mysql_docker_init","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/perfectstorm88%2Fmysql_docker_init","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/perfectstorm88%2Fmysql_docker_init/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/perfectstorm88%2Fmysql_docker_init/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/perfectstorm88%2Fmysql_docker_init/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/perfectstorm88","download_url":"https://codeload.github.com/perfectstorm88/mysql_docker_init/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/perfectstorm88%2Fmysql_docker_init/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32009814,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-18T20:23:30.271Z","status":"online","status_checked_at":"2026-04-19T02:00:07.110Z","response_time":55,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["docker","docker-compose","init","initializer","mysql"],"created_at":"2024-12-10T18:11:27.256Z","updated_at":"2026-04-19T14:31:15.389Z","avatar_url":"https://github.com/perfectstorm88.png","language":"TSQL","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 需求说明\n- 初始化的数据库脚本，需要在数据库首次使用时执行，之后重启应用和数据库，不再执行初始化脚本\n- 数据库的数据要进行持久化，mysql容器是否重启销毁不影响数据\n\n# 用法\n参加[docker-mysql](https://hub.docker.com/_/mysql)的\"Initializing a fresh instance\"部分：\n\n\u003eWhen a container is started for the first time, a new database with the specified name will be created and initialized with the provided configuration variables. Furthermore, it will execute files with extensions .sh, .sql and .sql.gz that are found in /docker-entrypoint-initdb.d. Files will be executed in alphabetical order. You can easily populate your mysql services by mounting a SQL dump into that directory and provide custom images with contributed data. SQL files will be imported by default to the database specified by the MYSQL_DATABASE variable.\n\n就是把初始化脚本，映射到/docker-entrypoint-initdb.d目录下，首次启动时执行\n```yml\nversion: \"3.5\"\nservices:\n  db:\n    image: mysql\n    command: --default-authentication-plugin=mysql_native_password\n    restart: always\n    environment:\n      MYSQL_ROOT_PASSWORD: abc\n    volumes:\n        - ./mysql/mydb_log.sql:/docker-entrypoint-initdb.d/mydb_log.sql\n        - ./mysql/setup.sql:/docker-entrypoint-initdb.d/setup.sql\n        - db_data:/var/lib/mysql\n  adminer:\n    image: adminer\n    restart: always\n    ports:\n      - 8080:8080 # http://localhost:8080\n\nvolumes:\n    db_data:\n```\n\n首次启动`docker-compose up`时可以看到下面日志，之后再重启就不再执行了\n```\n2020-01-30 03:24:32+00:00 [Note] [Entrypoint]: /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/mydb_log.sql\n2020-01-30 03:24:32+00:00 [Note] [Entrypoint]: /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/setup.sql\n```\n\n# 参考:\n\n- [Create database on docker-compose startup](https://stackoverflow.com/questions/43322033/create-database-on-docker-compose-startup):参照样例\n- [docker-mysql](https://hub.docker.com/_/mysql):参照\n\n# 附：辅助脚本，重复测试时使用\n```bash\n# 删除容器\ndocker ps -a|grep -v CONTAINER|grep -v Up|awk -F \" \" '{print $1}'|xargs docker rm\n# 情况volume\ndocker volume prune\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fperfectstorm88%2Fmysql_docker_init","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fperfectstorm88%2Fmysql_docker_init","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fperfectstorm88%2Fmysql_docker_init/lists"}