1. 程式人生 > 資料庫 >Azure IoT Edge入門(10)遠端在IoT Edge裝置上部署SQL資料庫-Deploy Azure SQL Edge to Azure IoT Edge

Azure IoT Edge入門(10)遠端在IoT Edge裝置上部署SQL資料庫-Deploy Azure SQL Edge to Azure IoT Edge

本文介紹:

遠端在IoT Edge裝置上部署 Azure SQL Edge的幾種方法;

連線到Azure SQL Edge的方法(Edge裝置物理機 / Edge裝置Module容器內);

Azure Data Studio連線到Azure SQL Edge;

 

視訊:

https://www.51azure.cloud/post/2020/11/15/azure-iot-edge-10-iot-edge-sql-deploy-azure-sql-edge-to-azure-iot-edge

 

重點圖文:

 

遠端在IoT Edge裝置上部署 Azure SQL Edge的幾種方法;

方法1.在Azure Portal上的IoT edge裝置上新增市場 模組:

進入IoT Hub 選中要部署SQL EDGE的iot edge裝置:

點選 模組設定:

 

點選新增 市場模組:

在市場中選擇SQL Server Module:

 

方法2.在Azure Portal上的Marketplace中選擇Azure SQL Edge,然後選擇待部署到的IoT Edge裝置,然後進行部署;

找到Azure SQL Edge,然後點選進入下一個頁面:

 

選擇某個訂閱下的某個hub的某個Edge裝置,然後按照提示進行安裝:

 

 

方法三.在VS Code或者visual studio中通過deployment.templete.json 部署檔案部署;

連線到Azure SQL Edge的方法(Edge裝置物理機 / Edge裝置Module容器內);

參考:

在edge 裝置物理機上或其他物理機上進行連線,使用edge裝置物理機IP地址和埠的方式:

如下圖中的1600表示埠,埠是可配置的選項。

如下圖Python語言:

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)

在 部署 sql edge時可指定對映到物理機的埠,如下圖,將容器的1433繫結到物理機的1600埠:

{
    "PortBindings": {
      "1433/tcp": [
        {
          "HostPort": "1600"
        }
      ]
    }
}

 

如下圖在管理工具中:

 

在Edge裝置上不同的Module中連線,即同一個裝置上的不同模組(容器)間連線,採用 SQL EDGE模組名的方式:

如下圖python語言:

import pyodbc
server = 'SQLServerModule' # 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)

 

如下圖C#語言中:

 var dbstring="Data Source=SQLServerModule;Initial Catalog=master;User Id=SA;Password=Strong!Passw0rd;TrustServerCertificate=False;Connection Timeout=30;";            
      
            try
            {
                 
                 var sensorData=new SensorData();
                 try
                    {
                     sensorData= JsonConvert.DeserializeObject<SensorData>(messageString);
                   
                    }
                    catch(Exception ex)
                    {
                        Console.WriteLine($"error{ex.Message}");
                    }
                
                using (SqlConnection con = new SqlConnection(dbstring))
                {
                    con.Open();
                    if (con.State ==   System.Data.ConnectionState.Open)
                    {
                    
                        string strCmd = $"insert into dbo.Telemetry(temperature,humidity,funcsavedt,deviceid) values ({sensorData.Temperature},{sensorData.Humidity},'{System.DateTime.Now}','{messageReceived.ConnectionDeviceId}' )";


                        SqlCommand sqlcmd = new SqlCommand(strCmd, con);
                        int   n = sqlcmd.ExecuteNonQuery();
                        if (n > 0)
                        {
                            logger.LogInformation("save to sql edge db successfully");
                        }
                        else
                        {
                            logger.LogError("save to sql edge db error");
                        }
                      
                }  
               con.Close();
               }
            }
            catch (Exception ex)
            {
               logger.LogInformation(ex.StackTrace);
            }

 其中的“SQLServerModule” 為配置的容器名稱:

也可以在deployment檔案中找到:

 

Azure Data Studio連線到Azure SQL Edge;

Azure Data Studio連線:

Azure Data Studio 是一種跨平臺的資料庫工具,適合在 Windows、macOS 和 Linux 上使用 Microsoft 系列的本地和雲資料平臺的資料專業人員。

在Edge物理機連線到Edge Module(容器)中的SQL EDGE資料庫時,可以使用IP和埠的方式進行連線:

如下圖在管理工具中:

如果是本機除錯,server ip 可以更換為localhost

 

 





宣告:

 

點選可查閱本站文章目錄

本站所有內容僅代表個人觀點,如與官文件衝突,請以官方文件為準。

可在本頁面下方留言或通過下方聯絡方式聯絡我:

微信:wxyusz;郵箱:

歡迎關注公眾號“雲端計算實戰”,接收最新文章推送。



知識共享許可協議

本作品由Sean Yu 採用進行許可。
歡迎轉載、使用、重新發布,但務必保留文章連結:,且不得用於商業目的。