網頁

2020/5/10

Windows 7 Docker 安裝Oracle 12c docker container

本篇記錄在Windows 7上建立Oracle 12的docker container環境並使用SQL Developer連線。

範例環境:

  • Windows 7 64 Bit
  • Docker Machine version 0.16.1, build cce350d7
  • Oracle VirtualBox 版本 5.2.20 r125813 (Qt5.6.2)

本範例的Docker Machine及VirtualBox是由Docker Toolbox安裝


使用docker-machine create --driver=virtualbox oracle-docker建立新machine並命名為oracle-docker

C:\Users\matt\>docker-machine create --driver=virtualbox oracle-docker

使用docker-machine ls列出現有的machine。oracle-docker即為新建的machine。

C:\Users\matt\>docker-machine ls
NAME          ACTIVE   DRIVER       STATE     URL                         SWARM  DOCKER     ERRORS
default       -        virtualbox   Stopped                                      Unknown
demo-docker   -        virtualbox   Stopped                                      Unknown
oracle-docker   *      virtualbox   Running   tcp://192.168.99.100:2376          v19.03.5

URL欄位可看到oracle-docker的連線對外IP為192.168.99.100,也是最後在SQL Developer要連線的位址。

使用docker-machine ssh oracle-docker連線到oracle-docker machine的docker client。

C:\Users\matt\>docker-machine ssh oracle-docker
   ( '>')
  /) TC (\   Core is distributed with ABSOLUTELY NO WARRANTY.
 (/-_--_-\)           www.tinycorelinux.net


在Docker Hub建立好帳號,然號在docker client輸入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: ************
Password:
WARNING! Your password will be stored unencrypted in /home/docker/.docker/config.json.
Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

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



填寫個人資訊併勾選右側的同意條款後按[Get Content]。

複製右側的docker指令docker pull store/oracle/database-enterprise:12.2.0.1貼到docker client執行來下載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

1B7fe12c04: Pulling fs layer
1B56e8e792: Pulling fs layer
1Ba1a28025: Pulling fs layer
1Be4ed872e: Pulling fs layer
1BDigest: 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 images
REPOSITORY                          TAG                 IMAGE ID            CREATED             SIZE
store/oracle/database-enterprise    12.2.0.1            12a359cd0528        2 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。並以設定docker container的port mapping為 1521->1521

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

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

$ docker ps
CONTAINER ID        IMAGE                                       COMMAND                  CREATED             STATUS                             PORTS                               NAMES
0537923ae5dc        store/oracle/database-enterprise:12.2.0.1   "/bin/sh -c '/bin/ba…"   49 seconds ago      Up 49 seconds (health: starting)   0.0.0.0:1521->1521/tcp, 5500/tcp    oracle-db

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

執行docker exec -it <Oracle-DB> 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 Sun May 10 14:11:40 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名稱。

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

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

離開SQL*Plus。

SQL> exit

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


建立新的使用者demo1,密碼demo1,並賦予權限。

ALTER SESSION SET "_ORACLE_SCRIPT"=TRUE;

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

ALTER SESSION SET "_ORACLE_SCRIPT"=FALSE;

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


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

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

建立成功。



參考:

沒有留言:

張貼留言