iT邦幫忙

0

使用 Docker 建立 Oracle 12c & 新增使用者建立範例資料庫

WM 2020-01-09 14:11:257560 瀏覽
  • 分享至 

  • xImage
  •  

建立Docker

首先要安裝Docker Desktop
需登入才能下載安裝檔。
安裝過程使用預設值,按OK就可以了。
安裝完成後,會詢問是否要開啟 Hyper-V的功能:
https://ithelp.ithome.com.tw/upload/images/20200108/20112573o1LFmekrpj.png

整個過程完成後,會登出。
再次登入後,Docker Desktop會自動啟動,在工具列上會出現鯨魚圖示:
https://ithelp.ithome.com.tw/upload/images/20200108/20112573fvjq27WrGe.png

待啟動完成後,點擊圖示,跳出選單。
需要登入,才能下載Oracle映像檔:
https://ithelp.ithome.com.tw/upload/images/20200108/20112573EKMTQWAgNI.png

切換至Linux container,目前已經在Linux container,所以會顯示Switch to Windows Container:
https://ithelp.ithome.com.tw/upload/images/20200108/201125732oUfZf44kR.png

建立 Oracle 12c

Oracle官方提供了Oracle Database Enterprise Edition的映像檔,可以快速地建置Oracle。
登入後,按下Proceed to Checkout:
https://ithelp.ithome.com.tw/upload/images/20200108/201125739a73SVXUzl.png

填寫基本資料後,按下Get Content:
https://ithelp.ithome.com.tw/upload/images/20200108/20112573G51TuBuXon.png

就可以看到Oracle Docker映像檔的說明文件了,Copy指令:
https://ithelp.ithome.com.tw/upload/images/20200109/20112573MzfpMUxcfG.png

官方提供兩種版本:

  • 完整版
    docker pull store/oracle/database-enterprise:12.2.0.1
    
  • 精簡版<slim>
    docker pull store/oracle/database-enterprise:12.2.0.1-slim
    

下載映像檔:
https://ithelp.ithome.com.tw/upload/images/20200109/20112573bnuw0bMI6j.png

執行Container:

docker run -d -it --name oracle -p 1521:1521 -p 5500:5500 store/oracle/database-enterprise:12.2.0.1-slim

執行docker ps,確認Oracle Container正在運作:
https://ithelp.ithome.com.tw/upload/images/20200109/20112573R5nygKuvVE.png

下載Oracle SQL Developer

開啟Oracle SQL Develop的左上角,新增連線:
https://ithelp.ithome.com.tw/upload/images/20200108/201125737CcBruErkK.png

官方預設如下:

  • 使用者名稱:sys
  • 密碼:Oradoc_db1
  • 角色:SYSDBA
  • SID:ORCLCDB

點選測試,若成功連線,左下角狀態會顯示成功:
https://ithelp.ithome.com.tw/upload/images/20200109/20112573IXiWaJYfyw.png

點選連線,就會出現MyOracle的資料庫連線:
https://ithelp.ithome.com.tw/upload/images/20200108/20112573IhmVNuAgjB.png

進入看看表格的資料:
https://ithelp.ithome.com.tw/upload/images/20200109/20112573pYca2lZB0z.png

新增使用者

執行:

ALTER SESSION SET "_ORACLE_SCRIPT"=TRUE;
CREATE USER wei IDENTIFIED BY password;
GRANT CONNECT, RESOURCE, DBA TO wei;
ALTER SESSION SET "_ORACLE_SCRIPT"=FALSE;

新增連線,輸入相關資訊:

  • 使用者名稱:wei
  • 密碼:password
  • SID:ORCLCDB

點選測試成功:
https://ithelp.ithome.com.tw/upload/images/20200109/20112573tirScGgEmX.png

點選連線,在連線欄位出現WEI連線:
https://ithelp.ithome.com.tw/upload/images/20200109/20112573EXrsvWPXsJ.png

建立範例資料庫

Oracle Sample Database 下載範例資料庫。
壓縮檔有4個檔案:

  • ot_create_user.sql:新增OT使用者,授予權限。
    請修改如下,才會正確執行:

    ALTER SESSION SET "_ORACLE_SCRIPT"=TRUE;
    
    -- create new user
    CREATE USER OT IDENTIFIED BY yourpassword;
    -- grant priviledges
    GRANT CONNECT, RESOURCE, DBA TO OT;
    
    ALTER SESSION SET "_ORACLE_SCRIPT"=FALSE;
    

    新增OT資料庫連線:
    https://ithelp.ithome.com.tw/upload/images/20200109/20112573QE4zR7wkuk.png

  • ot_schema.sql:建立tables schema。
    開啟WEI的SQL工作表:
    https://ithelp.ithome.com.tw/upload/images/20200109/20112573ssNtCD7gKQ.png
    https://ithelp.ithome.com.tw/upload/images/20200109/20112573UQhHQ93MyA.png
    另一篇文章.NET Core 3.1 with Oracle 12c會接續使用該範例資料庫,我們得做些修改,不然會出現幾個類似以下的錯誤:

    The types of the properties specified for the foreign key {'LocationId'} on entity type        'Warehouses' do not match the types of the properties in the principal key {'LocationId'} on entity type 'Locations'.
    

    這是因為PK與FK欄位的長度設定必須吻合,才能順利產出model。

    我們可以刪除或設定欄位長度,這邊選擇設定長度:

    表格 欄位 長度
    locations location_id 12
    employees employee_id 12
    customers customer_id 6
    contacts customer_id 6
    orders salesman_id 12
    orders order_id 12
    products product_id 12
    warehouses warehouse_id 12
  • ot_data.sql:匯入資料。
    在匯入前要執行下方指令,更改Date的表示語系:
    ALTER SESSION SET NLS_DATE_LANGUAGE='american';
    
  • ot_drop.sql:刪除所有表格。

成功建立範例資料庫。
接下來,我們將.NET Core與 Oracle 整合-.NET Core 3.1 with Oracle 12c

參考資訊:
How to Create a User and Grant Permissions in Oracle
如何快速建立 Oracle Database Server 12c R2 資料庫容器並建立使用者


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言