源码编译mosquitto

参考 https://mosquitto.org/documentation/

https://www.jianshu.com/p/d8ce71cb0a0d

https://blog.csdn.net/yuanpan1987/article/details/107816237

https://www.jianshu.com/p/9e3cb7042a2e

 

环境 CentOS Linux release 7.5.1804 (Core)

以下均使用root权限执行

  1. 1.     source编译安装

mkdir -p /home/develop/mosquitto/

cd /home/develop/mosquitto/

wget https://mosquitto.org/files/source/mosquitto-2.0.9.tar.gz

tar -zxvf mosquitto-2.0.9.tar.gz

cd mosquitto-2.0.9

make

报错:没有openssl

 源码编译mosquitto

#需安装openssl-devel

yum -y install openssl-devel

再次make报错:没有cjson

 源码编译mosquitto

##安装cjson

下载压缩包https://github.com/DaveGamble/cJSON/archive/master.zip

将下载的cJSON-master.zip放到服务器上/home/develop/mosquitto/,解压并重命名为cJSON

cd cJSON/

mkdir build

cd build/

yum -y install cmake

cmake ..

make

make install

##注意libcjson.so.1位置,安装到了/usr/local/lib64

源码编译mosquitto 

重新编译mosquitto

cd /home/develop/mosquitto/mosquitto-2.0.9

make install

 源码编译mosquitto

make install

 源码编译mosquitto

程序文件默认安装到以下位置

mosquitto:  /usr/local/sbin

configuration: /etc/mosquitto

utility command: /usr/local/bin

 

 

 

  1. 2.     配置启动

cd /etc/mosquitto/

cp /etc/mosquitto/mosquitto.conf.example /etc/mosquitto/mosquitto.conf

mosquitto -c /etc/mosquitto/mosquitto.conf

 源码编译mosquitto

#需要创建mosquitto用户组

groupadd mosquitto

useradd -g mosquitto mosquitto

chown -R mosquitto:mosquitto /etc/mosquitto/

#再次运行

mosquitto -c /etc/mosquitto/mosquitto.conf

#也可以选择后台启动

mosquitto -c /etc/mosquitto/mosquitto.conf  -d

默认配置 不允许匿名、绑定ipv4 1883端口、无密码

 

 

  1. 3.     订阅

mosquitto_sub -t test

报错mosquitto_sub: error while loading shared libraries: libmosquitto.so.1: cannot open shared object file: No such file or directory

 源码编译mosquitto

#创建软链接

ln -s /usr/local/lib/libmosquitto.so.1  /usr/lib/libmosquitto.so.1

ldconfig

#再次订阅

mosquitto_sub -t test

mosquitto_sub: error while loading shared libraries: libcjson.so.1: cannot open shared object file: No such file or directory

 源码编译mosquitto

#需要将libcjson.so.1所在目录/usr/local/li64目录添加到 /etc/ld.so.conf 文件中

vim /etc/ld.so.conf.d/usr-libs.conf   

添加 /usr/local/lib64

#更新库

ldconfig

 源码编译mosquitto

本机订阅发布测试:

#订阅

mosquitto_sub -t test

#另开一个窗口进行发布

mosquitto_pub  -t test -m "hello"

#可以在订阅窗口看到

 源码编译mosquitto

在云端服务器配置时出现远程订阅不通的情况,可以看到1883绑定到127.0.0.1的地址了,需要修改为0.0.0.0

 源码编译mosquitto

vim /etc/mosquitto/mosquitto.conf,

#绑定端口、ip

listener 1883  0.0.0.0

#允许匿名(此时无需密码)

allow_anonymous true

 ,重启mosquitto,此时1883绑定到了0.0.0.0

源码编译mosquitto

 

 

 

 

  1. 4.     用户密码权限(可跳过)

https://blog.csdn.net/github_34709302/article/details/91973146

 

 

  1. 5.     持久化(消息离线保存)

vim /etc/mosquitto/mosquitto.conf,打开持久化开关

persistence true

重启服务生效

 

 

  1. 6.     设置开机启动服务

vim /usr/lib/systemd/system/mosquitto.service

[Unit]

Description=mosquitto-2.0.9

After=network.target

Wants=network.target

[Service]

Type=forking

User=mosquitto

Group=mosquitto

ExecStart=/usr/local/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf -d

ExecReload=/bin/kill -s HUP $MAINPID

Restart=on-failure

# Give a reasonable amount of time for the server to start up/shut down

TimeoutSec=300

# Place temp files in a secure directory, not /tmp

PrivateTmp=true

[Install]

WantedBy=multi-user.target

#设置

chmod a+x /usr/lib/systemd/system/mosquitto.service

systemctl daemon-reload

systemctl enable mosquitto

systemctl start/stop/restart mosquitto

 

上一篇:Linux


下一篇:linux 安装samba共享文件并通过自定义主机名访问