All Projects → curzon01 → mqtt2sql

curzon01 / mqtt2sql

Licence: GPL-3.0 license
Copy MQTT topic payloads to MySQL/SQLite database

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to mqtt2sql

antares
A modern, fast and productivity driven SQL client with a focus in UX.
Stars: ✭ 836 (+1448.15%)
Mutual labels:  mariadb, sqlite3
json-sql-builder2
Level Up Your SQL-Queries
Stars: ✭ 59 (+9.26%)
Mutual labels:  mariadb, sqlite3
datastation
App to easily query, script, and visualize data from every database, file, and API.
Stars: ✭ 2,519 (+4564.81%)
Mutual labels:  mariadb, sqlite3
Denodb
MySQL, SQLite, MariaDB, PostgreSQL and MongoDB ORM for Deno
Stars: ✭ 498 (+822.22%)
Mutual labels:  mariadb, sqlite3
Python-Course
🐍 This is the most complete course in Python, completely practical and all the lessons are explained with examples, so that they can be easily understood. 🍫
Stars: ✭ 18 (-66.67%)
Mutual labels:  mariadb, sqlite3
Rom Sql
SQL support for rom-rb
Stars: ✭ 169 (+212.96%)
Mutual labels:  mariadb, sqlite3
newsql nosql library
整理12种数据库相关资料,mysql,mariaDB,Percona Server,MongoDB,Redis,RocksDB,TiDB,CouchDB,Cassandra,TokuDB,MemDB,Oceanbase
Stars: ✭ 270 (+400%)
Mutual labels:  mariadb
Athena
Test your Security Skills, and Clean Code Development as a Pythonist, Hacker & Warrior 🥷🏻
Stars: ✭ 43 (-20.37%)
Mutual labels:  sqlite3
Docker-Stack
This repo contains a simple Docker setup with minimal configuration and only few files you can drop into many PHP-based projects.
Stars: ✭ 31 (-42.59%)
Mutual labels:  mariadb
NSP
A Social Network that brings engineer's from all fields together to collaborate 🌐
Stars: ✭ 28 (-48.15%)
Mutual labels:  sqlite3
web trader
📊 Python Flask game that consolidates data from Nasdaq, allowing the user to practice buying and selling stocks.
Stars: ✭ 21 (-61.11%)
Mutual labels:  sqlite3
bun
SQL-first Golang ORM
Stars: ✭ 1,570 (+2807.41%)
Mutual labels:  sqlite3
sea-query
🔱 A dynamic SQL query builder for MySQL, Postgres and SQLite
Stars: ✭ 595 (+1001.85%)
Mutual labels:  mariadb
docker-mariadb
A docker image to run MariaDB with XtraBackup 🐳
Stars: ✭ 12 (-77.78%)
Mutual labels:  mariadb
convert-db-to-csv
convert-db-to-csv.sh is a shell script that uses SQLite3 to convert a .db file into .csv files. It converts each of the tables in the database into csv files.
Stars: ✭ 58 (+7.41%)
Mutual labels:  sqlite3
Queries
SQLite queries
Stars: ✭ 57 (+5.56%)
Mutual labels:  sqlite3
squirrel
Like curl, or wget, but downloads directly go to a SQLite databse
Stars: ✭ 24 (-55.56%)
Mutual labels:  sqlite3
masking
Command line tool for generating anonymizing database from existed database
Stars: ✭ 67 (+24.07%)
Mutual labels:  mariadb
PT-Tracking
Aplicação para registo e acompanhamento de encomendas da CTT Expresso, automatiza a consulta online do estado de tracking para várias remessas e mantém um registo dos pagamentos referentes aos envios à cobrança. As remessas que requerem atenção, devido a atrasos na entrega ou na receção do pagamento correspondente, bem como os cheques cuja data …
Stars: ✭ 18 (-66.67%)
Mutual labels:  sqlite3
libpassqlite
libPasSQLite is delphi and object pascal bindings and wrapper around SQLite library. SQLite is library that implements a small, fast, self-contained, high-reliability, full-featured, SQL database engine.
Stars: ✭ 18 (-66.67%)
Mutual labels:  sqlite3

mqtt2sql

This python 3 program creates copies of MQTT broker data into a SQL database (currently supports MySQL5.x/MariaDB 10.x and SQLite 3).

master License

If you like mqtt2sql give it a star or fork it:

GitHub stars GitHub forks

The MQTT data are provided in the following tables/view:

  • Table mqtt
    contains the last MQTT copied payload for the subcribed topic
  • Table mqtt_history
    contains the payloads history from mqtt. History data can be disabled by topic or in general (see History control).
  • View mqtt_history_view
    contains data from mqtt_history with readable topics and timestamps (see History view)

Content

Installation

During the installation we

  • create a usable Python 3.x environment
  • create the necessary databases and objects
  • test the program
  • and if desired, create a system daemon

