您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

在 Azure VM 上创建 Oracle 数据库Create an Oracle Database in an Azure VM

本指南详述了如何使用 Azure CLI 通过从 Oracle 市场库映像部署 Azure 虚拟机来创建 Oracle 19c 数据库。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 19c database. 部署服务器后,若要配置 Oracle 数据库,请先通过 SSH 进行连接。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.

如果选择在本地安装并使用 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 位置创建名为 rg-oracle 的资源组 。The following example creates a resource group named rg-oracle in the eastus location.

az group create --name rg-oracle --location eastus

创建虚拟机Create virtual machine

若要创建虚拟机 (VM),请使用 az vm create 命令。To create a virtual machine (VM), use the az vm create command.

以下示例创建一个名为 vmoracle19c 的 VM。The following example creates a VM named vmoracle19c. 此外,它还在默认密钥位置中不存在 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 rg-oracle ^
    --name vmoracle19c ^
    --image Oracle:oracle-database-19-3:oracle-database-19-0904:latest ^
    --size Standard_DS2_v2 ^
    --admin-username azureuser ^
    --generate-ssh-keys ^
    --public-ip-address-allocation static ^
    --public-ip-address-dns-name vmoracle19c

创建 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/rg-oracle/providers/Microsoft.Compute/virtualMachines/vmoracle19c",
  "location": "eastus",
  "macAddress": "00-0D-3A-36-2F-56",
  "powerState": "VM running",
  "privateIpAddress": "10.0.0.4",
  "publicIpAddress": "13.64.104.241",
  "resourceGroup": "rg-oracle"
}

为 Oracle 数据文件和 FRA 创建并附加新磁盘Create and attach a new disk for Oracle datafiles and FRA

az vm disk attach --name oradata01 --new --resource-group rg-oracle --size-gb 128 --sku StandardSSD_LRS --vm-name vmoracle19c

打开用于连接的端口Open ports for connectivity

在此任务中,必须通过设置用于保护 VM 的 Azure 网络安全组,为数据库侦听器和 EM Express 配置一些要使用的外部终结点。In this task you must configure some external endpoints for the database listener and EM Express to use by setting up the Azure Network Security Group that protects the VM.

  1. 若要打开用来远程访问 Oracle 数据库的终结点,请创建一个网络安全组规则,如下所示:To open the endpoint that you use to access the Oracle database remotely, create a Network Security Group rule as follows:

    az network nsg rule create ^
        --resource-group rg-oracle ^
        --nsg-name vmoracle19cNSG ^
        --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 rg-oracle ^
        --nsg-name vmoracle19cNSG ^
        --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 rg-oracle ^
        --name vmoracle19cPublicIP ^
        --query [ipAddress] ^
        --output tsv
    

准备 VM 环境Prepare the VM environment

  1. 连接到 VMConnect to the VM

    若要与 VM 建立 SSH 会话,请使用以下命令。To create an SSH session with the VM, use the following command. 请将 IP 地址替换为 VM 的 publicIpAddress 值。Replace the IP address with the publicIpAddress value for your VM.

    ssh azureuser@<publicIpAddress>
    
  2. 切换到 root 用户Switch to the root user

    sudo su -
    
  3. 检查上次创建的磁盘设备,我们将对其进行格式化以用于保存 Oracle 数据文件Check for last created disk device that we will format for use holding Oracle datafiles

    ls -alt /dev/sd*|head -1
    

    输出将类似于下面:The output will be similar to this:

    brw-rw----. 1 root disk 8, 16 Dec  8 22:57 /dev/sdc
    
  4. 对该设备进行格式化。Format the device. 以 root 用户身份在设备上分开运行As root user run parted on the device

    首先,创建磁盘标签:First create a disk label:

    parted /dev/sdc mklabel gpt
    

    然后,创建跨越整个磁盘的主分区:Then create a primary partition spanning the whole disk:

    parted -a optimal /dev/sdc mkpart primary 0GB 64GB   
    

    最后,通过打印设备的元数据来检查设备的详细信息:Finally check the device details by printing its metadata:

    parted /dev/sdc print
    

    输出应如下所示:The output should look similar to this:

    # parted /dev/sdc print
    Model: Msft Virtual Disk (scsi)
    Disk /dev/sdc: 68.7GB
    Sector size (logical/physical): 512B/4096B
    Partition Table: gpt
    Disk Flags:
    Number  Start   End     Size    File system  Name     Flags
     1      1049kB  64.0GB  64.0GB  ext4         primary
    
  5. 在设备分区上创建一个文件系统Create a filesystem on the device partition

    mkfs -t ext4 /dev/sdc1
    
  6. 创建装入点Create a mount point

    mkdir /u02
    
  7. 装载磁盘Mount the disk

    mount /dev/sdc1 /u02
    
  8. 更改对于装入点的权限Change permissions on the mount point

    chmod 777 /u02
    
  9. 将装载添加到 /etc/fstab 文件。Add the mount to the /etc/fstab file.

    echo "/dev/sdc1               /u02                    ext4    defaults        0 0" >> /etc/fstab
    
  10. 使用公共 IP 和主机名更新 /etc/hosts 文件。Update the */etc/hosts _ file with the public IP and hostname.

    更改公共 IP 和 VM 名称以反映你的实际值:Change the Public IP and VMname to reflect your actual values:

    echo "<Public IP> <VMname>.eastus.cloudapp.azure.com <VMname>" >> /etc/hosts
    
  11. 更新主机名文件Update the hostname file

    使用以下命令将 VM 的域名添加到 /etc/hostname 文件中。Use the following command to add the domain name of the VM to the _ /etc/hostname* file. 这假定你已在 eastus 区域中创建了资源组和 VM:This assumes you have created your resource group and VM in the eastus region:

    sed -i 's/$/\.eastus\.cloudapp\.azure\.com &/' /etc/hostname
    
  12. 打开防火墙端口Open firewall ports

    由于 SELinux 对于市场映像是默认启用的,因此我们需要为数据库侦听端口 1521 和企业管理器快速端口 5502 打开防火墙以接受流量。As SELinux is enabled by default on the Marketplace image we need to open the firewall to traffic for the database listening port 1521, and Enterprise Manager Express port 5502. 以 root 用户身份运行以下命令:Run the following commands as root user:

    firewall-cmd --zone=public --add-port=1521/tcp --permanent
    firewall-cmd --zone=public --add-port=5502/tcp --permanent
    firewall-cmd --reload
    

