在 Azure 门户中预配运行 SQL Server 的 Linux 虚拟机Provision a Linux virtual machine running SQL Server in the Azure portal

适用于:是Azure VM 上的 SQL ServerAPPLIES TO: yesSQL Server on Azure VM

在本快速入门教程中,你将使用 Azure 门户创建装有 SQL Server 2017 的 Linux 虚拟机。In this quickstart tutorial, you use the Azure portal to create a Linux virtual machine with SQL Server 2017 installed. 学习以下内容:You learn the following:

先决条件Prerequisites

如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, create a Trial before you begin.

创建安装了 SQL Server 的 Linux VMCreate a Linux VM with SQL Server installed

  1. 登录 Azure 门户Sign in to the Azure portal.

  2. 在左窗格中,选择“创建资源”。In the left pane, select Create a resource.

  3. 在“创建资源”窗格中选择“计算”。In the Create a resource pane, select Compute.

  4. 选择“特色”标题旁边的“全部查看”。 Select See all next to the Featured heading.

    查看所有 VM 映像

  5. 在搜索框中键入 SQL Server 2019,然后按 Enter 开始搜索 。In the search box, type SQL Server 2019, and select Enter to start the search.

  6. 选择“操作系统” > “Redhat”来限制搜索结果。 Limit the search results by selecting Operating system > Redhat.

    SQL Server 2019 VM 映像的搜索筛选器

  7. 从搜索结果中选择 SQL Server 2019 Linux 映像。Select a SQL Server 2019 Linux image from the search results. 本教程使用 RHEL74 上的 SQL Server 2019。This tutorial uses SQL Server 2019 on RHEL74.

    提示

    Developer 版允许使用 Enterprise 版的功能进行测试或开发,但没有 SQL Server 许可费用。The Developer edition lets you test or develop with the features of the Enterprise edition but no SQL Server licensing costs. 只需支付运行 Linux VM 的费用。You only pay for the cost of running the Linux VM.

  8. 选择“创建”。Select Create.

设置 Linux VMSet up your Linux VM

  1. 在“基本信息”选项卡中,选择自己的订阅资源组In the Basics tab, select your Subscription and Resource Group.

    “基本信息”窗口

  2. 在“虚拟机名称”中,输入新 Linux VM 的名称。In Virtual machine name, enter a name for your new Linux VM.

  3. 然后键入或选择以下值:Then, type or select the following values:

    • 区域:选择合适的 Azure 区域。Region: Select the Azure region that's right for you.

    • 可用性选项:选择最适合应用和数据的可用性与冗余选项。Availability options: Choose the availability and redundancy option that's best for your apps and data.

    • 更改大小:选择此选项以选择计算机大小,完成后,选择“选择”。Change size: Select this option to pick a machine size and when done, choose Select. 有关 VM 计算机大小的详细信息,请参阅 VM 大小For more information about VM machine sizes, see VM sizes.

      选择 VM 大小

    提示

    对于开发和功能测试,请使用 DS2 或更大的 VM 大小。For development and functional testing, use a VM size of DS2 or higher. 若要进行性能测试,则至少使用“DS13”。For performance testing, use DS13 or higher.

    • 身份验证类型:选择“SSH 公钥”。Authentication type: Select SSH public key.

      备注

      可以选择使用“SSH 公钥”或“密码”进行身份验证。You have the choice of using an SSH public key or a Password for authentication. SSH 更安全。SSH is more secure. 有关如何生成 SSH 密钥的说明,请参阅在 Linux 和 Mac 上为 Azure 中的 Linux VM 创建 SSH 密钥For instructions on how to generate an SSH key, see Create SSH keys on Linux and Mac for Linux VMs in Azure.

    • 用户名:输入 VM 的管理员名称。Username: Enter the Administrator name for the VM.

    • SSH 公钥:输入 RSA 公钥。SSH public key: Enter your RSA public key.

    • 公共入站端口:选择“允许所选的端口”,然后在“选择公共入站端口”列表中选择“SSH (22)”端口。 Public inbound ports: Choose Allow selected ports and pick the SSH (22) port in the Select public inbound ports list. 在本快速入门中,必须执行此步骤才能建立连接并完成 SQL Server 配置。In this quickstart, this step is necessary to connect and complete the SQL Server configuration. 如果要远程连接到 SQL Server,则需要在创建虚拟机后,手动允许通过 Internet 进行连接的 Microsoft SQL Server 使用的默认端口 (1433) 的流量。If you want to remotely connect to SQL Server, you will need to manually allow traffic to the default port (1433) used by Microsoft SQL Server for connections over the Internet after the virtual machine is created.

      入站端口

  4. 在后面的其他选项卡中对设置进行任何所需的更改,或保留默认设置。Make any changes you want to the settings in the following additional tabs or keep the default settings.

    • 磁盘Disks
    • 联网Networking
    • ManagementManagement
    • 来宾配置Guest config
    • 标记Tags
  5. 选择“查看 + 创建”。Select Review + create.

  6. 在“查看 + 创建”窗格中,选择“创建”。 In the Review + create pane, select Create.

