在 Azure VM 中建立 Oracle 資料庫Create an Oracle Database in an Azure VM

本指南詳述如何使用 Azure CLI 從 Oracle Marketplace 資源庫映像部署 Azure 虛擬機器,以便建立 Oracle 12c 資料庫。This guide details using the Azure CLI to deploy an Azure virtual machine from the Oracle marketplace gallery image in order to create an Oracle 12c database. 部署伺服器之後,您會透過 SSH 連接,以設定 Oracle 資料庫。Once the server is deployed, you will connect via SSH in order to configure the Oracle database.

如果您沒有 Azure 訂用帳戶,請在開始前建立 免費帳戶If you don't have an Azure subscription, create a free account before you begin.

使用 Azure Cloud ShellUse Azure Cloud Shell

Azure Cloud Shell 是裝載於 Azure 中的互動式殼層環境,可在瀏覽器中使用。Azure hosts Azure Cloud Shell, an interactive shell environment that you can use through your browser. 您可以使用 Bash 或 PowerShell 搭配 Cloud Shell,與 Azure 服務共同使用。You can use either Bash or PowerShell with Cloud Shell to work with Azure services. Azure Cloud Shell 已預先安裝一些命令,可讓您執行本文提到的程式碼,而不必在本機環境上安裝任何工具。You can use the Cloud Shell preinstalled commands to run the code in this article without having to install anything on your local environment.

要啟動 Azure Cloud Shell:To start Azure Cloud Shell:

選項Option 範例/連結Example/Link
選取程式碼區塊右上角的 [試試看] 。Select Try It in the upper-right corner of a code block. 選取 [試用] 並不會自動將程式碼複製到 Cloud Shell 中。Selecting Try It doesn't automatically copy the code to Cloud Shell. Azure Cloud Shell 的試試看範例
請前往 https://shell.azure.com 或選取 [啟動 Cloud Shell] 按鈕,在瀏覽器中開啟 Cloud Shell。Go to https://shell.azure.com, or select the Launch Cloud Shell button to open Cloud Shell in your browser. 在新視窗中啟動 Cloud ShellLaunch Cloud Shell in a new window
選取 Azure 入口網站右上角功能表列中的 [Cloud Shell] 按鈕。Select the Cloud Shell button on the top-right menu bar in the Azure portal. Azure 入口網站中的 [Cloud Shell] 按鈕

若要在 Azure Cloud Shell 中執行本文中的程式碼:To run the code in this article in Azure Cloud Shell:

  1. 啟動 Cloud Shell。Start Cloud Shell.

  2. 選取程式碼區塊上的 [複製] 按鈕,複製程式碼。Select the Copy button on a code block to copy the code.

  3. 在 Windows 和 Linux 上選取 Ctrl+Shift+V;或在 macOS 上選取 Cmd+Shift+V,將程式碼貼到 Cloud Shell 工作階段中。Paste the code into the Cloud Shell session by selecting Ctrl+Shift+V on Windows and Linux or by selecting Cmd+Shift+V on macOS.

  4. 選取 Enter 鍵執行程式碼。Select Enter to run the code.

如果您選擇在本機安裝和使用 CLI,本快速入門會要求您執行 Azure CLI 2.0.4 版或更新版本。If you choose to install and use the CLI locally, this quickstart requires that you are running the Azure CLI version 2.0.4 or later. 執行 az --version 以尋找版本。Run az --version to find the version. 如果您需要安裝或升級,請參閱安裝 Azure CLIIf you need to install or upgrade, see Install Azure CLI.

建立資源群組Create a resource group

使用 az group create 命令來建立資源群組。Create a resource group with the az group create command. Azure 資源群組是在其中部署與管理 Azure 資源的邏輯容器。An Azure resource group is a logical container into which Azure resources are deployed and managed.

下列範例會在 eastus 位置建立名為 myResourceGroup 的資源群組。The following example creates a resource group named myResourceGroup in the eastus location.

az group create --name myResourceGroup --location eastus

建立虛擬機器Create virtual machine

若要建立虛擬機器,請使用 az vm create 命令。To create a virtual machine (VM), use the az vm create command.

