Mosquitto 安装(ubuntu & docker)

ubuntu安装

安装mosquitto:$sudo apt-get install mosquitto


安装开发包:$sudo apt-get install libmosquitto-dev
 

安装客户端:$sudo apt-get install mosquitto-clients


查询运行状态:$sudo service mosquitto status 

=================================
启动MQTT Broker :mosquitto -c /etc/mosquitto/mosquitto.conf -d
=================================

本机测试


订阅主题"mqtt":$mosquitto_sub -h localhost -t "mqtt"-v
发布消息到主题 “mqtt”:$mosquitto_pub -h localhost -t "mqtt"-m "Hello MQTT"


网络测试(test.mosquitto.org)


可以用公网MQTT服务器:test.mosquitto.org 进行消息收发测试

https://mosquitto.org/documentation/

 鉴权配置:路径 /etc/mosquitto

新增:

port 1883   //服务绑定的端口号

max_connections -1   //允许的最大连接数,-1表示没有限制

allow_anonymous false     //禁止匿名登录

password_file /etc/mosquitto/pwfile     //增加用户密码配置

acl_file /etc/mosquitto/aclfiel       //增加操作配置

pwfile文件配置    

使用mosquitto自带的mosquitto_passwd命令生成密码文件:

mosquitto_passwd -c [最终生成的password_file文件]  [用户名]      //每次都只会生成只包含一个用户的文件

mosquitto_passwd -b [最终生成的password_file文件]  [用户名]      //新增一个用户,不会覆盖之前已生成的用户

aclfile文件配置

例:user admin

       topic read topic1/topic2/topic3/#

       topic write topic1/topic2/topic3/#

       user admin

       topic #  =========================================================================

docker安装

下载mqtt:

docker pull eclipse-mosquitto

启动mqtt:

docker run -it --name=mosquitto -p 1883:1883 -d eclipse-mosquitto

更改账号密码:

(1)进入容器:docker exec -it mosquitto sh

(2)进入文件夹:cd /mosquitto/config

(3)打开配置文件 vi mosquitto.conf

(4)增加:listener 1883(如果不添加,只有本机才能够访问,其它地址访问不了)

(5)设置:allow_anonymous false(可以 / allow_anonymous false 搜索,按n键搜索下一条)

(6)搜索:password_file,打开注释,修改为password_file /mosquitto/config/pwdfile.conf

(7)保存退出(密码文件位置,写绝对地址)

(8)新建文件:touch pwdfile.conf(退出到mosquitto.conf 所在位置)

(9)写入账号密码:mosquitto_passwd -b pwdfile.conf admin public (admin 是账号,public 是密码)
(10)退出容器,重启服务:docker restart mosquitto。

在mosquitto 2.xx中,不配置密码,客户端会显示无权限连接

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值