emqx服务器如何配置要求的用户名
时间: 2023-08-04 16:03:33 浏览: 48
要在EMQ X服务器上配置用户名,可以按照以下步骤进行操作:
1. 打开EMQ X的配置文件emqx.conf,找到以下配置项:
```
## Authentication and ACL
## Authentication and Access Control
##
## Docs: https://docs.emqx.io/broker/latest/en/security/authentication.html
##
## auth.anonymous: if allow anonymous user to access
## auth.plugindir: plugin directory for authentication/authorization plugin
## auth.password_hash: password hash algorithm, md5, sha, bcrypt, pbkdf2_sha256, argon2
##
auth.async_threads = 8
auth.anonymous = on
auth.plugindir = data/loaded_plugins
auth.password_hash = bcrypt
```
2. 将auth.anonymous配置项设置为off,禁止匿名访问:
```
auth.anonymous = off
```
3. 根据需要,在auth.plugindir配置项中添加相应的认证插件。EMQ X支持多种认证插件,包括基于数据库、LDAP、OAuth2.0等的认证插件。以基于数据库的认证插件emqx_auth_username为例,添加以下配置:
```
auth.plugindir = data/loaded_plugins, /path/to/emqx_auth_username
```
其中,/path/to/emqx_auth_username为emqx_auth_username插件所在的路径。
4. 配置emqx_auth_username插件。在emqx_auth_username插件的配置文件auth_username.conf中,添加以下内容:
```
## emqx_auth_username.config
## Connection settings for the authentication database.
##
## MySQL/MariaDB:
## mysql_host (default: "localhost")
## mysql_port (default: 3306)
## mysql_database (default: "emqx_auth")
## mysql_username (default: "root")
## mysql_password (default: "")
## mysql_pool_size (default: 8)
##
## PostgreSQL:
## pgsql_host (default: "localhost")
## pgsql_port (default: 5432)
## pgsql_database (default: "emqx_auth")
## pgsql_username (default: "postgres")
## pgsql_password (default: "")
## pgsql_pool_size (default: 8)
##
## Redis:
## redis_host (default: "localhost")
## redis_port (default: 6379)
## redis_database (default: 1)
## redis_pool_size (default: 8)
##
## Docs: https://github.com/emqx/emqx-auth-username#configuration
##
auth.username.mysql_host = your_mysql_host
auth.username.mysql_port = your_mysql_port
auth.username.mysql_database = your_mysql_database
auth.username.mysql_username = your_mysql_username
auth.username.mysql_password = your_mysql_password
auth.username.mysql_pool_size = 8
```
其中,your_mysql_host、your_mysql_port、your_mysql_database、your_mysql_username、your_mysql_password为你的MySQL数据库相关信息,需要根据实际情况进行配置。
5. 在数据库中创建相应的用户和密码。以MySQL数据库为例,可以使用以下命令创建一个名为test的用户,并设置密码:
```
CREATE USER 'test'@'localhost' IDENTIFIED BY 'password';
```
6. 在EMQ X中创建相应的ACL规则,以控制用户的访问权限。具体操作方法请参考EMQ X官方文档。
完成以上步骤后,就可以使用用户名和密码进行MQTT连接认证了。