将 Azure SQL Edge 部署为自定义 IoT Edge 模块Deploy Azure SQL Edge as custom IoT Edge Module

Azure SQL Edge 是已优化的关系数据库引擎,更适合 IoT 和 Azure IoT Edge 部署。Azure SQL Edge is a relational database engine optimized for IoT and Azure IoT Edge deployments. 它提供了为 IoT 应用和解决方案创建高性能数据存储和处理层的功能。It provides capabilities to create a high-performance data storage and processing layer for IoT applications and solutions. 本快速入门介绍如何将 Azure SQL Edge 部署为自定义 IoT Edge 模块。This quickstart shows you how to deploy Azure SQL Edge as a custom IoT Edge module. 若要从 Azure 市场部署 Azure SQL Edge,请参阅使用门户部署 Azure SQL EdgeTo deploy Azure SQL Edge from the Azure Marketplace, see Deploy Azure SQL Edge using portal.

开始之前Before you begin

备注

若要将 Azure Linux VM 部署为 IoT Edge 设备,请参阅这篇快速入门指南To deploy an Azure Linux VM as an IoT Edge device, see this quickstart guide.

将 SQL Edge 部署为自定义 IoT Edge 模块Deploy SQL Edge as a custom IoT Edge Module

  1. 登录 Azure 门户并导航到 IoT 中心。Sign in to the Azure portal and navigate to your IoT Hub.

  2. 在左侧窗格的菜单中选择“IoT Edge”,然后单击要向其部署 SQL Edge 的边缘设备。In the left pane, select IoT Edge from the menu, and then click on the Edge Device to which you will deploy SQL Edge.

  3. 在设备页上,单击“设置模块”。On the device page, click Set Modules.

  4. 在“在设备上设置模块:”页的“IoT Edge 模块”下,单击“+ 添加”,然后选择“IoT Edge 模块” 。On the Set Modules on device: page, click on + Add under IoT Edge Modules and select IoT Edge Module.

  5. 在“添加 IoT Edge 模块”边栏选项卡上的“模块设置”下,添加以下详细信息。On the Add IoT Edge Module blade, add the following details under module settings.

    名称Name “值”Value
    IoT Edge 模块名称IoT Edge Module Name AzureSQLEdgeAzureSQLEdge
    映像 URIImage URI mcr.microsoft.com/azure-sql-edge:latestmcr.microsoft.com/azure-sql-edge:latest
    重启策略Restart Policy 通用always
    所需状态Desired State “正在运行”running

    单击“环境变量”Click on Environment Variables

  6. 在“添加 IoT Edge 模块”边栏选项卡上的“环境变量”下,添加以下详细信息。On the Add IoT Edge Module blade, add the following details under Environment Variables.

    参数Parameter Value
    ACCEPT_EULAACCEPT_EULA YY
    PlanIdPlanId Azure SQL Edge PlanId,用于标识要部署的 SQL Edge SKU。The Azure SQL Edge PlanId to identify the SQL Edge Sku to deploy. 可能的值为 asde-developer-on-iot-edge 和 asde-premium-on-iot-edge 。The possible values are asde-developer-on-iot-edge and asde-premium-on-iot-edge . 有关详细信息,请参阅使用环境变量进行配置For more information, see Configure Using environment variables
    MSSQL_SA_PASSWORDMSSQL_SA_PASSWORD 更改默认值,以便为 SQL Edge 管理员帐户指定强密码。Change the default value to specify a strong password for the SQL Edge admin account.
    MSSQL_LCIDMSSQL_LCID 更改默认值,以设置要用于 SQL Edge 的所需语言 ID。Change the default value to set the desired language ID to use for SQL Edge. 例如,1036 为法语。For example, 1036 is French. 有关 SQL 语言 ID 的完整列表,请参阅服务器级排序规则For a full list of SQL Language ID's refer Server Level Collations
    MSSQL_COLLATIONMSSQL_COLLATION 更改默认值,以设置 SQL Edge 的默认排序规则。Change the default value to set the default collation for SQL Edge. 此设置替代语言 ID (LCID) 到排序规则的默认映射。This setting overrides the default mapping of language ID (LCID) to collation. 有关 SQL 排序规则的完整列表,请参阅服务器级排序规则For a full list of SQL Collations refer Server Level Collations

    重要

    请确保正确定义了 PlanId 环境变量。Make sure that PlanId environment variable is correctly defined. 如果未正确定义此值,则 Azure SQL Edge 容器将无法启动。If this value is not defined correctly, the Azure SQL Edge container will fail to start.

    单击“容器创建选项”。Click on Container Create Options.

  7. 在“添加 IoT Edge 模块”边栏选项卡上的“容器创建选项”下,添加以下详细信息。On the Add IoT Edge Module blade, add the following details under Container Create Options. 请确保根据需要更新以下选项。Make sure to update the following options as per requirements.

    • 主机端口:将指定主机端口映射到容器中的端口 1433(默认 SQL 端口)。Host Port : Map the specified host port to port 1433 (default SQL port) in the container.
    • “绑定”和“装载” :如需部署多个 SQL Edge 模块,请确保更新装载选项,以便为永久性卷新建源和目标对。Binds and Mounts : If you need to deploy more than one SQL Edge module, ensure that you update the mounts option to create a new source & target pair for the persistent volume. 若要详细了解装载和卷,请参阅 docker 文档中的使用卷For more information on mounts and volume, refer Use volumes on docker documentation.
    {
    "HostConfig": {
        "CapAdd": [
            "SYS_PTRACE"
        ],
        "Binds": [
            "sqlvolume:/sqlvolume"
        ],
        "PortBindings": {
            "1433/tcp": [
                {
                    "HostPort": "1433"
                }
            ]
        },
        "Mounts": [
            {
                "Type": "volume",
                "Source": "sqlvolume",
                "Target": "/var/opt/mssql"
            }
        ]
    },
    "Env": [
        "MSSQL_AGENT_ENABLED=TRUE",
        "ClientTransportType=AMQP_TCP_Only"
    ]
    }
    
  8. 在“添加 IoT Edge 模块”边栏选项卡上,单击“添加”。On the Add IoT Edge Module blade, click Add.

  9. 如果需要为部署定义路由,则在“在设备上设置模块”页上,单击“下一步:路由 >”。On the Set modules on device page click Next: Routes > if you need to define routes for your deployment. 否则,单击“审阅 + 创建”。Otherwise click Review + Create. 有关配置路由的详细信息,请参阅在 IoT Edge 中部署模块和建立路由For more information on configuring routes, see Deploy modules and establish routes in IoT Edge.

  10. 在“在设备上设置模块”页上,单击“创建” 。On the Set modules on device page, click Create.

