Deploy and Use MySQL
You can make the most of the massive amount of data collected by Litmus Edge by storing the data in a database for further analysis. The Litmus Edge default Marketplace includes a MySQL application.
Litmus Edge provides a variety of applications in its default marketplace catalog. In the case of MySQL, a container-based MySQL database is provided, which is local to the Litmus Edge device. Data collected at the edge can be stored in this local database. In addition, you can extract data from the local database for consumption by other databases in your enterprise.
Caution:
- Caution should be used when using this local database. If the size exceeds the hardware memory size, it crashes the Litmus Edge system, causing data to be lost and requiring a fresh installation.
- To manage the size of the local database, we recommend weekly backups and management of local data by using scripts.
- As an alternative to the local database, collect data and send it to an external database that is on the same network as the Litmus Edge device.
Internet connectivity is required to deploy the Default Public Marketplace and to deploy applications. Once you create an application instance, you no longer need internet access to create additional instances of the same application.
To deploy the MySQL application:
- In Litmus Edge, navigate to Applications > Marketplace.
- Click the MySQL tile.
- The MySQL Launch dialog box appears.
- From the Installation script version drop-down list, select latest. The MySQL form appears.
- Configure the following parameters.
- Name
- Description (Optional)
- Port: Enter a port. The default value is 3306.
- Database: Enter a database name.
- User: Enter a user name.
- Password: Enter a MySQL password.
- MySQL password: Enter a MYSQL root password.
- Restart: Configure the restart setting. Enter no, always, or on-failure.
- Click Launch. The MySQL application appears as a tile in the Catalog Apps pane.
You will now need to define a database table for the data you're collecting.
You can use a tool, such as MySQL Workbench, to connect to the Litmus Edge local database to create a table.
The columns and data types required for a database table depend on a device tag's configuration. For this exercise, a basic database table illustrates data that can be saved to the database. Using the Flows Manager, you can collect and parse the message payload to extract the device ID, device tag, and the register's value.
- You will need to configure the DataHub Subscribe node with a topic for a connected device. Navigate to DeviceHub > Tags, select a device, and copy the RAW Topic for the tag you want to use.
- You need to know the message format. In the following example, you can see the field names and values that you may want to save in a database.
To identify database table and column requirements:
- In Litmus Edge, navigate to Flows Manager.
Click the Go To Flow Definition icon for a selected Flows Manager. The Flow canvas opens in a new browser tab.
From the node palette, drag the DataHub Subscribe node (DataConnector section) to the canvas.
- Drag the Debug node to the canvas and connect the two nodes.
- Double click the DataHub Subscribe node. The Edit DataHub Subscribe Node dialog box appears.
- In the Topic field, paste the topic name you previously copied and click Done.
- If needed, configure the Datahub Subscribe connection. See the "Step 3: Configure Connector Nodes" section in Create a Flow to learn more.
- Click Deploy.
Expand the message window beneath the flow and click the Debug icon. Verify that messages are displaying.
While table creation can be accomplished by writing SQL statements in a flow, the preferred method uses a database tool, such as MySQL Workbench, to create the table and columns in the Litmus Edge local database.
For the purpose of this exercise, connect to the Litmus Edge database and create a database table with the following columns: deviceID, tagName, and tagValue.
You can create another flow to collect data and store it in a MySQL database.
Note: You can access the MySQL database located on the Litmus Edge device or you can use an external database on the same network as the Litmus Edge device.
Refer to the flow below that you will create.
- You will need to configure the DataHub Subscribe node with a topic for a connected device. Navigate to DeviceHub > Tags, select a device, and copy the RAW Topic for the tag you want to use.
- You must have experience working with flows and SQL queries.
To create a flow to populate a MySQL database:
- In Litmus Edge, navigate to Flows Manager.
Click the Go To Flow Definition icon for a selected Flows Manager. The Flow canvas opens in a new browser tab.
- Refer to the following tasks for nodes.
After configuring the DataHub Subscribe node with a topic from a connected device, it collects the message. The JSON node processes the message to ensure that it is in the proper JSON format required for further processing.
- Drag the DataHub Subscribe and JSON nodes onto the canvas and connect both nodes.
- Double-click the DataHub Subscribe node. The Edit DataHub Subscribe Node dialog box appears.
- In the Topic field, paste the topic name you previously copied and click Done.
- Click Deploy.
The Function node parses the incoming message.
- Drag the Function node onto the canvas and connect it to the JSON node.
- Double-click the Function node and enter the JavaScript code below in the On Message tab. This parses the message payload.
- Click Deploy.
Use the Template node to write SQL statements to insert data into the database columns.
- Drag the Template node onto the canvas and connect it to the Function node.
- Double-click the Template node.
- In the Property field, select msg. and enter topic.
- Enter the SQL statement below in the Template window.
- Click Deploy.
Note: In the example statement below, test represents the name of the database table, which you need to create. The table doesn't exist by default.
Configure this node with a port and credentials to connect to the local Litmus Edge MySQL database when using the Marketplace application. When using an external MySQL database on the same network as the Litmus Edge device, configure the MySQL node with the IP address, port, and credentials for that database server.
- Drag the MySQL node onto the canvas and connect it to the Template node.
- Double-click the MySQL node.
- In the Database field, click the Edit icon.
- Configure the following parameters.
- Host: 127.0.0.1
- Port: 3306
- User: root
- Password: Enter the MySQL database password
- Database: Enter the name of the database
Verify your flow is working as designed.
- Drag the Debug node onto the canvas and connect it to the MySQL node.
Expand the message window beneath the flow and click the Debug icon. Verify that messages are displaying.
To check that collected values are being inserted into the MySQL database, create a basic flow.
Refer to the updates to the flow created in the previous steps.
To validate database updates:
- From the flow canvas you used to create the previous flow, copy and paste the Template node previously created and connect it to the Function node.
- Double-click the Template node.
- Enter the following SQL statement in the Template window and click Done. select * from test;
- Click Deploy.
- Copy and paste the MySQL node previously created and connect it to the newer Template node.
- Click Deploy.
- Drag a new Debug node onto the canvas and connect it to the newer MySQL node.
Expand the message window beneath the flow and click the Debug icon. Verify that messages are displaying.
You will now update the original template node on the flow canvas to do the following:
- Collect raw data from various PLC registers.
- Aggregate the data at regular intervals.
- Save the aggregated data for use by other applications.
Use the Template node to format the SQL statement to update the database with values that have been collected from multiple registers.
To save multiple registers to MySQL:
- From the same flow canvas you used to create and update flows, double-click the original Template node.
- Enter the following SQL statement in the Template window and click Done.
Note: Depending on the MYSQL column types, certain formatting may be required. For example, if a column is VARCHAR, the following notation must be used: "{{}}".