连接到 Linux VMConnect to the Linux VM

如果已使用 BASH shell,请通过 ssh 命令连接到 Azure VM。If you already use a BASH shell, connect to the Azure VM using the ssh command. 在以下命令中,替换连接到 Linux VM 所需的 VM 用户名和 IP 地址。In the following command, replace the VM user name and IP address to connect to your Linux VM.

ssh azureadmin@40.55.55.555

可以在 Azure 门户中找到 VM 的 IP 地址。You can find the IP address of your VM in the Azure portal.

Azure 门户中的 IP 地址

如果在 Windows 上运行且没有 BASH shell,请安装 SSH 客户端,例如 PuTTY。If you're running on Windows and don't have a BASH shell, install an SSH client, such as PuTTY.

  1. 下载并安装 PuTTYDownload and install PuTTY.

  2. 运行 PuTTY。Run PuTTY.

  3. 在 PuTTY 配置屏幕上,输入 VM 的公共 IP 地址。On the PuTTY configuration screen, enter your VM's public IP address.

  4. 选择“打开”,并根据提示输入用户名和密码。Select Open and enter your username and password at the prompts.

若要详细了解如何连接到 Linux VM,请参阅使用门户在 Azure 上创建 Linux VMFor more information about connecting to Linux VMs, see Create a Linux VM on Azure using the Portal.

备注

如果出现有关不会在注册表中缓存服务器主机密钥的 PuTTY 安全警报,请从以下选项中进行选择。If you see a PuTTY security alert about the server's host key not being cached in the registry, choose from the following options. 如果你信任此主机,请选择“是”将密钥添加到 PuTTy 缓存并继续进行连接。If you trust this host, select Yes to add the key to PuTTy's cache and continue connecting. 如果你只想建立连接一次,而无需将密钥添加到缓存,请选择“否”。If you want to carry on connecting just once, without adding the key to the cache, select No. 如果你不信任此主机,请选择“取消”以放弃连接。If you don't trust this host, select Cancel to abandon the connection.

更改 SA 密码Change the SA password

新的虚拟机使用随机 SA 密码安装 SQL Server。The new virtual machine installs SQL Server with a random SA password. 重置此密码,然后使用 SA 登录名连接到 SQL Server。Reset this password before you connect to SQL Server with the SA login.

  1. 连接到 Linux VM 以后,请打开新的命令终端。After connecting to your Linux VM, open a new command terminal.

  2. 使用以下命令更改 SA 密码:Change the SA password with the following commands:

    sudo systemctl stop mssql-server
    sudo /opt/mssql/bin/mssql-conf set-sa-password
    

    出现提示时,输入新的 SA 密码和密码确认。Enter a new SA password and password confirmation when prompted.

  3. 重启 SQL Server 服务。Restart the SQL Server service.

    sudo systemctl start mssql-server
    

将工具添加到路径(可选)Add the tools to your path (optional)

