本文介绍:
远程在IoT Edge设备上部署 Azure SQL Edge的几种方法;
连接到Azure SQL Edge的方法(Edge设备物理机 / Edge设备Module容器内);
Azure Data Studio连接到Azure SQL 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容器内);
参考:https://docs.microsoft.com/zh-cn/azure/azure-sql-edge/connect
在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连接:https://docs.microsoft.com/zh-cn/sql/azure-data-studio/what-is-azure-data-studio?view=sql-server-2017
Azure Data Studio 是一种跨平台的数据库工具,适合在 Windows、macOS 和 Linux 上使用 Microsoft 系列的本地和云数据平台的数据专业人员。
在Edge物理机连接到Edge Module(容器)中的SQL EDGE数据库时,可以使用IP和端口的方式进行连接:
如下图在管理工具中:
如果是本机调试,server ip 可以更换为localhost