在Docker容器启动时运行SQL脚本。

huangapple go评论65阅读模式
英文:

Run sql script on docker container startup

问题

所以我有一个SQL脚本,用于添加新用户、数据库,切换到新数据库并创建一些表格。

这是我的docker-compose.yaml:

version: '3'

services:
  db:
    container_name: my_db
    image: mariadb
    restart: always
    volumes:
      - ./init:/docker-entrypoint-initdb.d
    environment:
      MYSQL_ROOT_PASSWORD: password

volumes:
  my_db:

我还尝试在docker-compose文件中添加 command: --init-file ./init/init_db.sql 之类的内容,但没有成功。

这是从docker-compose up输出的内容:

Starting my_db ... done
Attaching to my_db
my_db | 2023-05-21 17:54:10+00:00 [Note] [Entrypoint]: Entrypoint script for MariaDB Server 1:10.11.3+maria~ubu2204 started.
...
...
my_db | Version: '10.11.3-MariaDB-1:10.11.3+maria~ubu2204'  socket: '/run/mysqld/mysqld.sock'  port: 3306  mariadb.org binary distribution

我还尝试过在dockerfile中进行一些操作,但目前我没有使用任何dockerfile,我对Docker仍然相当新手。

另外,我想在docker-compose文件的environment字段中设置新用户和密码组合,但似乎没有生效,这就是为什么用户创建部分在entrypoint脚本中的原因。

英文:

So I have a sql script that add a new user, database, switches to the new db and creates some tables.
I've been trying to run this script when I run docker-compose up but I can't manage to do so.

Here is my docker-compose.yaml :

version: '3'

services:
  db:
    container_name: my_db
    image: mariadb
    restart: always
    volumes:
      - ./init:/docker-entrypoint-initdb.d
    environment:
      MYSQL_ROOT_PASSWORD: password

volumes:
  my_db:

I've also tried adding command: --init-file ./init/init_db.sql things like that to the docker-compose file, but without any success.

Here is the output from docker-compose up :

Starting my_db ... done
Attaching to my_db
my_db | 2023-05-21 17:54:10+00:00 [Note] [Entrypoint]: Entrypoint script for MariaDB Server 1:10.11.3+maria~ubu2204 started.
my_db | 2023-05-21 17:54:10+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
my_db | 2023-05-21 17:54:10+00:00 [Note] [Entrypoint]: Entrypoint script for MariaDB Server 1:10.11.3+maria~ubu2204 started.
my_db | 2023-05-21 17:54:10+00:00 [Note] [Entrypoint]: MariaDB upgrade not required
my_db | 2023-05-21 17:54:10 0 [Note] Starting MariaDB 10.11.3-MariaDB-1:10.11.3+maria~ubu2204 source revision 0bb31039f54bd6a0dc8f0fc7d40e6b58a51998b0 as process 1
my_db | 2023-05-21 17:54:10 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
my_db | 2023-05-21 17:54:10 0 [Note] InnoDB: Using transactional memory
my_db | 2023-05-21 17:54:10 0 [Note] InnoDB: Number of transaction pools: 1
my_db | 2023-05-21 17:54:10 0 [Note] InnoDB: Using crc32 + pclmulqdq instructions
my_db | 2023-05-21 17:54:10 0 [Note] mariadbd: O_TMPFILE is not supported on /tmp (disabling future attempts)
my_db | 2023-05-21 17:54:10 0 [Warning] mariadbd: io_uring_queue_init() failed with ENOSYS: check seccomp filters, and the kernel version (newer than 5.1 required)
my_db | 2023-05-21 17:54:10 0 [Warning] InnoDB: liburing disabled: falling back to innodb_use_native_aio=OFF
my_db | 2023-05-21 17:54:10 0 [Note] InnoDB: Initializing buffer pool, total size = 128.000MiB, chunk size = 2.000MiB
my_db | 2023-05-21 17:54:10 0 [Note] InnoDB: Completed initialization of buffer pool
my_db | 2023-05-21 17:54:10 0 [Note] InnoDB: File system buffers for log disabled (block size=4096 bytes)
my_db | 2023-05-21 17:54:10 0 [Note] InnoDB: 128 rollback segments are active.
my_db | 2023-05-21 17:54:10 0 [Note] InnoDB: Setting file './ibtmp1' size to 12.000MiB. Physically writing the file full; Please wait ...
my_db | 2023-05-21 17:54:10 0 [Note] InnoDB: File './ibtmp1' size is now 12.000MiB.
my_db | 2023-05-21 17:54:10 0 [Note] InnoDB: log sequence number 47126; transaction id 16
my_db | 2023-05-21 17:54:10 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
my_db | 2023-05-21 17:54:10 0 [Note] Plugin 'FEEDBACK' is disabled.
my_db | 2023-05-21 17:54:10 0 [Warning] You need to use --log-bin to make --expire-logs-days or --binlog-expire-logs-seconds work.
my_db | 2023-05-21 17:54:10 0 [Note] InnoDB: Buffer pool(s) load completed at 230521 17:54:10
my_db | 2023-05-21 17:54:10 0 [Note] Server socket created on IP: '0.0.0.0'.
my_db | 2023-05-21 17:54:10 0 [Note] Server socket created on IP: '::'.
my_db | 2023-05-21 17:54:10 0 [Note] mariadbd: ready for connections.
my_db | Version: '10.11.3-MariaDB-1:10.11.3+maria~ubu2204'  socket: '/run/mysqld/mysqld.sock'  port: 3306  mariadb.org binary distribution


I tried some things with the dockerfile as well, but I'm not using any right now, I'm still fairly new to docker.

Little addition, I wanted to setup a new user and password combo inside the environment field in the docker-compose file, but it didn't seem to work, that's why the user creation part is inside the entrypoint script.

答案1

得分: 2

我在进行修改并将此脚本添加为启动脚本之前创建了容器。我必须先移除容器 docker container rm <container id> 然后再次运行 docker-compose up

英文:

I created the container before making the modifications and adding this script as the startup script. I had to remove the container first docker container rm &lt;container id&gt; and then run docker-compose up again.

huangapple
  • 本文由 发表于 2023年5月22日 02:10:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76301277.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定