MQTT--linux安装部署(CentOS)

OS环境:CentOS6.5

1、安装依赖

yum -y install gcc gcc-c++ openssl-devel c-ares-devel libuuid-devel wget cmake

2、为mosquitto增加websocket支持,安装文本socket库文件 ,解压缩

# tar zxfv libwebsockets-v1.6-stable.tar.gz

  

3、运行 websocket

# cd libwebsockets-v1.6-stable

# mkdir bulid

# cd bulid

# cmake ..

# make && make install

  

4、安装mosquitto

下载mqtt--- mosquitto-1.4.8.tar.gz, 解压缩

# tar zxfv mosquitto-1.4.8.tar.gz

  

5、修改config.mk文件以使后面编译的mosquitto文件支持websocket

# cd /mosquitto-1.4.8 

  


mosquitto-1.4.8目录下的config.mk文件,把config.mk 文件中的 WITH_WEBSOCKETS:=no 改为yes

保存后,执行

# make && make install

# ln -s /usr/local/lib/libwebsockets.so.6 /usr/lib64/libwebsockets.so.6

# groupadd mosquitto

# useradd -g mosquitto mosquitto

 

6、创建mosquitto.conf、pwfile文件

# cd /etc/mosquitto/

 

如果该目录下没有mosquitto.conf 和 pwfile 
执行

# cp mosquitto.conf.example mosquitto.conf

# cp pwfile.example pwfile

  

 

然后修改 文件mosquitto.conf ,

511 allow_anonoymous true
.
.
.
537 password_file /etc/mosquitto/pwfile

  

 

并在文件最后加入

port 1883
listener 8080
protocol websockets

  

 

7、启动mqtt

# mosquitto -c /etc/mosquitto/mosquitto.conf

  

 

8、测试 
另外打开两个终端 
终端一: 
 这里其实就是订阅一个主题为”mqtt”的消息。”mqtt”为主题名,假如有客户端发布了主题为”mqtt”的消息,这个终端将会收到消息的内容。

mosquitto_sub -t mqtt

  

 

终端二: 
 这里就是发布一个主题为”mqtt”,内容为”hello mqtt”的消息。

mosquitto_pub -h localhost -t mqtt -m "hello mqtt"

  

 

这时终端一会收到:hello mqtt

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

  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值