前置条件:
- 环境: VMWare 12.x + Ubuntu14 64bits
- 所需软件:VIM
搭建mosquitto:
-
添加仓库,更新软件包
sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
sudo apt-get update这部分我更新软件包时,公钥报错: 解决方案:
>sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys xxxxxxxxxxxxxxxx #此处xxxxxxxx为错误提示的key
-
安装mosquitto组件
sudo apt-get install mosquitto
sudo apt-get install mosquitto-clients -
修改配置文件mosquitto.conf
路径: /etc/mosquito/
-
创建pwfile
touch /etc/mosquito/pwfile
mosquitto_passwd /etc/mosquito/pwfile [username] -
编辑aclfile文件
vi /etc/mosquitto/aclfile
-
以配置文件启动
mosquito –c /etc/mosquitto/mosquitto.conf –d
测试mosquitto:
-
Subscribe:
mosquitto_sub –h localhost –t “test/#” –u beelinker2 –P 123456 –i “client1”
-
Publish:
mosquitto_pub –h localhost –t “test/abc” –u beelinker1 –P 123456 –i “client3” –m “contextxxxxxxxx”
其他操作
-
查看进程
ps aux|grep mosquitto
-
结束进程
kill -9 进程码
其他问题
-
启用mosquitto.conf,出现 “address already in use” 的问题时,
查看当前mosquitto 进程 ,并删除。
如果无法正常结束进程,删除路径下mosquitto.conf 文件,然后执行删除操作。下次启用时重新配置。 -
Connection Refused
用户名密码不匹配。关注pwfile、aclfile文件,及mosquitto.conf文件中allow_anoymous参数值。
-
客户端1 订阅主题后,客户端2 发布同主题信息,没有反应。
几个问题:
1、在aclfile中不能追求整齐随意使用制表符,定义参数之间用 “ ” 代替。
2、在配置好 mosquitto.conf 后,不通过mosquitto -c mosquitto.conf -d的方式启动,就不会出现服务stop/waiting的问题,使用service mosquitto start启动
拓展
- 外网访问本地mqtt服务器
-
下载安装配置花生壳内网穿透服务
dpkg-i phddns_3.0_x86_64.deb #安装
phddns status #查看phddns状态
-
登录花生壳管理平台, 使用SN码登录,绑定激活申请好的域名,建立端口映射。