MongoDB

    Plugin:

    TIP

    The emqx_auth_mongo plugin also includes ACL feature, which can be disabled via comments

    To enable MongoDB authentication, you need to configure the following in :

    1. # etc/plugins/emqx_auth_mongo.conf
    2. ## MongoDB Architecture type
    3. ##
    4. ## Value: single | unknown | sharded | rs
    5. auth.mongo.type = single
    6. ##rs mode needs to set rs name
    7. ## auth.mongo.rs_set_name =
    8. ## Server list, which is separated by comma in cluster mode
    9. ## Examples: 127.0.0.1:27017,127.0.0.2:27017...
    10. auth.mongo.server = 127.0.0.1:27017
    11. auth.mongo.pool = 8
    12. auth.mongo.login =
    13. auth.mongo.password =
    14. ## auth.mongo.auth_source = admin
    15. auth.mongo.database = mqtt
    16. ## SSL option
    17. # auth.mongo.ssl = false
    18. ## auth.mongo.ssl_opts.keyfile =
    19. ## auth.mongo.ssl_opts.certfile =
    20. ## auth.mongo.ssl_opts.cacertfile =
    21. ## MongoDB write mode.
    22. ##
    23. ## Value: unsafe | safe
    24. ## auth.mongo.w_mode =
    25. ## Mongo read mode.
    26. ##
    27. ## Value: master | slave_ok
    28. ## auth.mongo.r_mode =
    29. ## MongoDB topology configuration, which is not used generally. See MongoDB official ##website documentation
    30. auth.mongo.topology.pool_size = 1
    31. auth.mongo.topology.max_overflow = 0
    32. ## auth.mongo.topology.overflow_ttl = 1000
    33. ## auth.mongo.topology.local_threshold_ms = 1000
    34. ## auth.mongo.topology.socket_timeout_ms = 100
    35. ## auth.mongo.topology.server_selection_timeout_ms = 30000
    36. ## auth.mongo.topology.wait_queue_timeout_ms = 1000
    37. ## auth.mongo.topology.heartbeat_frequency_ms = 10000
    38. ## auth.mongo.topology.min_heartbeat_frequency_ms = 1000

    In the default configuration of MongoDB authentication, you need to ensure that the database has the following collections:

    The sample data in the default configuration is as follows:

    1. use mqtt
    2. db.mqtt_user.insert({
    3. "username": "emqx",
    4. "password": "efa1f375d76194fa51a3556a97e641e61685f914d446979da50a551a4333ffd7",
    5. "is_superuser": false,
    6. "salt": ""
    7. })

    After MongoDB authentication is enabled, you can connect with username: emqx, password: public.

    TIP

    MongoDB authentication support to configure :

    During authentication, EMQX Broker will use the current client information to populate and execute the user-configured authentication SQL to query the client’s authentication data in the database.

    MongoDB supported configuration collection name, password field, and selector command

    1. # etc/plugins/emqx_auth_mongo.conf
    2. auth.mongo.auth_query.collection = mqtt_user
    3. ## If salting is enabled, it needs to be configured as password,salt
    4. ## Value: password | password,salt
    5. auth.mongo.auth_query.password_field = password
    6. auth.mongo.auth_query.selector = username=%u

    You can use the following placeholders in the selector, and EMQX Broker will be automatically populated with client information when executed:

    • %u:Username
    • %c:Client ID
    • %C:TLS certificate common name (the domain name or subdomain name of the certificate), valid only for TLS connections
    • %d:TLS certificate subject, valid only for TLS connections
    1. The query result must include the password field, which is used by EMQX Broker to compare with the client password
    2. MongoDB uses the findOne query command to ensure that the query results you expect are shown in the first data