连接和查询 Azure SQL EdgeConnect and query Azure SQL Edge

在 Azure SQL Edge 中,在部署容器后,可以从以下任意位置连接到数据库引擎:In Azure SQL Edge, after you deploy a container, you can connect to the database engine from any of the following locations:

  • 在容器内部Inside the container
  • 从另一个 Docker 容器(在同一主机上运行)From another Docker container running on the same host
  • 从主机From the host machine
  • 从网络上的任何其他客户端计算机From any other client machine on the network

连接到 Azure SQL Edge 的工具Tools to connect to Azure SQL Edge

可以通过以下任一常用工具连接到 Azure SQL Edge 实例:You can connect to an instance of Azure SQL Edge instance from any of these common tools:

  • sqlcmd:sqlcmd 客户端工具已包含在 Azure SQL Edge 的容器映像中。sqlcmd: sqlcmd client tools are already included in the container image of Azure SQL Edge. 如果使用交互式 bash shell 附加到正在运行的容器,则可以在本地运行这些工具。If you attach to a running container with an interactive bash shell, you can run the tools locally. SQL 客户端工具在 ARM64 平台上不可用,因为 SQL Edge 容器的 ARM64 版本中不包含这些工具。SQL client tools are NOT available on the ARM64 platform, as such they are not included in the ARM64 version of the SQL Edge containers.
  • SQL Server Management StudioSQL Server Management Studio
  • Azure Data StudioAzure Data Studio
  • Visual Studio CodeVisual Studio Code

若要从网络计算机连接到 Azure SQL Edge 数据库引擎,你需要以下各项:To connect to an Azure SQL Edge database engine from a network machine, you need the following:

  • 主机的 IP 地址或网络名称:这是运行 Azure SQL Edge 容器的主机。IP Address or network name of the host machine: This is the host machine where the Azure SQL Edge container is running.

  • Azure SQL Edge 容器主机端口映射:这是 Docker 容器端口到主机端口的映射。Azure SQL Edge container host port mapping: This is the mapping for the Docker container port to a port on the host. 在容器内,Azure SQL Edge 始终映射到端口 1433。Within the container, Azure SQL Edge is always mapped to port 1433. 如果需要,你可以更改此端口。You can change this if you want to. 若要更改端口号,请在 Azure IoT Edge 中更新 Azure SQL Edge 模块的“容器创建选项”。To change the port number, update the Container Create Options for the Azure SQL Edge module in Azure IoT Edge. 在下面的示例中,容器上的端口 1433 映射到主机上的端口 1600。In the following example, port 1433 on the container is mapped to port 1600 on the host.

    {
        "PortBindings": {
          "1433/tcp": [
            {
              "HostPort": "1600"
            }
          ]
        }
    }
    
  • Azure SQL Edge 实例的 SA 密码:这是在部署 Azure SQL Edge 期间为 SA_PASSWORD 环境变量指定的值。SA password for the Azure SQL Edge instance: This is the value specified for the SA_PASSWORD environment variable during deployment of Azure SQL Edge.

从容器内连接到数据库引擎Connect to the database engine from within the container

SQL Server 命令行工具包括在 Azure SQL Edge 的容器映像中。The SQL Server command-line tools are included in the container image of Azure SQL Edge. 如果使用交互式命令提示符进行附加到此容器的操作,则可在本地运行这些工具。If you attach to the container with an interactive command prompt, you can run the tools locally. SQL 客户端工具在 ARM64 平台上不可用,因为 SQL Edge 容器的 ARM64 版本中不包含这些工具。SQL client tools are NOT available on the ARM64 platform, as such they are not included in the ARM64 version of the 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. 在下面的示例中,e69e056c702d 是容器 ID。In the following example, e69e056c702d is the container ID.

    docker exec -it <Azure SQL Edge container ID or name> /bin/bash
    

    提示

    并非始终需要指定完整的容器 ID。You don't always have to specify the entire container ID. 只需指定能够唯一标识它的足够字符即可。You only have to specify enough characters to uniquely identify it. 因此,在本示例中,使用 e6e69 足矣,无需使用完整 ID。So in this example, it might be enough to use e6 or e69, rather than the full ID.

  2. 当在容器内部时,使用 sqlcmd 在本地进行连接。When you're inside the container, connect locally with sqlcmd. 默认情况下,sqlcmd 不在路径之中,因此需要指定完整路径。Sqlcmd isn't in the path by default, so you have to specify the full path.

    /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P '<YourPassword>'
    
  3. 使用 sqlcmd 完成操作后,键入 exitWhen you're finished with sqlcmd, type exit.

  4. 使用交互式命令提示符完成操作后,键入 exitWhen you're finished with the interactive command prompt, type exit. 退出交互式 Bash Shell 后,容器将继续运行。Your container continues to run after you exit the interactive bash shell.

