MQTTのMessageを残らずMySQL(MariaDB)に保存するコマンド

#個人的なメモ

db構造

  
sudo mysql -e "SHOW FULL COLUMNS FROM mqtt.msg"  
+-------+---------------+-------------------+------+-----+---------+-------+---------------------------------+---------+  
| Field | Type          | Collation         | Null | Key | Default | Extra | Privileges                      | Comment |  
+-------+---------------+-------------------+------+-----+---------+-------+---------------------------------+---------+  
| m     | varchar(1124) | latin1_swedish_ci | YES  |     | NULL    |       | select,insert,update,references |         |  
| tm    | int(11)       | NULL              | YES  |     | NULL    |       | select,insert,update,references |         |  
+-------+---------------+-------------------+------+-----+---------+-------+---------------------------------+---------+  

コマンド

mosquitto_sub -t "#" -v | xargs -I@ sudo mysql -e "insert into mqtt.msg (m,tm)values('@',$(date +%s))"  

上記だと、時間が固定になってしまいます。
SQLの方で持ってくるように変更

mosquitto_sub -t "#" -v | xargs -I@ sudo mysql -e "insert into mqtt.msg (m,tm)values('@',UNIX_TIMESTAMP(NOW()))"  

課題

・セキュリティ的に問題有り
・トピックとMessageが分割できていない

commentいただけると嬉しいです!


MQTTのMessageを残らずMySQL(MariaDB)に保存するコマンド
https://blog.hashito.biz/2021/12/01/fb0e358a-6036-407b-82d0-3e03d6945843/
著者
hashito
作成日
2021年12月1日
著作権