如何在Docker中自动启动sql脚本文件?

问题描述 投票:0回答:1

当我运行这个 docker 容器时,我试图启动 setup.sql 文件。 我认为主要问题是它无法运行这个“setup.sql”文件。但我不知道为什么。

FROM jaspeen/oracle-xe-11g

ENV ORACLE_HOME=/u01/app/oracle/product/11.2.0/xe
ENV PATH=$ORACLE_HOME/bin:$PATH
ENV ORACLE_SID=XE

COPY setup.sql /docker-entrypoint-initdb.d/setup.sql

EXPOSE 1521

CMD ["/bin/bash", "-c", "source /etc/profile && sqlplus /nolog @/docker-entrypoint-initdb.d/setup.sql && /etc/init.d/oracle-xe start && tail -f /u01/app/oracle/diag/rdbms/xe/XE/trace/alert_XE.log"]

安装.sql

CREATE USER open_dn IDENTIFIED BY 1234;
GRANT CONNECT, RESOURCE, DBA TO open_dn;

CREATE TABLE mytable (
    id NUMBER,
    name VARCHAR2(50)
);

INSERT INTO mytable (id, name) VALUES (1, 'John');
INSERT INTO mytable (id, name) VALUES (2, 'Jane');
COMMIT;

我检查了容器的日志

2024-05-10 11:08:58 Database not initialized. Initializing database.
2024-05-10 11:08:58 Setting up:
2024-05-10 11:08:58 processes=500
2024-05-10 11:08:58 sessions=555
2024-05-10 11:08:58 transactions=610
2024-05-10 11:08:58 If you want to use different parameters set processes, sessions, transactions env variables and consider this formula:
2024-05-10 11:08:58 processes=x
2024-05-10 11:08:58 sessions=x*1.1+5
2024-05-10 11:08:58 transactions=sessions*1.1
2024-05-10 11:08:58 
2024-05-10 11:08:58 Oracle Database 11g Express Edition Configuration
2024-05-10 11:08:58 -------------------------------------------------
2024-05-10 11:08:58 ls: cannot access /u01/app/oracle/oradata: No such file or directory
2024-05-10 11:08:58 This will configure on-boot properties of Oracle Database 11g Express 
2024-05-10 11:08:58 Edition.  The following questions will determine whether the database should 
2024-05-10 11:08:58 be starting upon system boot, the ports it will use, and the passwords that 
2024-05-10 11:08:58 will be used for database accounts.  Press <Enter> to accept the defaults. 
2024-05-10 11:08:58 Ctrl-C will abort.
2024-05-10 11:08:58 
2024-05-10 11:08:58 Specify the HTTP port that will be used for Oracle Application Express [8080]:
2024-05-10 11:08:58 Specify a port that will be used for the database listener [1521]:
2024-05-10 11:08:58 Specify a password to be used for database accounts.  Note that the same
2024-05-10 11:08:58 password will be used for SYS and SYSTEM.  Oracle recommends the use of 
2024-05-10 11:08:58 different passwords for each database account.  This can be done after 
2024-05-10 11:08:58 initial configuration:
2024-05-10 11:08:58 Confirm the password:
2024-05-10 11:08:58 
2024-05-10 11:08:58 Do you want Oracle Database 11g Express Edition to be started on boot (y/n) [y]:
2024-05-10 11:08:58 Starting Oracle Net Listener...Done
2024-05-10 11:10:33 Configuring database...Done
2024-05-10 11:10:33 Starting Oracle Database 11g Express Edition instance...Done
2024-05-10 11:10:33 Installation completed successfully.
2024-05-10 11:10:33 Database initialized. Please visit http://#containeer:8080/apex to proceed with configuration
2024-05-10 11:10:33 Oracle Database 11g Express Edition instance is already started
2024-05-10 11:10:33 
2024-05-10 11:10:33 Database ready to use. Enjoy! ;)

看似没有问题,但是

SQL> select username from all_users;             

USERNAME
------------------------------
XS$NULL
APEX_040000
APEX_PUBLIC_USER
FLOWS_FILES
HR
MDSYS
ANONYMOUS
XDB
CTXSYS
OUTLN
SYSTEM

USERNAME
------------------------------
SYS

12 rows selected.

没有用户名“open_dn”。 这意味着SQL查询没有执行?

我删除了镜像和容器,重建,运行

它说

2024-05-10 13:21:57 数据库未配置。如果需要,请运行 /etc/init.d/oracle-xe configure。

sql oracle docker
1个回答
0
投票

基础镜像中有一个entrypoint.sh脚本,您可以自定义脚本,在“Enjoy! ;)”行后面添加sql命令,如下所示:

#!/bin/bash