连接到 Azure SQL EdgeConnect to Azure SQL Edge

下列步骤在容器内部使用 Azure SQL Edge 命令行工具 sqlcmd 来连接 Azure SQL Edge。The following steps use the Azure SQL Edge command-line tool, sqlcmd, inside the container to connect to Azure SQL Edge.

备注

SQL 命令行工具 (sqlcmd) 在 Azure SQL Edge 容器的 ARM64 版本中不可用。SQL Command line tools (sqlcmd) are not available inside the ARM64 version of Azure SQL Edge containers.

  1. 使用 docker exec -it 命令在运行的容器内部启动交互式 Bash Shell。Use the docker exec -it command to start an interactive bash shell inside your running container. 在下面的示例中,azuresqledge 是由 IoT Edge 模块的 Name 参数指定的名称。In the following example azuresqledge is name specified by the Name parameter of your IoT Edge Module.

    sudo docker exec -it azuresqledge "bash"
    
  2. 在容器内部使用 sqlcmd 进行本地连接。Once inside the container, connect locally with sqlcmd. 默认情况下,sqlcmd 不在路径之中,因此需要指定完整路径。Sqlcmd is not in the path by default, so you have to specify the full path.

    /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P "<YourNewStrong@Passw0rd>"
    

    提示

    可以省略命令行上提示要输入的密码。You can omit the password on the command-line to be prompted to enter it.

  3. 如果成功,应会显示 sqlcmd 命令提示符:1>If successful, you should get to a sqlcmd command prompt: 1>.

创建和查询数据Create and query data

以下部分将引导你使用 sqlcmd 和 Transact-SQL 完成新建数据库、添加数据并运行查询的整个过程。The following sections walk you through using sqlcmd and Transact-SQL to create a new database, add data, and run a query.

新建数据库Create a new database

以下步骤创建一个名为 TestDB 的新数据库。The following steps create a new database named TestDB.

  1. 在 sqlcmd 命令提示符中,粘贴以下 Transact-SQL 命令以创建测试数据库:From the sqlcmd command prompt, paste the following Transact-SQL command to create a test database:

    CREATE DATABASE TestDB
    Go
    
  2. 在下一行中,编写一个查询以返回服务器上所有数据库的名称:On the next line, write a query to return the name of all of the databases on your server:

    SELECT Name from sys.Databases
    Go
    

插入数据Insert data

接下来创建一个新表 Inventory,然后插入两个新行。Next create a new table, Inventory, and insert two new rows.

  1. 在 sqlcmd 命令提示符中,将上下文切换到新的 TestDB 数据库:From the sqlcmd command prompt, switch context to the new TestDB database:

    USE TestDB
    
  2. 创建名为 Inventory 的新表:Create new table named Inventory:

    CREATE TABLE Inventory (id INT, name NVARCHAR(50), quantity INT)
    
  3. 将数据插入新表:Insert data into the new table:

    INSERT INTO Inventory VALUES (1, 'banana', 150); INSERT INTO Inventory VALUES (2, 'orange', 154);
    
  4. 要执行上述命令的类型 GOType GO to execute the previous commands:

    GO
    

选择数据Select data

现在,运行查询以从 Inventory 表返回数据。Now, run a query to return data from the Inventory table.

  1. 通过 sqlcmd 命令提示符输入查询,以返回 Inventory 表中数量大于 152 的行:From the sqlcmd command prompt, enter a query that returns rows from the Inventory table where the quantity is greater than 152:

    SELECT * FROM Inventory WHERE quantity > 152;
    
  2. 执行此命令:Execute the command:

    GO
    

退出 sqlcmd 命令提示符Exit the sqlcmd command prompt

  1. 要结束 sqlcmd 会话,请键入 QUITTo end your sqlcmd session, type QUIT:

    QUIT
    
  2. 要在容器中退出交互式命令提示,请键入 exitTo exit the interactive command-prompt in your container, type exit. 退出交互式 Bash Shell 后,容器将继续运行。Your container continues to run after you exit the interactive bash shell.

从容器外连接Connect from outside the container

可以从支持 SQL 连接的任何外部 Linux、Windows 或 macOS 工具连接 Azure SQL Edge 实例,并对其运行 SQL 查询。You can connect and run SQL queries against your Azure SQL Edge instance from any external Linux, Windows, or macOS tool that supports SQL connections. 有关从外部连接到 SQL Edge 容器的详细信息,请参阅连接和查询 Azure SQL EdgeFor more information on connecting to a SQL Edge container from outside, refer Connect and Query Azure SQL Edge.

在本快速入门中,你在 IoT Edge 设备上部署了 SQL Edge 模块。In this quickstart, you deployed a SQL Edge Module on an IoT Edge device.

后续步骤Next Steps