AdSense

網頁

2020/10/10

Mac Docker 安裝Oracle 12c docker container

在macOS建立Oracle 12的docker container環境並使用Oracle SQL Developer連線。

範例環境:

  • macOS Catalina Version 10.15.5
  • Docker version 19.03.12, build 48a66213fe

Docker Hub建立好帳號並登入。



在mac終端機bash輸入docker login登入Docker Hub。

$ docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: <docker_hub_username>
Password:
Login Succeeded

在Docker Hub搜尋Oracle Database Enterprise Edition,然後點選[Proceed to Checkout]。



進入Oracle image頁面要填寫個人資訊並勾選右側的同意條款後按[Get Content]。

複製右側的docker指令docker pull store/oracle/database-enterprise:12.2.0.1貼到bash執行來下載Oracle database的image檔。image檔很大(3.44GB)要等一陣子才能下載完。



$ docker pull store/oracle/database-enterprise:12.2.0.1
12.2.0.1: Pulling from store/oracle/database-enterprise
4ce27fe12c04: Pull complete
9d3556e8e792: Pull complete
fc60a1a28025: Pull complete
0c32e4ed872e: Pull complete
b465d9b6e399: Pull complete
Digest: sha256:40760ac70dba2c4c70d0c542e42e082e8b04d9040d91688d63f728af764a2f5d
Status: Downloaded newer image for store/oracle/database-enterprise:12.2.0.1
docker.io/store/oracle/database-enterprise:12.2.0.1

使用docker images檢視下載的Oracle資料庫docker image映像檔。

REPOSITORY                         TAG                 IMAGE ID            CREATED             SIZE
store/oracle/database-enterprise   12.2.0.1            12a359cd0528        3 years ago         3.44GB

執行docker run -d -it --name <container_name> -p 1521:1521 store/oracle/database-enterprise:12.2.0.1來啟動container。

<container_name>是自訂的container的名稱,例如這邊命名為oracle-db
-p 1521:1521為設定docker container的port 1521 mapping到host的1521。

$ docker run -d -it --name oracle-db -p 1521:1521 store/oracle/database-enterprise:12.2.0.1
aa1e3abc7a08b8d4a534cd166b1c6c41c436b3629992d5bb88bc654dba70a88c

使用docker ps檢視運行中的container。

$ docker ps
CONTAINER ID        IMAGE                                       COMMAND                  CREATED             STATUS                   PORTS                              NAMES
aa1e3abc7a08        store/oracle/database-enterprise:12.2.0.1   "/bin/sh -c '/bin/ba…"   3 minutes ago       Up 3 minutes (healthy)   0.0.0.0:1521->1521/tcp, 5500/tcp   oracle-db

PORTS欄位可以看到container對外以0.0.0.0:1521 port mapping到host的1521 port及Oracle內部SQLNet protocol的port 5500

執行docker exec -it <container_name> bash -c "source /home/oracle/.bashrc; sqlplus /nolog"以container中的SQL*Plus連接到Oracle資料庫。

$ docker exec -it oracle-db bash -c "source /home/oracle/.bashrc; sqlplus /nolog"

SQL*Plus: Release 12.2.0.1.0 Production on Sat Oct 10 08:59:05 2020

Copyright (c) 1982, 2016, Oracle.  All rights reserved.

SQL>

輸入connect sys as sysdba以預設帳號sys登入Oracle資料庫,預設密碼Oradoc_db1

SQL> connect sys as sysdba
Enter password:
Connected.

輸入alter user sys identified by <new-password>更改sys的登入密碼。<new-password>為新密碼,這邊使用123

SQL> alter user sys identified by 123;

User altered.

檢視資料庫版本。

SQL> select banner from v$version where rownum = 1;

BANNER
--------------------------------------------------------------------------------
Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production

檢視資料庫的SERVICE_NAME名稱,預設為ORCLCDB.localdomain

SQL> select value from v$parameter where name='service_names';

VALUE
--------------------------------------------------------------------------------
ORCLCDB.localdomain

輸入exit離開SQL*Plus。

SQL> exit
Disconnected from Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production

在本機開啟SQL Developer,使用帳號sys,密碼123登入。角色要選擇[SYSDBA]。服務名稱設為ORCLCDB.localdomain



在SQL Developer執行下面指令建立新的使用者admin123,密碼admin123並賦予權限。

ALTER SESSION SET "_ORACLE_SCRIPT"=TRUE;

CREATE USER admin123 IDENTIFIED BY admin123;
GRANT CONNECT, RESOURCE, DBA TO admin123;

ALTER SESSION SET "_ORACLE_SCRIPT"=FALSE;



然後改用新的使用者admin123登入。



建立一個資料表測試看看。

create table DEMO_TABLE (
    ID NUMBER(19) NOT NULL,
    NAME VARCHAR2(50) NOT NULL
);



建立成功。




沒有留言:

AdSense