# Prevent owner issues on mounted folders
chown -R oracle:dba /u01/app/oracle
rm -f /u01/app/oracle/product
ln -s /u01/app/oracle-product /u01/app/oracle/product
# Update hostname
sed -i -E "s/HOST = [^)]+/HOST = $HOSTNAME/g" /u01/app/oracle/product/11.2.0/xe/network/admin/listener.ora
sed -i -E "s/PORT = [^)]+/PORT = 1521/g" /u01/app/oracle/product/11.2.0/xe/network/admin/listener.ora
echo "export ORACLE_HOME=/u01/app/oracle/product/11.2.0/xe" > /etc/profile.d/oracle-xe.sh
echo "export PATH=\$ORACLE_HOME/bin:\$PATH" >> /etc/profile.d/oracle-xe.sh
echo "export ORACLE_SID=XE" >> /etc/profile.d/oracle-xe.sh
. /etc/profile

case "$1" in
        '')
                #Check for mounted database files
                if [ "$(ls -A /u01/app/oracle/oradata)" ]; then
                        echo "found files in /u01/app/oracle/oradata Using them instead of initial database"
                        echo "XE:$ORACLE_HOME:N" >> /etc/oratab
                        chown oracle:dba /etc/oratab
                        chown 664 /etc/oratab
                        printf "ORACLE_DBENABLED=false\nLISTENER_PORT=1521\nHTTP_PORT=8080\nCONFIGURE_RUN=true\n" > /etc/default/oracle-xe
                        rm -rf /u01/app/oracle-product/11.2.0/xe/dbs
                        ln -s /u01/app/oracle/dbs /u01/app/oracle-product/11.2.0/xe/dbs
                else
                        echo "Database not initialized. Initializing database."

                        printf "Setting up:\nprocesses=$processes\nsessions=$sessions\ntransactions=$transactions\n"
                        echo "If you want to use different parameters set processes, sessions, transactions env variables and consider this formula:"
                        printf "processes=x\nsessions=x*1.1+5\ntransactions=sessions*1.1\n"

                        mv /u01/app/oracle-product/11.2.0/xe/dbs /u01/app/oracle/dbs
                        ln -s /u01/app/oracle/dbs /u01/app/oracle-product/11.2.0/xe/dbs

                        #Setting up processes, sessions, transactions.
                        sed -i -E "s/processes=[^)]+/processes=$processes/g" /u01/app/oracle/product/11.2.0/xe/config/scripts/init.ora
                        sed -i -E "s/processes=[^)]+/processes=$processes/g" /u01/app/oracle/product/11.2.0/xe/config/scripts/initXETemp.ora

                        sed -i -E "s/sessions=[^)]+/sessions=$sessions/g" /u01/app/oracle/product/11.2.0/xe/config/scripts/init.ora
                        sed -i -E "s/sessions=[^)]+/sessions=$sessions/g" /u01/app/oracle/product/11.2.0/xe/config/scripts/initXETemp.ora

                        sed -i -E "s/transactions=[^)]+/transactions=$transactions/g" /u01/app/oracle/product/11.2.0/xe/config/scripts/init.ora
                        sed -i -E "s/transactions=[^)]+/transactions=$transactions/g" /u01/app/oracle/product/11.2.0/xe/config/scripts/initXETemp.ora

                        printf 8080\\n1521\\noracle\\noracle\\ny\\n | /etc/init.d/oracle-xe configure

                        echo "Database initialized. Please visit http://#containeer:8080/apex to proceed with configuration"
                fi

                /etc/init.d/oracle-xe start
                echo "Database ready to use. Enjoy! ;)"

                ## Run your customer sql file
                sqlplus system/oracle @docker-entrypoint-initdb.d/setup.sql

                ##
                ## Workaround for graceful shutdown. ....ing oracle... ‿( ́ ̵ _-`)‿
                ##
                while [ "$END" == '' ]; do
                        sleep 1
                        trap "/etc/init.d/oracle-xe stop && END=1" INT TERM
                done
                ;;

        *)
                echo "Database is not configured. Please run /etc/init.d/oracle-xe configure if needed."
                $1
                ;;
esac

并更改 Dockerfile,如下所示:

FROM jaspeen/oracle-xe-11g

ENV ORACLE_HOME=/u01/app/oracle/product/11.2.0/xe
ENV PATH=$ORACLE_HOME/bin:$PATH
ENV ORACLE_SID=XE

COPY setup.sql /docker-entrypoint-initdb.d/setup.sql
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

EXPOSE 1521

然后重建你的图像并运行它,它不是那么优雅,因为你必须更改entrypoint.sh,但它可以工作。

© www.soinside.com 2019 - 2024. All rights reserved.