Python prerequisites

If not already done, install a working Python 3.x environment described there.

Note: Due to the Python 2.7 EOL in Jan 2020 Python 2.x is no longer supported.

Install Pip, Paho MQTT and MySQLdb lib to your python environment use

sudo apt-get install python3-pip python3-mysqldb python3-configargparse python3-paho-mqtt

Check that Python 3.x is installed e.g.

$ python3 --version
Python 3.8.0

Check that pip installed pao-mqtt greater or equal version 1.2.3, e.g.

$ pip3 show paho-mqtt
...
Name: paho-mqtt
Version: 1.5.0
...

Copy the program

Copy repository using git and make the program executable:

git clone https://github.com/curzon01/mqtt2sql
cd mqtt2sql
chmod +x mqtt2sql.py

Create database objects

the sql scripts we use here for MySQL and SQLite are creating all neccessary databases and objects. The default database is mqtt and the tables are mqtt_history with history data enabled. If you want to use different namings or existing databases, edit the top of the related *sql script before using it.

Using MySQL

mysql --host localhost < mysql.sql

If a username and password is set on your server, use

mysql --host localhost -u <username> -p < mysql.sql

Using SQLite3

sqlite3 mqtt.db <sqlite.sql

Usage

Start from command line

For first help, start the script with parameter -h

./mqtt2sql.py -h

If you got a help page, you can start try to run it using one of the existing database objects above

Run program using MySQL

Change parameter (e.g. mqtthost, sqlhost) to your needs

./mqtt2sql.py --mqtt-host localhost --mqtt-username mqttuser --mqtt-password 'mqttpasswd' \
--mqtt-topic 'mytopic/#' \
--sql-type mysql --sql-host localhost --sql-username sqluser --sql-password 'sqlpasswd' --sql-db mqtt -v

Run program using SQLite3

Change parameter (e.g. mqtthost) to your needs

./mqtt2sql.py --mqtt-host localhost --mqtt-username mqttuser --mqtt-password 'mqttpasswd' \
--mqtt-topic 'mytopic/#' \
--sql-type sqlite --sql-db mqtt.db -v

Start as systemd manager daemon

The program allows the entire program parameters to be transferred in a configuration file instead of as individual program parameters.
For the following service file we use a copy of the configuration file mqtt2sql.conf for parameterization and chnage it to our needs. This means that we do not have to edit the service file in the case of changes.

Make a copy of the program and configuration file and edit the parameter

sudo mkdir -p /usr/local/bin/
sudo cp mqtt2sql.py /usr/local/bin/
sudo mkdir -p /usr/local/etc/
sudo cp mqtt2sql.conf /usr/local/etc/
sudo nano /usr/local/etc/mqtt2sql.conf

edit the configuration parameter for your needs and save it with Ctrl+o Ctrl+x.

Create mqtt2sql.service

sudo nano /etc/systemd/system/mqtt2sql.service

Insert the following lines

Description=MQTT2SQL
After=local-fs.target network.target mysql.service

[Service]
Type=simple
Restart=always
RestartSec=10
ExecStart=/usr/local/bin/mqtt2sql.py --configfile /usr/local/etc/mqtt2sql.conf

[Install]
WantedBy=multi-user.target

Reload systemd manager, restart daemon and check success

sudo systemctl daemon-reload
sudo systemctl restart mqtt2sql
sudo systemctl status mqtt2sql

Finally enable the service

sudo systemctl enable mqtt2sql

History data

Table mqtt_history contains data history from table mqtt changes received by the MQTT subscription. The default setup is storing only changed values within mqtt_history.

Database objects created by this scripts enables history data as default.

History control

History data creation depends on two columns in mqtt table:

  • column history_enable actuate whether topic payload is saved in history (1) or not (0).
  • column history_diffonly actuate whether topic payload is saved in history if it is different to previously (1) or always (0). Note: this column setting neglected if history_enable is 0.

Change history control for exiting records

For existing mqtt table records use the UPDATE command, e.g. UPDATE mqtt SET history_enable=0 to disable history saving for all existing topic records (accordingly same sing column history_diffonly)

Change history control for newly created records

For newly created mqtt table records change the default of the related column using the ALTER command, e.g.

ALTER TABLE `mqtt`
    CHANGE COLUMN `history_enable`
    `history_enable` TINYINT(4) NOT NULL DEFAULT 0;`

set the same as above (disable history saving for topic records) for newly created topics.

History view

The view mqtt_history_view can be used to get the history data with human readable topics instead of foreign keys from original table mqtt_history. The view has also two timestamp columns:

  • ts is the timestamp from lastest insert into the mqtt_history table
  • ts_last is the timestamp from lastest change

If history_diffonly is enabled (1), ts shows the timestamp of the last payload change where the ts_last shows the latest recevied timestamp (independent if the last recevied payload has change or not).

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].