centos6.8下mosquitto简介及搭建 centos6.8下mosquitto简介及搭建

 

centos6.8下mosquitto简介及搭建

216人阅读  评论(0)  收藏  举报
  分类:
mqtt(3) 
1.Mosquitto简介
一款实现了消息推送协议 MQTT v3.1 的开源消息代理软件,提供轻量级的,支持可发布/可订阅的的消息推送模式,
使设备对设备之间的短消息通信变得简单,比如现在应用广泛的低功耗传感器,手机、嵌入式计算机、微型控制器等移动设备。
一个典型的应用案例就是 Andy Stanford-ClarkMosquitto(MQTT协议创始人之一)在家中实现的远程监控和自动化。
并在 OggCamp 的演讲上,对MQTT协议进行详细阐述。
2.所需安装包
libwebsockets-2.4.1.tar.gz,mosquitto-1.4.14.tar.gz
下载后分别上传至服务器/home目录
3.下载地址
libwebsockets:https://github.com/warmcat/libwebsockets/releases
mosquitto:https://github.com/eclipse/mosquitto/releases
4.安装依赖
#yum -y install gcc gcc-c++ openssl-devel  c-ares-devel libuuid-devel wget cmake xmlto
5.增加websocket支持
cd /home
tar -zxvf libwebsockets-2.4.1.tar.gz
cd libwebsockets-2.4.1
mkdir bulid
cd bulid
cmake ..
make && make install
6.安装mosquitto
6.1解压缩
cd /home
tar -zxvf mosquitto-1.4.14.tar.gz
6.2添加websocket支持
修改config.mk文件以使后面编译的mosquitto文件支持websocket
cd mosquitto-1.4.14
找到mosquitto-1.4.14目录下的config.mk文件,把config.mk文件中的WITH_WEBSOCKETS:=no改为yes
# Build with websockets support on the broker.
WITH_WEBSOCKETS:=yes
修改后,保存。
6.3安装
make && make install
如果安装过报错如下:
warning: failed to load external entity "/usr/share/xml/docbook/stylesheet/docbook-xsl/manpages/docbook.xsl"
compilation error: file manpage.xsl line 3 element import
xsl:import : unable to load /usr/share/xml/docbook/stylesheet/docbook-xsl/manpages/docbook.xsl
compilation error: file mosquitto.8.xml line 4 element refentry
xsltParseStylesheetProcess : document is not a stylesheet
make[1]: *** [mosquitto.8] Error 5
make[1]: Leaving directory `/home/mosquitto-1.4.14/man'
make: *** [docs] Error 2
则修改/home/mosquitto-1.4.14/man/manpage.xsl
将<xsl:import href="/usr/share/xml/docbook/stylesheet/docbook-xsl/manpages/docbook.xsl"/>
改为:<xsl:import href="/usr/share/sgml/docbook/xsl-stylesheets-1.75.2/manpages/docbook.xsl"/>
重新执行:make && make install命令
如果执行程序时找不到libwebsockets的库文件,执行下面的命令为库做一下符号连接)
ln -s /usr/local/lib/libwebsockets.so.6 /usr/lib64/libwebsockets.so.6
groupadd mosquitto
useradd -g mosquitto mosquitto
6.4创建mosquitto.conf、pwfile文件
cd /etc/mosquitto/
cp mosquitto.conf.example mosquitto.conf
cp pwfile.example pwfile
cp aclfile.example aclfile

6.5mosquitto.conf修改配置如下:

[html]  view plain  copy
  1. # =================================================================  
  2. # General configuration  
  3. # =================================================================  
  4. # 服务进程的PID  
  5. pid_file /var/run/mosquitto.pid  
  6. # 服务端口号  
  7. port 1883  
  8. # websockets端口号  
  9. listener 9090  
  10. # 添加websockets协议支持  
  11. protocol websockets  
  12. # 最大连接数,-1表示不限制  
  13. max_connections -1  
  14. # 服务进程的系统用户  
  15. user mosquitto  
  16.   
  17. # =================================================================  
  18. # Security  
  19. # =================================================================  
  20. # 不允许匿名用户  
  21. allow_anonymous false  
  22. # 用户/密码文件,默认格式:username:password  
  23. password_file /etc/mosquitto/pwfile  
  24. # 配置用户访问控制  
  25. acl_file /etc/mosquitto/aclfile  
  26.   
  27. # =================================================================  
  28. #Persistence configuration  
  29. # =================================================================  
  30. # 消息自动保存的间隔时间:默认30分钟往磁盘写一次,0表示mosquitto关闭才写到磁盘中  
  31. autosave_interval 1800  
  32. # 消息自动保存功能的开关  
  33. autosave_on_changes false  
  34. # 持久化功能的开关  
  35. persistence true  
  36. # 持久化DB文件  
  37. persistence_file mosquitto.db  
  38. # 持久化DB文件目录  
  39. persistence_location /usr/mosquitto/  
  40.   
  41. # =================================================================  
  42. # Logging  
  43. # =================================================================  
  44. # 4种日志模式:stdout、stderr、syslog、topic  
  45. # 日志路径:none 则表示不记日志,此配置可以提升些许性能  
  46. log_dest file /usr/mosquitto/mosquitto.log  
  47. # 选择日志的级别(可设置多项)  
  48. log_type error  
  49. # 是否记录客户端连接信息  
  50. #connection_messages true  
  51. # 是否记录日志时间  
  52. #log_timestamp true  

6.6创建用户及密码

mosquitto_passwd -c /etc/mosquitto/pwfile hthl_pub
输入命令后,控制台会提示输入新建用户的密码,连续输入两次密码后,则会生成一个pwfile文件
注意,mosquitto_passwd -c命令每次都只会生成只包含一个用户的文件,如果你想在passwd.conf中存放多个用户,
可以使用mosquitto_passwd -b 命令: 
mosquitto_passwd -b [最终生成的password_file文件]  [用户名]  [密码]
mosquitto_passwd -b /etc/mosquitto/pwfile hthl_sub 123456
mosquitto_passwd -D 命令删除一个用户
mosquitto_passwd -D [最终生成的password_file文件]  [用户名]

6.7aclfile修改配置如下:

[html]  view plain  copy
  1. #This only affects clients with username "hthl_pub".  
  2. user hthl_pub  
  3. topic write hthl/#  
  4. # This only affects clients with username "hthl_sub".  
  5. user hthl_sub  
  6. topic read hthl/#  

6.8创建存储目录

mkdir /usr/mosquitto
chown -R mosquitto /usr/mosquitto
7.启动 mqtt
mosquitto -c /etc/mosquitto/mosquitto.conf -d
8.推送测试
mosquitto_sub -h 192.168.20.38 -t hthl/hyt -u hthl_sub -P 123456
mosquitto_pub -h 192.168.20.38 -t hthl/hyt -u hthl_pub -P 123456 -m "你好,世界!"
9.错误解决
在安装过程中,或测试过程中可能会遇到错误:
mosquitto_sub: error while loading shared libraries: libmosquitto.so.1: cannot open shared object file: No such file or directory
解决方法:
# cat /etc/ld.so.conf
# echo "/usr/local/lib">>/etc/ld.so.conf
# ldconfig

猜你喜欢

转载自blog.csdn.net/wangshuminjava/article/details/80068511