Docker容器

docker-compose中完全启动oracle19c后执行初始化脚本添加用户和表

本文主要是介绍docker-compose中完全启动oracle19c后执行初始化脚本添加用户和表,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

目的:container-registry.oracle.com/database/enterprise:19.3.0.0 up后会自动创建ORCLCDB,我需要创建这个数据库之后自动运行一些脚本来添加用户、表以及插入表数据。

方法一:oracle 19c的image自动提供了一种方法,直接mount,已自测成功

1.在docker-compose.yml中添加oracle 19c的相关信息

oracle-19c:
    image: container-registry.oracle.com/database/enterprise:19.3.0.0
    restart: always
    environment:
      - ORACLE_SID=ORCLCDB
      - ORACLE_HOME=/opt/oracle/product/19c/dbhome_1
    volumes:
      - "./db-server/oracle19c/init:/docker-entrypoint-initdb.d/"    
    ports:
      - "1522:1521"

说明:

01. image的连接时官方连接,需要注册,docker login

02. environment中的两个环境变量的值是image里设定好的,我们可以先不写这部分,启动之后,docker exec -it oracle_container_id bash进去找到合适的值添上去。各个oracle不同版本可能不相同。

03. ports这部分是端口映射,前一个1522是连通宿主机的port,后一个1521是docker image里设定的oracle缺省的port,一般情况下是1521.

04. 重点在volumes这部分mount。前半部分是宿主机上的目录,后一个是docker image里的目录。

我看的21c的官方文档,19c上同样适用

如果我们需要在初始化过程中执行某些sql语句或bash脚本,需要将这些脚本放在/opt/oracle/scripts/setup中;如果我们需要在oracle自带的初始化完成之后也就是数据库创建成功之后执行脚本,则需要将这些脚本放在/opt/oracle/scripts/startup中。而这两个目录image已经提供了软连接/opt/oracle/scripts到/docker-entrypoint-initdb.d/。因此volumes的后半部分可以是/docker-entrypoint-initdb.d/。

2. 在docker-compose.yml所在的同级目录中创建按照这个级别创建目录 db-server/oracle19c/init,并且在这个目录中创建setup和startup目录。注意setup和startup必须严格这么写,oracle完成数据库创建之后才会自动执行。

3. 我的需求是数据库创建成功之后创建用户、表和插入数据。因此我需要将我的sql文件放到startup中。需要注意的是,如果有多个文件,oracle会自动按照文件名的顺序来执行,因此我们需要人为地将这些文件合适地命名,保证需要先执行的文件在前。我有两个文件create01_users.sql和create02_english_tables.sql,第一个文件是创建user,第二个文件在该user下创建表,有先后顺序,因此用01,02来排序,如果反过来则是不成功的。

 

参看官方文档:https://docs.oracle.com/en/database/oracle/oracle-database/21/deeck/index.html#DEEDK-GUID-797CE794-D8D9-46F0-A40E-BA072E21E38F

Running Scripts After Setup and on Startup

Learn about how to configure Oracle Database images on Docker to run scripts after setup, and on database startup.

At the time of this release, you can run shell scripts (.sh) and SQL scripts (.sql) during database setup. To run these scripts, mount the volume /opt/oracle/scripts/setup, and include the scripts in this directory.

To run scripts after database startup, mount the volume /opt/oracle/scripts/startup, and include to include scripts in this directory.

Both the setup directory and startup directory locations are also represented by the symbolic link /docker-entrypoint-initdb.d. This symbolic link provides consistency with other database docker images. You can decide whether to put the setup and startup scripts under /opt/oracle/scripts, or /docker-entrypoint-initdb.d.

Note:

Startup scripts are run after the initial database creation is complete. Setup scripts are run only at the time of the initial database creation.

After the database is set up or started, the scripts in those folders are run on the database in the container. SQL scripts are run using the SYSDBA privileges (as sysdba), and shell scripts are run with the group privileges of the current user.

To ensure that scripts run in correct order, Oracle recommends that you prefix your scripts with a number. For example:

01_users.sql
02_permissions.sql

 

In the following example, the docker command starts up the database named dbtest, and mounts mounts the local directory myScripts to /opt/oracle/scripts/startup, which is then searched for custom startup scripts:

$ docker run -d --name dbtest -v /home/oracle/myScripts:/opt/oracle/scripts/startup container-registry.oracle.com/database/enterprise:21.3.0

 

方法二:更普适一些,就是用 https://docs.docker.com/reference/ Docker file 和 compose file 中entrypoint来实现。这两个entrypoint的差别和到底怎么实现,我还不明白。以后再看。

这篇关于docker-compose中完全启动oracle19c后执行初始化脚本添加用户和表的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!