下列範例會建立名為 myVM 的 VM。The following example creates a VM named myVM. 如果預設的金鑰位置還沒有 SSH 金鑰的話,此範例也會建立這些金鑰。It also creates SSH keys, if they do not already exist in a default key location. 若要使用一組特定金鑰,請使用 --ssh-key-value 選項。To use a specific set of keys, use the --ssh-key-value option.

az vm create \
    --resource-group myResourceGroup \
    --name myVM \
    --image Oracle:Oracle-Database-Ee:12.1.0.2:latest \
    --size Standard_DS2_v2 \
    --admin-username azureuser \
    --generate-ssh-keys

在您建立 VM 後,Azure CLI 會顯示類似下列範例的資訊。After you create the VM, Azure CLI displays information similar to the following example. 請記下 publicIpAddress 的值。Note the value for publicIpAddress. 您必須使用此位址來存取 VM。You use this address to access the VM.

{
  "fqdns": "",
  "id": "/subscriptions/{snip}/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM",
  "location": "westus",
  "macAddress": "00-0D-3A-36-2F-56",
  "powerState": "VM running",
  "privateIpAddress": "10.0.0.4",
  "publicIpAddress": "13.64.104.241",
  "resourceGroup": "myResourceGroup"
}

連接至 VMConnect to the VM

若要對 VM 建立 SSH 工作階段,請使用下列命令。To create an SSH session with the VM, use the following command. 以 VM 的 publicIpAddress 值取代 IP 位址。Replace the IP address with the publicIpAddress value for your VM.

ssh azureuser@<publicIpAddress>

建立資料庫Create the database

Marketplace 映像上已安裝 Oracle 軟體。The Oracle software is already installed on the Marketplace image. 建立範例資料庫,如下所示。Create a sample database as follows.

  1. 切換至 oracle 超級使用者,然後將接聽程式初始化以啟用記錄功能:Switch to the oracle superuser, then initialize the listener for logging:

    $ sudo su - oracle
    $ lsnrctl start
    

    輸出大致如下:The output is similar to the following:

    Copyright (c) 1991, 2014, Oracle.  All rights reserved.
    
    Starting /u01/app/oracle/product/12.1.0/dbhome_1/bin/tnslsnr: please wait...
    
    TNSLSNR for Linux: Version 12.1.0.2.0 - Production
    Log messages written to /u01/app/oracle/diag/tnslsnr/myVM/listener/alert/log.xml
    Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=myVM.twltkue3xvsujaz1bvlrhfuiwf.dx.internal.cloudapp.net)(PORT=1521)))
    
    Connecting to (ADDRESS=(PROTOCOL=tcp)(HOST=)(PORT=1521))
    STATUS of the LISTENER
    ------------------------
    Alias                     LISTENER
    Version                   TNSLSNR for Linux: Version 12.1.0.2.0 - Production
    Start Date                23-MAR-2017 15:32:08
    Uptime                    0 days 0 hr. 0 min. 0 sec
    Trace Level               off
    Security                  ON: Local OS Authentication
    SNMP                      OFF
    Listener Log File         /u01/app/oracle/diag/tnslsnr/myVM/listener/alert/log.xml
    Listening Endpoints Summary...
    (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=myVM.twltkue3xvsujaz1bvlrhfuiwf.dx.internal.cloudapp.net)(PORT=1521)))
    The listener supports no services
    The command completed successfully
    
  2. 建立資料庫︰Create the database:

    dbca -silent \
           -createDatabase \
           -templateName General_Purpose.dbc \
           -gdbname cdb1 \
           -sid cdb1 \
           -responseFile NO_VALUE \
           -characterSet AL32UTF8 \
           -sysPassword OraPasswd1 \
           -systemPassword OraPasswd1 \
           -createAsContainerDatabase true \
           -numberOfPDBs 1 \
           -pdbName pdb1 \
           -pdbAdminPassword OraPasswd1 \
           -databaseType MULTIPURPOSE \
           -automaticMemoryManagement false \
           -storageType FS \
           -ignorePreReqs
    

    建立資料庫需要幾分鐘的時間。It takes a few minutes to create the database.

  3. 設定 Oracle 變數Set Oracle variables

