連線並查詢 Azure SQL Edge

重要

Azure SQL Edge 不再支援 ARM64 平台。

在 Azure SQL Edge 中,於部署容器之後,您可以從下列任何位置連線到資料庫引擎:

  • 容器內
  • 從相同主機上執行的另一個 Docker 容器
  • 從主機電腦
  • 從網路上任何其他用戶端電腦

用來連線到 Azure SQL Edge 的工具

您可以從下列任何一個常用工具連線到 Azure SQL Edge 的執行個體:

若要從網路電腦連線到 Azure SQL Edge 資料庫引擎,您需要下列項目:

  • 主機電腦的 IP 位址或網路名稱:這是 Azure SQL Edge 容器執行所在的主機電腦。

  • Azure SQL Edge 容器的主機連接埠對應 - 這是 Docker 容器連接埠與主機上連接埠的對應。 在容器內,Azure SQL Edge 一律會對應到連接埠 1433。 如有需要,您可以變更此連接埠。 若要變更連接埠號碼,請在 Azure IoT Edge 中更新 Azure SQL Edge 模組的 [容器建立選項]。 在下列範例中,容器上的連接埠 1433 會對應到主機上的連接埠 1600。

    {
        "PortBindings": {
          "1433/tcp": [
            {
              "HostPort": "1600"
            }
          ]
        }
    }
    
  • Azure SQL Edge 執行個體的 SA 密碼:這是在 Azure SQL Edge 部署期間針對 SA_PASSWORD 環境變數所指定的值。

從容器內連線到資料庫引擎

SQL Server 命令列工具會包含在 Azure SQL Edge 的容器映像中。 如果您使用互動式命令提示字元來附加至容器,則可在本機執行這些工具。 ARM64 平台上沒有 SQL 用戶端工具。

  1. 使用 docker exec -it 命令在您執行的容器中啟動互動式 Bash 殼層。 在下列範例中,e69e056c702d 是容器識別碼。

    docker exec -it e69e056c702d /bin/bash
    

    提示

    您不一定要指定整個容器識別碼。 您只需指定足夠的字元來唯一識別它。 因此,在此範例中,使用 e6e69 可能就已足夠,而不需使用完整識別碼。

  2. 當您在容器內時,請使用 sqlcmd 進行本機連線。 預設路徑並不包含 sqlcmd,因此您必須指定完整路徑。

    /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P '<YourPassword>'
    
  3. 使用 sqlcmd 完成時,請輸入 exit

  4. 使用互動式命令提示字元完成時,輸入 exit。 結束互動式 Bash 殼層後,容器會繼續執行。

從相同主機上的另一個容器連線到 Azure SQL Edge

因為在相同主機上執行的兩個容器都位於相同的 Docker 網路上,所以您可以使用服務的容器名稱和連接埠位址,輕鬆地存取這兩個容器。 例如,如果您要從相同主機上的另一個 python 模組 (容器) 連線到 Azure SQL Edge 的執行個體,您可以使用類似下列的連接字串。 (此範例假設 Azure SQL Edge 已設定為接聽預設的連接埠。)

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 Edge

您可能想要從網路上的另一部電腦連線到 Azure SQL Edge 的執行個體。 若要這樣做,請使用 Docker 主機的 IP 位址,以及 Azure SQL Edge 容器所對應的主機連接埠。 例如,如果 Docker 主機的 IP 位址是 192.168.2.121,而且 Azure SQL Edge 容器對應到主機連接埠 1600,則 Azure SQL Edge 執行個體的伺服器位址將是 192.168.2.121,1600。 更新的 python 指令碼如下:

import pyodbc
server = '192.168.2.121,1600' # Replace this with the actual name or IP address 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 Studio

若要使用 Windows、macOS 或 Linux 電腦上的 Visual Studio Code,連線到 Azure SQL Edge 的執行個體,請參閱 Visual Studio Code

若要使用 Windows、macOS 或 Linux 電腦上的 Azure Data Studio,連線到 Azure SQL Edge 的執行個體,請參閱 Azure Data Studio

下一步