默认安装了多个 SQL Server ,包括 SQL Server 命令行工具包。Several SQL Server packages are installed by default, including the SQL Server command-line Tools package. 工具包包含 sqlcmd 和 bcp 工具。The tools package contains the sqlcmd and bcp tools. 为了方便,可以选择将工具路径 /opt/mssql-tools/bin/ 添加到 PATH 环境变量。For convenience, you can optionally add the tools path, /opt/mssql-tools/bin/, to your PATH environment variable.

  1. 运行以下命令以修改登录会话和交互式/非登录会话的路径:Run the following commands to modify the PATH for both login sessions and interactive/non-login sessions:

    echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
    echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
    source ~/.bashrc
    

针对远程连接进行配置Configure for remote connections

如果需要远程连接到 Azure VM 上的 SQL Server,必须在网络安全组上配置入站规则。If you need to remotely connect to SQL Server on the Azure VM, you must configure an inbound rule on the network security group. 该规则允许 SQL Server 所侦听的端口(默认为 1433)上的流量。The rule allows traffic on the port on which SQL Server listens (default of 1433). 以下步骤演示如何使用此步骤所对应的 Azure 门户。The following steps show how to use the Azure portal for this step.

提示

如果在预配过程中已在设置中选择了入站端口“MS SQL (1433)”,则已进行这些更改。If you selected the inbound port MS SQL (1433) in the settings during provisioning, these changes have been made for you. 有关如何配置防火墙,可以转到下一部分。You can go to the next section on how to configure the firewall.

  1. 在门户中选择“虚拟机”,然后选择 SQL Server VM。In the portal, select Virtual machines, and then select your SQL Server VM.

  2. 在左侧窗格中的“设置”下,选择“网络”。In the left navigation pane, under Settings, select Networking.

  3. 在“网络”窗口中,选择“入站端口规则”下的“添加入站端口”。 In the Networking window, select Add inbound port under Inbound Port Rules.

    入站端口规则

  4. 在“服务”列表中,选择“MS SQL”。In the Service list, select MS SQL.

    MS SQL 安全组规则

  5. 单击“确定”保存 VM 的规则。Click OK to save the rule for your VM.

打开 RHEL 上的防火墙Open the firewall on RHEL

本教程介绍了如何创建 Red Hat Enterprise Linux (RHEL) VM。This tutorial directed you to create a Red Hat Enterprise Linux (RHEL) VM. 若要远程连接到 RHEL VM,则还需在 Linux 防火墙上打开端口 1433。If you want to connect remotely to RHEL VMs, you also have to open up port 1433 on the Linux firewall.

  1. 连接到 RHEL VM。Connect to your RHEL VM.

  2. 在 BASH shell 中运行以下命令:In the BASH shell, run the following commands:

    sudo firewall-cmd --zone=public --add-port=1433/tcp --permanent
    sudo firewall-cmd --reload
    

后续步骤Next steps

在 Azure 中有了 SQL Server 2017 虚拟机以后,即可使用 sqlcmd 进行本地连接,以便运行 Transact-SQL 查询。Now that you have a SQL Server 2017 virtual machine in Azure, you can connect locally with sqlcmd to run Transact-SQL queries.

如果已将 Azure VM 配置为建立远程 SQL Server 连接,则应可建立远程连接。If you configured the Azure VM for remote SQL Server connections, you should be able to connect remotely. 若要通过示例来了解如何从 Windows 远程连接到 Linux 上的 SQL Server,请参阅使用 Windows 上的 SSMS 连接到 Linux 上的 SQL ServerFor an example of how to connect remotely to SQL Server on Linux from Windows, see Use SSMS on Windows to connect to SQL Server on Linux. 若要通过 Visual Studio Code 进行连接,请参阅使用 Visual Studio Code 创建和运行 SQL Server 的 Transact-SQL 脚本To connect with Visual Studio Code, see Use Visual Studio Code to create and run Transact-SQL scripts for SQL Server

有关 Linux 上的 SQL Server 的其他一般信息,请参阅 Linux 上的 SQL Server 2017 概述For more general information about SQL Server on Linux, see Overview of SQL Server 2017 on Linux. 若要详细了解如何使用 SQL Server 2017 Linux 虚拟机,请参阅 Azure 上的 SQL Server 2017 虚拟机概述For more information about using SQL Server 2017 Linux virtual machines, see Overview of SQL Server 2017 virtual machines on Azure.