从同一主机上的另一个容器连接到 Azure SQL EdgeConnect to Azure SQL Edge from another container on the same host

由于在同一主机上运行的两个容器位于同一 Docker 网络上,因此可以使用该服务的容器名称和端口地址轻松访问这些容器。Because two containers that are running on the same host are on the same Docker network, you can easily access them by using the container name and the port address for the service. 例如,如果从同一主机上的另一个 Python 模块(容器)连接到 Azure SQL Edge 的实例,则可以使用类似于以下内容的连接字符串。For example, if you're connecting to the instance of Azure SQL Edge from another python module (container) on the same host, you can use a connection string similar to the following. (此示例假定 Azure SQL Edge 配置为在默认端口上侦听。)(This example assumes that Azure SQL Edge is configured to listen on the default port.)


import pyodbc
server = 'MySQLEdgeContainer' # Replace this with the actual name of your SQL Edge Docker container
username = 'sa' # SQL Server username
password = 'MyStrongestP@ssword' # Replace this with the actual SA password from your deployment
database = 'MyEdgeDatabase' # Replace this with the actual database name from your deployment. If you do not have a database created, you can use Master database.
db_connection_string = "Driver={ODBC Driver 17 for SQL Server};Server=" + server + ";Database=" + database + ";UID=" + username + ";PWD=" + password + ";"
conn = pyodbc.connect(db_connection_string, autocommit=True)

从另一台网络计算机连接到 Azure SQL EdgeConnect to Azure SQL Edge from another network machine

你可能需要从网络上的另一台计算机连接到 Azure SQL Edge 的实例。You might want to connect to the instance of Azure SQL Edge from another machine on the network. 为此,请使用 Docker 主机的 IP 地址和 Azure SQL Edge 容器映射到的主机端口。To do so, use the IP address of the Docker host and the host port to which the Azure SQL Edge container is mapped. 例如,如果 Docker 主机的 IP 地址是 xxx.xxx.xxx.xxx,并且 Azure SQL Edge 容器映射到主机端口 1600,则 Azure SQL Edge 实例的服务器地址将是 xxx.xxx.xxx.xxx,1600。For example, if the IP address of the Docker host is xxx.xxx.xxx.xxx, and the Azure SQL Edge container is mapped to host port 1600, then the server address for the instance of Azure SQL Edge would be xxx.xxx.xxx.xxx,1600. 更新后的 Python 脚本为:The updated python script is:


import pyodbc
server = 'xxx.xxx.xxx.xxx,1600' # Replace this with the actual name of your SQL Edge Docker container
username = 'sa' # SQL Server username
password = 'MyStrongestP@ssword' # Replace this with the actual SA password from your deployment
database = 'MyEdgeDatabase' # Replace this with the actual database name from your deployment. If you do not have a database created, you can use Master database.
db_connection_string = "Driver={ODBC Driver 17 for SQL Server};Server=" + server + ";Database=" + database + ";UID=" + username + ";PWD=" + password + ";"
conn = pyodbc.connect(db_connection_string, autocommit=True)

若要使用在 Windows 计算机上运行的 SQL Server Management Studio 连接到 Azure SQL Edge 实例,请参阅 SQL Server Management StudioTo connect to an instance of Azure SQL Edge by using SQL Server Management Studio running on a Windows machine, see SQL Server Management Studio.

若要使用 Windows、Mac 或 Linux 计算机上的 Visual Studio Code 连接到 Azure SQL Edge 实例,请参阅 Visual Studio CodeTo connect to an instance of Azure SQL Edge by using Visual Studio Code on a Windows, Mac or Linux machine, see Visual Studio Code.

若要使用 Windows、Mac 或 Linux 计算机上的 Azure Data Studio 连接到 Azure SQL Edge 实例,请参阅 Azure Data StudioTo connect to an instance of Azure SQL Edge by using Azure Data Studio on a Windows, Mac or Linux machine, see Azure Data Studio.

后续步骤Next steps

连接和查询Connect and query

在 Linux 上安装 SQL Server 工具Install SQL Server tools on Linux