在連線之前,您需要設定兩個環境變數︰ORACLE_HOME 和 ORACLE_SID。Before you connect, you need to set two environment variables: ORACLE_HOME and ORACLE_SID.

ORACLE_HOME=/u01/app/oracle/product/12.1.0/dbhome_1; export ORACLE_HOME
ORACLE_SID=cdb1; export ORACLE_SID

您也可以將 ORACLE_HOME 和 ORACLE_SID 變數新增至 .bashrc 檔案。You also can add ORACLE_HOME and ORACLE_SID variables to the .bashrc file. 這會儲存環境變數以供未來登入。請確認已使用您選擇的編輯器,將下列陳述式新增至 ~/.bashrc 檔案。This would save the environment variables for future sign-ins. Confirm the following statements have been added to the ~/.bashrc file using editor of your choice.

# Add ORACLE_HOME. 
export ORACLE_HOME=/u01/app/oracle/product/12.1.0/dbhome_1 
# Add ORACLE_SID. 
export ORACLE_SID=cdb1 

Oracle EM Express 連線能力Oracle EM Express connectivity

對於您可用來瀏覽資料庫的 GUI 管理工具,請設定 Oracle EM Express。For a GUI management tool that you can use to explore the database, set up Oracle EM Express. 若要連線到 Oracle EM Express,您必須先在 Oracle 中設定連接埠。To connect to Oracle EM Express, you must first set up the port in Oracle.

  1. 使用 sqlplus 連線到您的資料庫:Connect to your database using sqlplus:

    sqlplus / as sysdba
    
  2. 連線後,請針對 EM Express 設定連接埠 5502Once connected, set the port 5502 for EM Express

    exec DBMS_XDB_CONFIG.SETHTTPSPORT(5502);
    
  3. 如果容器 PDB1 尚未開啟,請加以開啟,但是先檢查狀態:Open the container PDB1 if not already opened, but first check the status:

    select con_id, name, open_mode from v$pdbs;
    

    輸出大致如下:The output is similar to the following:

      CON_ID NAME                           OPEN_MODE 
      ----------- ------------------------- ---------- 
      2           PDB$SEED                  READ ONLY 
      3           PDB1                      MOUNT
    
  4. 如果 PDB1 的 OPEN_MODE 不是「讀寫」,則執行下列命令以開啟 PDB1:If the OPEN_MODE for PDB1 is not READ WRITE, then run the followings commands to open PDB1:

     alter session set container=pdb1;
     alter database open;
    

您需要鍵入 quit 結束 sqlplus 工作階段,並鍵入 exit 登出 oracle 使用者。You need to type quit to end the sqlplus session and type exit to logout of the oracle user.

自動進行資料庫啟動和關機Automate database startup and shutdown

當您重新啟動 VM 時,Oracle 資料庫預設不會自動啟動。The Oracle database by default doesn't automatically start when you restart the VM. 若要將 Oracle 資料庫設定為自動啟動,請先以 root 的身分登入。To set up the Oracle database to start automatically, first sign in as root. 接著,建立並更新一些系統檔案。Then, create and update some system files.

  1. 以 root 的身分登入Sign on as root

    sudo su -
    
  2. 使用您最愛的編輯器,編輯 /etc/oratab 檔案,並將預設的 N 變更為 YUsing your favorite editor, edit the file /etc/oratab and change the default N to Y:

    cdb1:/u01/app/oracle/product/12.1.0/dbhome_1:Y
    
  3. 建立名為 /etc/init.d/dbora 的檔案,並貼上下列內容︰Create a file named /etc/init.d/dbora and paste the following contents:

    #!/bin/sh
    # chkconfig: 345 99 10
    # Description: Oracle auto start-stop script.
    #
    # Set ORA_HOME to be equivalent to $ORACLE_HOME.
    ORA_HOME=/u01/app/oracle/product/12.1.0/dbhome_1
    ORA_OWNER=oracle
    
    case "$1" in
    'start')
        # Start the Oracle databases:
        # The following command assumes that the Oracle sign-in
        # will not prompt the user for any values.
        # Remove "&" if you don't want startup as a background process.
        su - $ORA_OWNER -c "$ORA_HOME/bin/dbstart $ORA_HOME" &
        touch /var/lock/subsys/dbora
        ;;
    
    'stop')
        # Stop the Oracle databases:
        # The following command assumes that the Oracle sign-in
        # will not prompt the user for any values.
        su - $ORA_OWNER -c "$ORA_HOME/bin/dbshut $ORA_HOME" &
        rm -f /var/lock/subsys/dbora
        ;;
    esac
    
  4. 使用 chmod 變更檔案的權限,如下所示:Change permissions on files with chmod as follows:

    chgrp dba /etc/init.d/dbora
    chmod 750 /etc/init.d/dbora
    
  5. 建立用於啟動和關閉的符號連結,如下所示:Create symbolic links for startup and shutdown as follows:

    ln -s /etc/init.d/dbora /etc/rc.d/rc0.d/K01dbora
    ln -s /etc/init.d/dbora /etc/rc.d/rc3.d/S99dbora
    ln -s /etc/init.d/dbora /etc/rc.d/rc5.d/S99dbora
    
  6. 若要測試您的變更,請重新啟動 VM:To test your changes, restart the VM:

    reboot
    