创建数据库Create the database

该 Oracle 软件已安装在市场映像中。The Oracle software is already installed on the Marketplace image. 如下所述创建一个示例数据库。Create a sample database as follows.

  1. 切换到 oracle 用户:Switch to the oracle user:

    $ sudo su - oracle
    
  2. 启动数据库侦听器Start the database listener

    $ lsnrctl start
    

    输出与下面类似:The output is similar to the following:

    LSNRCTL for Linux: Version 19.0.0.0.0 - Production on 20-OCT-2020 01:58:18
    
    Copyright (c) 1991, 2019, Oracle.  All rights reserved.
    
    Starting /u01/app/oracle/product/19.0.0/dbhome_1/bin/tnslsnr: please wait...
    
    TNSLSNR for Linux: Version 19.0.0.0.0 - Production
    Log messages written to /u01/app/oracle/diag/tnslsnr/vmoracle19c/listener/alert/log.xml
    Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=vmoracle19c.eastus.cloudapp.azure.com)(PORT=1521)))
    
    Connecting to (ADDRESS=(PROTOCOL=tcp)(HOST=)(PORT=1521))
    STATUS of the LISTENER
    ------------------------
    Alias                     LISTENER
    Version                   TNSLSNR for Linux: Version 19.0.0.0.0 - Production
    Start Date                20-OCT-2020 01:58:18
    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/vmoracle19c/listener/alert/log.xml
    Listening Endpoints Summary...
      (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=vmoracle19c.eastus.cloudapp.azure.com)(PORT=1521)))
    The listener supports no services
    The command completed successfully
    
  3. 为 Oracle 数据文件创建一个数据目录:Create a data directory for the Oracle data files:

    mkdir /u02/oradata
    
  4. 运行数据库创建助手:Run the Database Creation Assistant:

    dbca -silent \
       -createDatabase \
       -templateName General_Purpose.dbc \
       -gdbname test \
       -sid test \
       -responseFile NO_VALUE \
       -characterSet AL32UTF8 \
       -sysPassword OraPasswd1 \
       -systemPassword OraPasswd1 \
       -createAsContainerDatabase false \
       -databaseType MULTIPURPOSE \
       -automaticMemoryManagement false \
       -storageType FS \
       -datafileDestination "/u02/oradata/" \
       -ignorePreReqs
    

    创建数据库需要几分钟的时间。It takes a few minutes to create the database.

    你将看到如下所示的输出:You will see output that looks similar to the following:

        Prepare for db operation
       10% complete
       Copying database files
       40% complete
       Creating and starting Oracle instance
       42% complete
       46% complete
       50% complete
       54% complete
       60% complete
       Completing Database Creation
       66% complete
       69% complete
       70% complete
       Executing Post Configuration Actions
       100% complete
       Database creation complete. For details check the logfiles at: /u01/app/oracle/cfgtoollogs/dbca/test.
       Database Information:
       Global Database Name:test
       System Identifier(SID):test
       Look at the log file "/u01/app/oracle/cfgtoollogs/dbca/test/test.log" for further details.
    
  5. 设置 Oracle 变量Set Oracle variables

    在连接之前,需要设置环境变量 ORACLE_SID:Before you connect, you need to set the environment variable ORACLE_SID:

        export ORACLE_SID=test
    

    你还应使用以下命令将 ORACLE_SID 变量添加到 oracle 用户 .bashrc 文件以供将来登录:You should also add the ORACLE_SID variable to the oracle users .bashrc file for future sign-ins using the following command:

    echo "export ORACLE_SID=test" >> ~oracle/.bashrc
    

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 sys as sysdba
    
  2. 在连接后,为 EM Express 设置端口 5502Once connected, set the port 5502 for EM Express

    exec DBMS_XDB_CONFIG.SETHTTPSPORT(5502);
    
  3. 从浏览器连接 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 帐户登录,并选中“以 sysdba 身份”复选框。You can log in by using the SYS account, and check the as sysdba checkbox. 使用你在安装期间设置的密码 OraPasswd1Use the password OraPasswd1 that you set during installation.

    Oracle OEM Express 登录页面的屏幕截图

自动执行数据库启动和关闭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 更改为 YRun the following command to change the automated startup flag from N to Y in the /etc/oratab file:

    sed -i 's/:N/:Y/' /etc/oratab
    
  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/19.0.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
    

清理资源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 备份策略保护数据库Understand how to protect your database in Azure with Oracle Backup Strategies

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

尝试安装和配置 Oracle Automated Storage Management 教程。Try the Installing and Configuring Oracle Automated Storage Management tutorial.