zoukankan      html  css  js  c++  java
  • 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容器内);

    参考: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

     





    声明:

    点击可查阅本站文章目录 《文章分类目录》

    本站所有内容仅代表个人观点,如与官文档冲突,请以官方文档为准。

    可在本页面下方留言或通过下方联系方式联系我:

    微信:wxyusz;邮箱:shuzhen.yu@foxmail.com

    欢迎关注公众号“云计算实战”,接收最新文章推送。



    知识共享许可协议

    本作品由Sean Yu 采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。
    欢迎转载、使用、重新发布,但务必保留文章链接:https://www.51azure.cloud,且不得用于商业目的。

  • 相关阅读:
    CSS相关(2)
    CSS相关(1)
    Git(待补充)
    Jenkins学习(1)-什么是Jenkins
    Yaml(待补充)
    Tomcat(待补充)
    Json(待补充)
    Java语言学习(8)-Java中的异常处理
    Java语言学习(7)-Java中IO
    Java语言学习(6)-Java中封装类(正则表达式操作类)
  • 原文地址:https://www.cnblogs.com/shuzhenyu/p/13994456.html
Copyright © 2011-2022 走看看