使用Docker容器運行Oracle數據庫方式
在實際開發(fā)中,大部分時間可能都在開發(fā)、測試和驗證工作,有些時候我們需要快速使用一個臨時 oracle 數據庫來做開發(fā)或者驗證工作,而又會因為 oracle 安裝麻煩而煩惱。
這種快速臨時性需求,我們可以選中使用 docker 容器的方式運行一個 oracle 數據庫,官方也給出了對應的鏡像,包括 amd64 和 arm64 架構的都有。
啟動容器
- docker-compose.yml
services: oracle: image: container-registry.oracle.com/database/enterprise:19.3.0.0 container_name: oracledb ports: - 1521:1521 - 5500:5500 volumes: - ./oracle/oradata:/opt/oracle/oradata environment: TZ: Asia/Shanghai ORACLE_SID: orcl ORACLE_PWD: oracle123456 ENABLE_ARCHIVELOG: true logging: driver: "json-file" options: max-size: "1g" max-file: "20"
- 或者直接運行腳本啟動容器:
docker run -d --name oracledb \ -p 1521:1521 -p 5500:5500 \ -e ORACLE_SID=orcl \ -e ORACLE_PWD=oracle123456 \ -e ENABLE_ARCHIVELOG=true \ -v /opt/soft/oracle/oradata:/opt/oracle/oradata \ container-registry.oracle.com/database/enterprise:19.3.0.0
注意提前創(chuàng)建 oradata 目錄,并且為該目錄設置 chmod 777 權限,否則會出現容器掛載后寫入數據不正常的問題(如果是權限問題日志中會有權限問題的相關內容)。
- 容器環(huán)境變量更多參數說明如下:
Parameters: --name: The name of the container (default: auto generated -p: The port mapping of the host port to the container port. Two ports are exposed: 1521 (Oracle Listener), 5500 (OEM Express) -e ORACLE_SID: The Oracle Database SID that should be used (default:ORCLCDB) -e ORACLE_PDB: The Oracle Database PDB name that should be used (default: ORCLPDB1) -e ORACLE_PWD: The Oracle Database SYS, SYSTEM and PDBADMIN password (default: auto generated) -e INIT_SGA_SIZE: The total memory in MB that should be used for all SGA components (optional) -e INIT_PGA_SIZE: The target aggregate PGA memory in MB that should be used for all server processes attached to the instance (optional) -e ORACLE_EDITION: The Oracle Database Edition (enterprise/standard, default: enterprise) -e ORACLE_CHARACTERSET: The character set to use when creating the database (default: AL32UTF8) -e ENABLE_ARCHIVELOG: To enable archive log mode when creating the database (default: false). Supported 19.3 onwards. -v /opt/oracle/oradata The data volume to use for the database. Has to be writable by the Unix "oracle" (uid: 54321) user inside the container If omitted the database will not be persisted over container recreation. -v /opt/oracle/scripts/startup | /docker-entrypoint-initdb.d/startup Optional: A volume with custom scripts to be run after database startup. For further details see the "Running scripts after setup and on startup" section below. -v /opt/oracle/scripts/setup | /docker-entrypoint-initdb.d/setup Optional: A volume with custom scripts to be run after database setup. For further details see the "Running scripts after setup and on startup" section below.
常用命令
連接到容器內SQLPlus
- 通過使用以下命令之一從容器中執(zhí)行SQL*Plus命令,可以連接到Oracle數據庫服務器:
$ docker exec -it <oracle-db> sqlplus / as sysdba $ docker exec -it <oracle-db> sqlplus sys/<your_password>@<your_SID> as sysdba $ docker exec -it <oracle-db> sqlplus system/<your_password>@<your_SID> $ docker exec -it <oracle-db> sqlplus pdbadmin/<your_password>@<your_PDBname>
更改SYS用戶的默認密碼
在容器的第一次啟動時,如果沒有提供,將為數據庫生成一個隨機密碼。在創(chuàng)建數據庫并且相應的容器處于健康狀態(tài)后,用戶必須強制更改密碼。
使用docker exec命令,通過調用容器中的setPassword.sh腳本來更改這些帳戶的密碼。請注意,容器必須正在運行。
- 例如:
$ docker exec <oracle-db> ./setPassword.sh <your_password>
瀏覽器登錄 Oracle EM Express
容器中的Oracle數據庫還配置了Oracle Enterprise Manager Database Express(EM Express)。
要訪問EM Express,請使用瀏覽器訪問以下URL:
https://localhost:5500/em/
注:其中 localhost 修改為自己實際的服務器 IP 地址。
點擊鏈接《官方文檔資料》 查閱官方資料關于容器創(chuàng)建后的更多詳細說明。
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
docker 在容器外執(zhí)行某個容器內的某個命令操作
這篇文章主要介紹了docker 在容器外執(zhí)行某個容器內的某個命令操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11Docker容器無法被stop or kill問題的解決方法
這篇文章主要介紹了Docker容器無法被stop or kill問題的解決方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-09-09