[fly-iot Feifan IoT] (16): The open source IOT Internet of Things project has connected to Emqx’s MQTT service and configured MySQL database authentication. It is still very convenient. You can use websocker to connect and test

Preface


fly-iot Feifan IoT Column:
https://blog.csdn.net/freewebsys/category_12219758.html

1. Video demonstration address


https://www.bilibili.com/video/BV1JG411B7ty/

[fly-iot] (5): The open source IOT Internet of Things project is very convenient to connect to Emqx’s MQTT service and configure MySQL database authentication. You can use websocker to connect and test

Insert image description here

2. First configure the database connection authentication method


Connected, which means the database passwords are configured correctly.
Insert image description here
Insert image description here

The most important thing is this authentication query SQL: The query for MySQL is as follows:

SELECT token as password_hash FROM devices where 
 `deviceUsername` = ${username} AND  blocked = 0 LIMIT 1

3. Create devices on the IOT platform

Insert image description here

Then you can test it in the tool:

Insert image description here

Can be connected successfully. Then the subscription was tested and the message was sent.

4. Configuration in docker-compose

The entire docker-compose code:
https://gitee.com/fly-iot/docker-compose

...
################## emqx latest 版本 ##################
  emqx:
    image: emqx/emqx:latest
    container_name: emqx
    environment:
    - "[email protected]"
    - "EMQX_CLUSTER__DISCOVERY_STRATEGY=static"
    - "EMQX_CLUSTER__STATIC__SEEDS=[[email protected]]"
    healthcheck:
      test: ["CMD", "/opt/emqx/bin/emqx ctl", "status"]
      interval: 5s
      timeout: 25s
      retries: 5
    ports:
      - 1883:1883
      - 8083:8083
      - 8084:8084
      - 8883:8883
      - 18083:18083
    volumes:
       - ./emqx/cluster.hocon:/opt/emqx/data/configs/cluster.hocon
    depends_on:
      - mysql-iot

You can use the configuration method, so you don't have to log in to the interface to configure.
You can close the management background later. It is safer to use the configuration file directly.

Configuration file for cluster.hocon

authentication = [
  {
    
    
    backend = mysql
    database = fly_iot
    enable = true
    mechanism = password_based
    password = mysqliot
    password_hash_algorithm {
    
    name = plain, salt_position = disable}
    pool_size = 8
    query = "SELECT token as password_hash FROM devices where \n `deviceUsername` = ${username} AND  blocked = 0 LIMIT 1"
    query_timeout = 5s
    server = "mysql-iot:3306"
    ssl {
    
    
      ciphers = []
      depth = 10
      enable = false
      hibernate_after = 5s
      log_level = notice
      reuse_sessions = true
      secure_renegotiate = true
      verify = verify_peer
      versions = [tlsv1.3, tlsv1.2]
    }
    username = root
  }
]

Just have a configuration file.

5. Summary

It is very convenient to use emqx to provide mqtt service.
Authentication is to query the database directly. So it doesn't matter whether the background system is python or golang.
The pressure is on the emqx server and mysql database.

Guess you like

Origin blog.csdn.net/freewebsys/article/details/135433773