练习 - 配置 Azure SQL 数据库

已完成

在本单元中,你将部署 Azure SQL 逻辑服务器并创建数据库,以利用讨论的功能来保护 Azure SQL 数据库。

设置:使用脚本部署 Azure SQL 数据库

左侧是 Azure Cloud Shell,可通过它使用浏览器与 Azure 进行交互。 在开始练习之前,你将在 Cloud Shell 中运行脚本以在 Azure SQL 数据库中创建带 AdventureWorks 示例数据库的环境。 在脚本中,系统将提示输入新数据库的密码和本地 IP 地址,以使设备能够连接到数据库。

完成这些脚本需要 3-5 分钟。 请务必记下你的 passwordunique IDregion,因为它们将不再显示。

  1. 首先获取本地 IP 地址。 确保已断开与所有 VPN 服务的连接,并在设备上打开本地 PowerShell 终端。 运行以下命令并记下生成的 IP 地址。

    (Invoke-WebRequest -Uri "https://ipinfo.io/ip").Content
    
  2. 在 Cloud Shell 中运行以下命令。 输入一个复杂的密码,并在提示符下输入你在上一步中获得的本地公共 IP 地址。 将“[沙盒资源组名称]”替换为“[沙盒资源组名称]”。

    $adminSqlLogin = "cloudadmin"
    $password = Read-Host "Your username is 'cloudadmin'. Enter a password for your Azure SQL Database server that meets the password requirements"
    # Prompt for local ip address
    $ipAddress = Read-Host "Disconnect your VPN, open PowerShell on your machine and run '(Invoke-WebRequest -Uri "https://ipinfo.io/ip").Content'. Enter the value (include periods) next to 'Address' " 
    # Get resource group and location and random string
    $resourceGroup = Get-AzResourceGroup | Where ResourceGroupName -like "[sandbox resource group name]"
    $resourceGroupName = "[sandbox resource group name]"
    $uniqueID = Get-Random -Minimum 100000 -Maximum 1000000
    $storageAccountName = "mslearnsa"+$uniqueID
    $location = $resourceGroup.Location
    # The logical server name has to be unique in the system
    $serverName = "aw-server$($uniqueID)"
    
  3. 通过在 Cloud Shell 中运行以下代码,以文本文件的形式输出并存储本模块中所需的信息。 粘贴到代码后,可能需要按 Enter,因为默认不运行最后一行。

    Write-Host "Your unique ID for future exercises in this module:" $uniqueID
    Write-Host "Your resource group name is:" $resourceGroupName
    Write-Host "Your resources were deployed in the following region:" $location
    Write-Host "Your server name is:" $serverName
    

    注意

    切记记下你的 passwordunique IDregion。 你将在本模块中使用它们。

  4. 运行以下脚本,使用 AdventureWorks 示例部署 Azure SQL 数据库逻辑服务器,以及我们将在后续单元中讨论的账本数据库。 此脚本还将添加 IP 地址作为防火墙规则、启用 Microsoft Defender for SQL,并创建存储帐户以供后续单元使用。 等待几分钟,以便完成脚本。

    # The sample database name
    $databaseName = "AdventureWorks"
    # The ledger database name
    $databaseName2 = "myLedgerDatabase"
    # The storage account name has to be unique in the system
    $storageAccountName = $("sql$($uniqueID)")
    # Create a new server with a system wide unique server name
    $server = New-AzSqlServer -ResourceGroupName $resourceGroupName `
        -ServerName $serverName `
        -Location $location `
        -SqlAdministratorCredentials $(New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $adminSqlLogin, $(ConvertTo-SecureString -String $password -AsPlainText -Force))
    # Create a server firewall rule that allows access from the specified IP range and all Azure services
    $serverFirewallRule = New-AzSqlServerFirewallRule `
        -ResourceGroupName $resourceGroupName `
        -ServerName $serverName `
        -FirewallRuleName "AllowedIPs" `
        -StartIpAddress $ipAddress -EndIpAddress $ipAddress 
    $allowAzureIpsRule = New-AzSqlServerFirewallRule `
        -ResourceGroupName $resourceGroupName `
        -ServerName $serverName `
        -AllowAllAzureIPs
    # Create databases
    $database = New-AzSqlDatabase  -ResourceGroupName $resourceGroupName `
        -ServerName $serverName `
        -DatabaseName $databaseName `
        -SampleName "AdventureWorksLT" `
        -Edition "GeneralPurpose" -Vcore 2 -ComputeGeneration "Gen5"
    $database2 = New-AzSqlDatabase  -ResourceGroupName $resourceGroupName `
        -ServerName $serverName `
        -DatabaseName $databaseName2 `
        -Edition "GeneralPurpose" -Vcore 2 -ComputeGeneration "Gen5"
    # Enable Microsoft Defender for SQL
    $azureDefender = Enable-AzSqlServerAdvancedDataSecurity `
        -ResourceGroupName $resourceGroupName `
        -ServerName $serverName
    # Create a storage account
    $storageAccount = New-AzStorageAccount -ResourceGroupName $resourceGroupName `
        -AccountName $storageAccountName `
        -Location $location `
        -Type "Standard_LRS"
    
  5. 打开 SQL Server Management Studio (SSMS),然后创建与逻辑服务器的新连接。 对于“服务器名称”,输入 Azure SQL 数据库逻辑服务器的名称(例如 aw-server<unique ID>.database.windows.net)。 如果之前未保存该名称,可能需要转到 Azure 门户获取该名称。

    在 Azure 门户中,搜索“AdventureWorks”以查找数据库及其关联的逻辑服务器。

    将身份验证更改为“SQL Server 身份验证”,然后输入相应的服务器管理员登录名和密码(在上一练习的部署过程中所提供的登录名和密码)。

    选中“记住密码”复选框,然后选择“连接”。

    如何连接到 SSMS 中的 SQL 数据库的屏幕截图。

    注意

    根据本地配置(例如 VPN),客户端 IP 地址可能不同于部署期间 Azure 门户所用的 IP 地址。 如果确实不同,你将看到一条弹出消息,显示“客户端 IP 地址无权访问服务器。 请登录到 Azure 帐户并新建防火墙规则以支持访问。”如果收到此消息,请使用要用于沙盒的帐户登录,并为客户端 IP 地址添加防火墙规则。 可以通过在 SSMS 中使用弹出式向导来完成所有这些步骤。

  6. 连接到 Azure SQL 逻辑服务器后,展开“数据库”文件夹。

  7. 你应会看到已创建的两个数据库,AdventureWorksmyLedgerDatabase。 右键单击 myLedgerDatabase 数据库并选择“新建查询”,然后运行以下命令以创建名为“Account.Balance”的表。 你可以通过在任务栏中选择“执行”来运行查询。

    CREATE SCHEMA [Account];
    GO  
    CREATE TABLE [Account].[Balance]
    (
        [CustomerID] INT NOT NULL PRIMARY KEY CLUSTERED,
        [LastName] VARCHAR (50) NOT NULL,
        [FirstName] VARCHAR (50) NOT NULL,
        [Balance] DECIMAL (10,2) NOT NULL
    )
    WITH 
    (
     SYSTEM_VERSIONING = ON (HISTORY_TABLE = [Account].[BalanceHistory]),
     LEDGER = ON
    );