開始連接埠進行連線Open ports for connectivity

最後一項工作是設定一些外部端點。The final task is to configure some external endpoints. 若要設定可保護 VM 的 Azure 網路安全性群組,請先結束 VM 中您的 SSH 工作階段 (應已在前一個步驟中重新啟動時被移出 SSH)。To set up the Azure Network Security Group that protects the VM, first exit your SSH session in the VM (should have been kicked out of SSH when rebooting in previous step).

  1. 若要開啟您用來從遠端存取 Oracle 資料庫的端點,請使用 az network nsg rule create 建立網路安全性群組規則,如下所示:To open the endpoint that you use to access the Oracle database remotely, create a Network Security Group rule with az network nsg rule create as follows:

    az network nsg rule create \
        --resource-group myResourceGroup\
        --nsg-name myVmNSG \
        --name allow-oracle \
        --protocol tcp \
        --priority 1001 \
        --destination-port-range 1521
    
  2. 若要開啟您用來從遠端存取 Oracle EM Express 的端點,請使用 az network nsg rule create 建立網路安全性群組規則,如下所示:To open the endpoint that you use to access Oracle EM Express remotely, create a Network Security Group rule with az network nsg rule create as follows:

    az network nsg rule create \
        --resource-group myResourceGroup \
        --nsg-name myVmNSG \
        --name allow-oracle-EM \
        --protocol tcp \
        --priority 1002 \
        --destination-port-range 5502
    
  3. 如有需要,請使用 az network public-ip show 再次取得 VM 的公用 IP 位址,如下所示:If needed, obtain the public IP address of your VM again with az network public-ip show as follows:

    az network public-ip show \
        --resource-group myResourceGroup \
        --name myVMPublicIP \
        --query [ipAddress] \
        --output tsv
    
  4. 從您的瀏覽器連接 EM Express。Connect EM Express from your browser. 確定您的瀏覽器與 EM Express 相容 (需要安裝 Flash):Make sure your browser is compatible with EM Express (Flash install is required):

    https://<VM ip address or hostname>:5502/em
    

您可以使用 SYS 帳戶進行登入,然後勾選 as sysdba 核取方塊。You can log in by using the SYS account, and check the as sysdba checkbox. 使用您在安裝期間設定的密碼 OraPasswd1。Use the password OraPasswd1 that you set during installation.

Oracle OEM Express 登入頁面的螢幕擷取畫面

清除資源Clean up resources

完成在 Azure 上探索第一個 Oracle 資料庫而且不再需要 VM 之後,就可以使用 az group delete 命令移除資源群組、VM 和所有相關資源。Once you have finished exploring your first Oracle database on Azure and the VM is no longer needed, you can use the az group delete command to remove the resource group, VM, and all related resources.

az group delete --name myResourceGroup

後續步驟Next steps

深入了解 Azure 上的其他 Oracle 解決方案Learn about other Oracle solutions on Azure.

嘗試安裝和設定 Oracle Automated Storage Management 教學課程。Try the Installing and Configuring Oracle Automated Storage Management tutorial.