Save data to SQLServer

Set up a SQLServer database and set the user name and password to sa/mqtt_public. Take MacOS X as an example:

  1. docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=mqtt_public' -p 1433:1433 -d mcr.microsoft.com/mssql/server:2017-latest

Enter the SQLServer container and initialize the SQLServer table:

Setting up SQLServer sa password

  1. $ /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P mqtt_public -d master

Create the “mqtt” database:

  1. CREATE DATABASE mqtt;
  2. go;

Create the t_mqtt_msg table:

  1. USE mqtt;
  2. go;
  3. CREATE TABLE t_mqtt_msg (id int PRIMARY KEY IDENTITY(1000000001,1) NOT NULL,
  4. msgid VARCHAR(64) NULL,
  5. topic VARCHAR(100) NULL,
  6. qos tinyint NOT NULL DEFAULT 0,
  7. payload NVARCHAR(100) NULL,
  8. arrived DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP);
  9. go;

Configure odbc driver in Mac:

  1. $ brew install unixodbc freetds
  2. $ vim /usr/local/etc/odbcinst.ini
  3. [ms-sql]
  4. Description = ODBC for FreeTDS
  5. Driver = /usr/local/lib/libtdsodbc.so
  6. Setup = /usr/local/lib/libtdsodbc.so
  7. FileUsage = 1

Configure odbc driver in CentOS:

  1. $ yum install unixODBC unixODBC-devel freetds freetds-devel perl-DBD-ODBC perl-local-lib
  2. $ vim /etc/odbcinst.ini
  3. # add as below
  4. [ms-sql]
  5. Description = ODBC for FreeTDS
  6. Driver = /usr/lib64/libtdsodbc.so
  7. Setup = /usr/lib64/libtdsS.so.2
  8. Driver64 = /usr/lib64/libtdsodbc.so
  9. Setup64 = /usr/lib64/libtdsS.so.2
  10. FileUsage = 1

Configure odbc driver in Ubuntu (Take Ubuntu 20.04 as an example, for other versions please refer to the odbc official documentation):

  1. $ apt-get install unixodbc unixodbc-dev tdsodbc freetds-bin freetds-common freetds-dev libdbd-odbc-perl liblocal-lib-perl
  2. $ vim /etc/odbcinst.ini
  3. # add as below
  4. [ms-sql]
  5. Description = ODBC for FreeTDS
  6. Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
  7. Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
  8. FileUsage = 1

Create rules:

Open EMQX DashboardSave data to SQLServer - 图1 (opens new window) and select the “Rules” tab on the left.

Fill in the rule SQL:

  1. SELECT * FROM "t/#"

image

Related actions:

On the “Response Action” interface, select “Add”, and then select “Save Data to SQLServer” in the “Action” drop-down box.

image

Fill in the action parameters:

The “Save data to SQLServer” action requires two parameters: 1). SQL template. In this example, we insert a piece of data into SQLServer, and the SQL template is:

  1. insert into t_mqtt_msg(msgid, topic, qos, payload) values ('${id}', '${topic}', ${qos}, '${payload}')

image

2). The ID of the associated resource. Now the resource drop-down box is empty, and you can click “New Resource” in the upper right corner to create a SQLServer resource:

Fill in the resource configuration: Fill in “mqtt” for database name, “sa” for user name, and “mqtt_public” for password

image

Click the “Confirm” button.

Return to the response action interface and click “OK”.

image

Return to the rule creation interface and click “Create”.

image

The rule has been created. Now, send a piece of data:

  1. Topic: "t/a"
  2. QoS: 1
  3. Payload: "hello"

In the rule list, click the “View” button or the rule ID connection to preview the rule you just created:

Here we can see that metrics has been increased.

image

Then check the SQLServer table to see whether the new record is added successfully:

image