gpt4 book ai didi

mysql - Docker-compose/init.sql无法正确执行

转载 作者:行者123 更新时间:2023-12-02 19:21:31 25 4
gpt4 key购买 nike

我正在尝试通过docker-compose设置本地MySQL实例,它看起来像这样:

version: '3'

services:
database:
image: mysql:latest
ports:
- '3306:3306'
command: '--init-file /data/application/init.sql'
command: 'mysqld --default-authentication-plugin=mysql_native_password'
volumes:
- './init.sql:/data/application/init.sql'
- './data:/var/lib/mysql'
env_file:
- ./.env
expose:
- '3306'
# cap_add:
# - SYS_NICE
environment:
- MYSQL_ROOT_PASSW="${MYSQL_ROOT_PASSWORD}"
- MYSQL_DATABASE="${MYSQL_DATABASE}"
- MYSQL_USER="${MYSQL_USER}"
- MYSQL_ROOT_USER="${MYSQL_ROOT_USER}"
- MYSQL_PASSWORD="${MYSQL_PASSWORD}"
# network_mode: host

使用以下 init.sql 脚本,应负责创建数据库和设置表:

CREATE DATABASE IF NOT EXISTS test;
USE test;

CREATE TABLE IF NOT EXISTS `listings`(
id INTEGER
,PRIMARY KEY (id)
);

容器被构建并正常启动:
Attaching to so-feed-parse_database_1
database_1 | 2019-12-12 14:58:39+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.18-1debian9 started.
database_1 | 2019-12-12 14:58:39+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
database_1 | 2019-12-12 14:58:39+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.18-1debian9 started.
database_1 | 2019-12-12 14:58:39+00:00 [Note] [Entrypoint]: Initializing database files
database_1 | 2019-12-12T14:58:39.976524Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
database_1 | 2019-12-12T14:58:39.976669Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.18) initializing of server in progress as process 46
database_1 | 2019-12-12T14:58:41.624212Z 5 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
database_1 | 2019-12-12 14:58:44+00:00 [Note] [Entrypoint]: Database files initialized
database_1 | 2019-12-12 14:58:44+00:00 [Note] [Entrypoint]: Starting temporary server
database_1 | 2019-12-12T14:58:44.542746Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
database_1 | 2019-12-12T14:58:44.542845Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.18) starting as process 95
database_1 | 2019-12-12T14:58:45.012478Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
database_1 | 2019-12-12T14:58:45.016362Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
database_1 | 2019-12-12T14:58:45.054899Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.18' socket: '/var/run/mysqld/mysqld.sock' port: 0 MySQL Community Server - GPL.
database_1 | 2019-12-12 14:58:45+00:00 [Note] [Entrypoint]: Temporary server started.
database_1 | 2019-12-12T14:58:45.128416Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock'
database_1 | Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
database_1 | Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it.
database_1 | Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
database_1 | Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.
database_1 | 2019-12-12 14:58:47+00:00 [Note] [Entrypoint]: Creating database "test"
database_1 | 2019-12-12 14:58:47+00:00 [Note] [Entrypoint]: Creating user "utest"
database_1 | 2019-12-12 14:58:47+00:00 [Note] [Entrypoint]: Giving user "utest" access to schema "test"
database_1 |
database_1 | 2019-12-12 14:58:47+00:00 [Note] [Entrypoint]: Stopping temporary server
database_1 | 2019-12-12T14:58:47.355373Z 14 [System] [MY-013172] [Server] Received SHUTDOWN from user root. Shutting down mysqld (Version: 8.0.18).
database_1 | 2019-12-12T14:58:49.162768Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.18) MySQL Community Server - GPL.
database_1 | 2019-12-12 14:58:49+00:00 [Note] [Entrypoint]: Temporary server stopped
database_1 |
database_1 | 2019-12-12 14:58:49+00:00 [Note] [Entrypoint]: MySQL init process done. Ready for start up.
database_1 |
database_1 | 2019-12-12T14:58:49.662509Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
database_1 | 2019-12-12T14:58:49.662675Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.18) starting as process 1
database_1 | 2019-12-12T14:58:50.034163Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
database_1 | 2019-12-12T14:58:50.036925Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
database_1 | 2019-12-12T14:58:50.062397Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.18' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL.
database_1 | 2019-12-12T14:58:50.222174Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060

现在,登录到服务器后, show databases;给出以下输出(尝试 use test;输出 unknown database:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| "test" |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.01 sec)

手动执行初始化脚本就像一个 super 按钮一样,不知道它是否取决于 --init-file命令。

编辑:
this问题中的OP正在遵循类似的方法,但不使用 command: '--init-file',而是将init.sql文件安装在 ./init.sql:/docker-entrypoint-initdb.d/init.sql上。
在将我的docker-compose.yml改组为数据库之后,现在可以正确创建了。我不太确定为什么会这样...

最佳答案

'./init.sql:/data/application/init.sql'不是正确的路径,应将其与/docker-entrypoint-initdb.d映射,作为在/docker-entrypoint-initdb.d下查找文件的入口点。

也最好从文档中查看此警告。

警告:

scripts in /docker-entrypoint-initdb.d are only run if you start the container with a data directory that is empty; any pre-existing database will be left untouched on container startup. One common problem is that if one of your /docker-entrypoint-initdb.d scripts fails (which will cause the entrypoint script to exit) and your orchestrator restarts the container with the already initialized data directory, it will not continue on with your scripts.



postgres Initialization scripts

关于mysql - Docker-compose/init.sql无法正确执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59307448/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com