ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

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

2022-07-13 19:03:08  阅读:207  来源: 互联网

标签:oracle19c compose run database startup 和表 scripts oracle docker


目的: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的差别和到底怎么实现,我还不明白。以后再看。

标签:oracle19c,compose,run,database,startup,和表,scripts,oracle,docker
来源: https://www.cnblogs.com/panda4671/p/16475185.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有