SQL Client Server
SQL Client Server
Database connection
Siemens
SIMATIC IOT2020, SIMATIC IOT2040 Industry
Online
https://support.industry.siemens.com/cs/ww/en/view/Item ID Support
Frei verwendbar
Warranty and liability
Note The Application Examples are not binding and do not claim to be complete
regarding the circuits shown, equipping and any eventuality. The Application
Examples do not represent customer-specific solutions. They are only intended
to provide support for typical applications. You are responsible for ensuring that
the described products are used correctly. These Application Examples do not
relieve you of the responsibility to use safe practices in application, installation,
operation and maintenance. When using these Application Examples, you
recognize that we cannot be made liable for any damage/claims beyond the
liability clause described. We reserve the right to make changes to these
Application Examples at any time without prior notice.
If there are any deviations between the recommendations provided in these
Application Examples and other Siemens publications – e.g. Catalogs – the
contents of the other documents have priority.
We do not accept any liability for the information contained in this document.
Any claims against us – based on whatever legal reason – resulting from the use of
the examples, information, programs, engineering and performance data etc.,
described in this Application Example shall be excluded. Such an exclusion shall
not apply in the case of mandatory liability, e.g. under the German Product Liability
Act (“Produkthaftungsgesetz”), in case of intent, gross negligence, or injury of life,
body or health, guarantee for the quality of a product, fraudulent concealment of a
Siemens AG 2018 All rights reserved
Security Siemens provides products and solutions with industrial security functions that
informa- support the secure operation of plants, systems, machines and networks.
tion In order to protect plants, systems, machines and networks against cyber
threats, it is necessary to implement – and continuously maintain – a holistic,
state-of-the-art industrial security concept. Siemens’ products and solutions only
form one element of such a concept.
Customer is responsible to prevent unauthorized access to its plants, systems,
machines and networks. Systems, machines and components should only be
connected to the enterprise network or the internet if and to the extent necessary
and with appropriate security measures (e.g. use of firewalls and network
segmentation) in place.
Additionally, Siemens’ guidance on appropriate security measures should be
taken into account. For more information about industrial security, please visit
http://www.siemens.com/industrialsecurity.
Siemens’ products and solutions undergo continuous development to make them
more secure. Siemens strongly recommends to apply product updates as soon
as available and to always use the latest product versions. Use of product
versions that are no longer supported, and failure to apply latest updates may
increase customer’s exposure to cyber threats.
To stay informed about product updates, subscribe to the Siemens Industrial
Security RSS Feed under http://www.siemens.com/industrialsecurity.
Table of contents
Warranty and liability ................................................................................................... 2
1 Task ..................................................................................................................... 4
1.1 Overview............................................................................................... 4
2 Demo 1 – Install SQL server on IOT2000......................................................... 6
2.1 Preconditions for installing MariaDB .................................................... 6
2.2 Installing MariaDB ................................................................................ 7
2.2.1 Create a new user for secure installation ............................................. 7
2.2.2 Optional: mount USB-Drive .................................................................. 7
2.2.3 Install binaries ...................................................................................... 9
2.2.4 Testing installed MariaDB server ....................................................... 11
2.2.5 Securing MariaDB installation ............................................................ 11
2.3 Adding MariaDB to autostart .............................................................. 12
2.4 Creating an example database .......................................................... 12
3 Demo 2 – SQL client with Node-RED ............................................................. 14
3.1 Preconditions for an SQL client with Node-RED ................................ 14
3.2 Start Node-RED ................................................................................. 14
3.2.1 Auto start Node-RED on boot ............................................................ 14
3.2.2 Manual start Node-RED ..................................................................... 16
3.3 Install required Node-RED nodes ...................................................... 17
3.4 Configure Node-RED dashboard ....................................................... 18
Siemens AG 2018 All rights reserved
1 Task
1.1 Overview
Introduction
This application example is divided into three parts.
One example shows how to install an SQL server at the IOT200 device.
Two examples show how to connect the IOT2000 with an SQL server.
Every example was created with the IOT2000 example image V2.2 (see Related
links)
Demo 2
IOT2000 Server
SQL Database
Demo 3
IOT2000 Server
SQL Database
C++ SQL Client
Goals
After working through this document you will know how to
Install an SQL server on the IOT2000
Create an SQL client with Eclipse as C++ application
Create an SQL client with Node-RED
NOTICE Precondition!
To use this sample it is required that the initial operation of the documents
“Setting up the SIMATIC IOT2000” and “Getting Started” has been executed
once.
NOTICE Precondition!
Basic Linux knowledge is needed to understand the examples
Siemens AG 2018 All rights reserved
To start you have to download MariaDB 5.5 for a Linux x86-system because the
IOT2000 has a 32-bit processor.
2.
The version 5.5.59 was the current version at the time of creation of this
document.
NOTE
It may differ at the time you will download it.
Frei verwendbar
To create some secure environment for the MariaDB server it is required to create
a new user. That user will be used to run MariaDB. Because of the file system right
management MariaDB has only access to files that are owned by the installer user
(e.g. "mysql") or files that allow access for the user.
Table 2-2 Create new user
No. Action
1. Connect to the IOT2000 via Putty
2. Create new user group mysql
groupadd mysql
3. Add new user mysql to the previously created group mysql
useradd –g mysql mysql
Siemens AG 2018 All rights reserved
No. Action
Add the line like in the image below and replace /dev/sda1 and vfat with your
device id and type
Siemens AG 2018 All rights reserved
Press “Ctrl + X” to exit and answer the following question with Yes (y)
5. Mount USB device manually with this command:
mount -a
NOTE If you don’t want to use an USB-drive as storage you have to change all values
where /media/usb-drive is written with your own path in the following steps.
We are using the USB-drive as storage for MariaDB. If you want you can change it
to any directory. To do that you have to change the datadir parameter in the
following table to match your directory. Also make sure that the mysql user has
access to that folder.
To give the mysql user permission to a specific directory you can use the following
command by substituting DIR with your used directory:
This command has to be executed only if you are using a different directory than
the USB-device.
No. Action
[mysqld]
datadir=/media/usb-drive/sqldb
7. Switch to the mysql directory with
cd mysql
Change current user to the previously created mysql user
Siemens AG 2018 All rights reserved
8.
su mysql
NOTE You can set any path to datadir but you have to use the same in the my.cnf file
and when starting the installation script
Also note that the mysql user must have read and write permission to that folder
The following command can be used to change the owner of a directory
chown -R mysql DIR (You have to change DIR to your own directory)
To secure our installation we can run a script that helps removing some tables that
are not needed and we are forced to set a password for the root user of MariaDB.
We also prevent external access as root user to increase security.
Table 2-6 Secure installed MariaDB server
No. Action
1. Logoff from mysql user with the command
exit
No. Action
6. To receive the value you can use select to retrieve rows from the database
SELECT * FROM iot2000Table;
7. Create a user that can access the server from outside localhost
You may replace password with your own one
CREATE USER 'iot2000user' IDENTIFIED BY 'password';
NOTE Make sure that you end each command with an “;”
IOT2000 Server
SQL Database
2. Select Software
Frei verwendbar
No. Action
No. Action
NOTE It may take up to 5 minutes until complete Node-RED startup after IOT2000
power-on.
NOTE For the next steps an online connection to the internet is needed for the
IOT2000. The needed nodes are directly downloaded from the web.
2. Open “Manage palette” which is located on the top right of the page
Siemens AG 2018 All rights reserved
2. Click on edit to rename Tab 1 to a more precise name and click on Update
Siemens AG 2018 All rights reserved
3. Add two groups that are used for writing and reading values from and to the SQL
database
4. Rename one group to SQL Input with the default width parameter and the other
group to SQL Output with width set to 10
1x slider
1x button
3x function
2x mysql
1x text
Siemens AG 2018 All rights reserved
2. Double click on Flow 1 to edit the name of the flow to structure it if you have multiple
flows. Save changes with a click on Done
3. Double click each of the inserted nodes to open the edit page. The values you have
to enter are shown in the following steps. You save your changes by pressing Done
4. Slider configuration
5. Button configuration
No. Action
6. Function 1 (INSERT):
msg.topic = "INSERT INTO iot2000Table (value) VALUES (" + msg.payload + ");";
return msg;
Siemens AG 2018 All rights reserved
7. Function 2 (SELECT):
msg.topic = "SELECT * FROM iot2000Table ORDER BY insert_time DESC LIMIT 25;";
return msg;
8. Function 3 (Format)
var output = "";for (var i = 0; i < msg.payload.length; i++) {
var item = msg.payload[i];
output += `${i+1} - ${item.insert_time} --> [${item.value}]<br>`;
}
No. Action
msg.payload = output;
return msg;
No. Action
No. Action
15. After pressing Read values the screen may look similar to the screen below if have
moved the slide some times.
Siemens AG 2018 All rights reserved
IOT2000 Server
SQL Database
C++ SQL Client
2. Extract the downloaded MariaDB folder with 7zip into the newly created Eclipse
project. You can ignore any errors that occur during the unzip process. Only
include and lib folder are required.
Frei verwendbar
No. Action
NOTE If you modify the files outside of Eclipse it may be possible to refresh you project
by pressing F5
No. Action
1. Copy all libmysqlclient files located at the lib folder from the downloaded
MariaDB archive to /usr/lib on the IOT2000
Table 4-4 Install library from a previously installed MySQL server installation
No. Action
1. Open library configuration file by typing
nano /etc/ld.so.conf
2. Add the following line to include the lib folder of the server installation. Here we
use the path that we used in Demo 1 – Install SQL server
int main() {
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
/* Database settings */
/* replace IP address with the IP of your IOT */
conn = mysql_init(NULL);
Siemens AG 2018 All rights reserved
/* Connect to database */
if (!mysql_real_connect(conn, server, user,
password, database, 0, NULL,
0)) {
fprintf(stderr, "%s\n", mysql_error(conn));
return 1;
}
/* SQL query */
if (mysql_query(conn,
"SELECT * FROM iot2000Table ORDER BY
insert_time DESC LIMIT 25")) {
fprintf(stderr, "%s\n", mysql_error(conn));
return 1;
}
res = mysql_use_result(conn);
/* Output values */
printf("Values in database:\n");
while ((row = mysql_fetch_row(res)) != NULL)
printf("%s --> [%3s]\n", row[0], row[1]);
/* Close connection */
mysql_free_result(res);
mysql_close(conn);
return 0;
}
2. The program can now be started and will use the previously installed
“libmysqlclient.so”
No. Action
3. The output of the program may look like the image below
Siemens AG 2018 All rights reserved
5 Checklist
This chapter contains a Checklist which summarizes all important steps in this
application example.
6 Related links
Table 4-1 Related links
Topic
\1\ SIMATIC IOT2000 forum
www.siemens.com/iot2000-forum
\2\ SIMATIC IOT2000 Setting Up
https://support.industry.siemens.com/tf/ww/en/posts/155642/
\3\ SIMATIC IOT2000 Getting Started
https://support.industry.siemens.com/tf/ww/en/posts/155643/
7 History
Table 5-1 History
Version Date Modifications
V1.0 05/2018 First version
Siemens AG 2018 All rights reserved