Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3046318
  • 博文数量: 396
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 4209
  • 用 户 组: 普通用户
  • 注册时间: 2016-07-04 13:04
文章分类

全部博文(396)

文章存档

2022年(1)

2021年(2)

2020年(8)

2019年(24)

2018年(135)

2017年(158)

2016年(68)

我的朋友

分类: 嵌入式

2019-03-07 17:02:24

本文就众多MQTT-Server中的mosquitto的安装进行讲解。

一、下载。

对于Ubuntu系统,可以使用sudo apt-get 来安装mosquitto,但是这种方法虽然简单,但是对于配置文件的修改和管理比较麻烦,配置文件需要自己写好然后启动时载入,因此不太推荐。

本人更推荐的是第二种方法,也就是本文重点要讲的,下载tar.gz安装包,进行安装。

首先,到mosquitto的官方网站()下载,tar.gz安装包。本人使用的是1.4.15版本。如果你使用的是服务器版本,你可以使用wget  mosquitto-1.4.15.tar.gz 这个命令直接下载。

二、解压

如果你习惯 控制台命令或是在server版编程的话,使用 tar -xvf mosquitto-1.4.15.tar.gz 命令来进行解压,解药后会产生相应的文件夹,如果你更习惯于图形化安装,使用exact进行提取安装也是不错的选择。

三、安装

重点关注一下config.mk这个文件,这个文件会提供一些基础 配置。

下面我们进行安装。安装使用make命令,但是往往不可以一次make成功,所以,就安装部分进行重点讲解。

如果,使用make 时,提示没有make,首先使用sudo apt-get install make 来执行。

如果,有了可以正常使用make命令,你输入make 会发现,并不能成功make 而是会报错,一般会出现四到五种错误。

1)提示:缺少ssl.h

set -e; for d in lib client src; do make -C ${d}; done
make[1]: Entering directory `/home/jia/Desktop/mqtt/mosquitto-1.4.15/lib'
cc -Wall -ggdb -O2  -I. -I.. -I../lib -fPIC -DWITH_TLS -DWITH_TLS_PSK -DWITH_THREADING -DWITH_SOCKS -DWITH_SRV -c mosquitto.c -o mosquitto.o
In file included from mosquitto.c:33:0:
./mosquitto_internal.h:27:27: fatal error: openssl/ssl.h: No such file or directory
 #  include
                           ^
compilation terminated.
make[1]: *** [mosquitto.o] Error 1
make[1]: Leaving directory `/home/jia/Desktop/mqtt/mosquitto-1.4.15/lib'
make: *** [mosquitto] Error 2

解决办法:使用 sudo apt-get install libssl-dev

2)提示:缺少ares.h

set -e; for d in lib client src; do make -C ${d}; done
make[1]: Entering directory `/home/jia/Desktop/mqtt/mosquitto-1.4.15/lib'
cc -Wall -ggdb -O2  -I. -I.. -I../lib -fPIC -DWITH_TLS -DWITH_TLS_PSK -DWITH_THREADING -DWITH_SOCKS -DWITH_SRV -c mosquitto.c -o mosquitto.o
In file included from mosquitto.c:33:0:
./mosquitto_internal.h:40:20: fatal error: ares.h: No such file or directory
 #  include
                    ^
compilation terminated.
make[1]: *** [mosquitto.o] Error 1
make[1]: Leaving directory `/home/jia/Desktop/mqtt/mosquitto-1.4.15/lib'
make: *** [mosquitto] Error 2

解决办法:使用 sudo apt-get install libc-ares-dev

3)提示:缺少g++

make[2]: Entering directory `/home/jia/Desktop/mqtt/mosquitto-1.4.15/lib/cpp'
g++ -Wall -ggdb -O2  -I. -I.. -I../lib  -fPIC -c mosquittopp.cpp -o mosquittopp.o
make[2]: g++: Command not found
make[2]: *** [mosquittopp.o] Error 127
make[2]: Leaving directory `/home/jia/Desktop/mqtt/mosquitto-1.4.15/lib/cpp'
make[1]: *** [all] Error 2
make[1]: Leaving directory `/home/jia/Desktop/mqtt/mosquitto-1.4.15/lib'
make: *** [mosquitto] Error 2

解决办法 :使用 sudo apt-get install g++

4)提示:没有UUID

read_handle_server.c:31:25: fatal error: uuid/uuid.h: No such file or directory
 #  include
                         ^
compilation terminated.
make[1]: *** [read_handle_server.o] Error 1
make[1]: Leaving directory `/home/jia/Desktop/mqtt/mosquitto-1.4.15/src'
make: *** [mosquitto] Error 2

解决办法: 使用  sudo apt-get install uuid-dev

5)这里说xsltproc命令找不到

    make[1]: Leaving directory `/home/lxb/mosquitto-1.4.14/src'
    set -e; for d in man; do make -C ${d}; done
    make[1]: Entering directory `/home/lxb/mosquitto-1.4.14/man'
    xsltproc mosquitto.8.xml
    make[1]: xsltproc: Command not found
    make[1]: *** [mosquitto.8] Error 127
    make[1]: Leaving directory `/home/lxb/mosquitto-1.4.14/man'
    make: *** [docs] Error 2

解决办法:sudo apt-get install xsltproc

6)然后就是mosquitt.8错误,看错误提示,根源还是找不到docbook.xsl

    set -e; for d in lib client src; do make -C ${d}; done
    make[1]: Entering directory '/home/ctbri/mosquitto-cluster/lib'
    make -C cpp
    make[2]: Entering directory '/home/ctbri/mosquitto-cluster/lib/cpp'
    make[2]: Nothing to be done for 'all'.
    make[2]: Leaving directory '/home/ctbri/mosquitto-cluster/lib/cpp'
    make[1]: Leaving directory '/home/ctbri/mosquitto-cluster/lib'
    make[1]: Entering directory '/home/ctbri/mosquitto-cluster/client'
    make[1]: Nothing to be done for 'all'.
    make[1]: Leaving directory '/home/ctbri/mosquitto-cluster/client'
    make[1]: Entering directory '/home/ctbri/mosquitto-cluster/src'
    make[1]: Nothing to be done for 'all'.
    make[1]: Leaving directory '/home/ctbri/mosquitto-cluster/src'
    set -e; for d in man; do make -C ${d}; done
    make[1]: Entering directory '/home/ctbri/mosquitto-cluster/man'
    xsltproc mosquitto.8.xml
    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
    Makefile:48: recipe for target 'mosquitto.8' failed
    make[1]: *** [mosquitto.8] Error 5
    make[1]: Leaving directory '/home/ctbri/mosquitto-cluster/man'
    Makefile:17: recipe for target 'docs' failed
    make: *** [docs] Error 2

解决办法:sudo apt-get install docbook-xsl

7)有可能出现找不到libmosquitto.so.1的情况

解决办法:sudo cp /usr/local/lib/libmosquitto.so.1 /usr/lib/cp /usr/local/lib/libmosquitto.so.1 /usr/lib/

或者sudo ln -s /usr/local/lib/libmosquitto.so.1 /usr/lib/libmosquitto.so.1

通常大家安装会遇到上述的情况,解决完上述问题,就可以正常使用make命令,make后,使用sudo make install 安装即可

四、使用

首先要注意的是,安装完后的mosquitto的配置文件在根目录下的etc下的mosquitto,使用 cd /etc/mosquitto 就可以找到相应的位置。

之后,使用  cp mosquitto.conf.example mosquitto.conf 命令,复制一份新的conf配置文件。

使用sudo vi  mosquitto.conf  或者 sudo gedit  mosquitto.conf  均可,总之,注意加权限。

入门的话,首先修改这几个地方:

1) user :默认是mosquitto,要更换成你当前的用户

2)port:打开1883端口,如果使用ssl或tls打开8883端口

3)protocol:mqtt

修改好后保存,回到安装目录。

五 、启动

使用, mosquitto -c /etc/mosquitto/mosquitto.conf  开启服务器。

在新建一个端口,使用, netstat -tunlp|grep 1883 查看端口是否被监听

之后,使用 mosquitto_sub -t 'topic' 请求一个订阅

再新建一个终端,使用 mosquitto_pub -t 'topic' -m 'msg1'发布一个订阅,这时候即可查看到订阅消息。

六、附录

最后在附一份他的配置文件中文版:

# =================================================================
# General configuration
# =================================================================

# 客户端心跳的间隔时间
#retry_interval 20

# 系统状态的刷新时间
#sys_interval 10

# 系统资源的回收时间,0表示尽快处理
#store_clean_interval 10

# 服务进程的PID
#pid_file /var/run/mosquitto.pid

# 服务进程的系统用户
#user mosquitto

# 客户端心跳消息的最大并发数
#max_inflight_messages 10

# 客户端心跳消息缓存队列
#max_queued_messages 100

# 用于设置客户端长连接的过期时间,默认永不过期
#persistent_client_expiration

# =================================================================
# Default listener
# =================================================================

# 服务绑定的IP地址
#bind_address

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

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

# cafile:CA证书文件
# capath:CA证书目录
# certfile:PEM证书文件
# keyfile:PEM密钥文件
#cafile
#capath
#certfile
#keyfile

# 必须提供证书以保证数据安全性
#require_certificate false

# 若require_certificate值为true,use_identity_as_username也必须为true
#use_identity_as_username false

# 启用PSK(Pre-shared-key)支持
#psk_hint

# SSL/TSL加密算法,可以使用“openssl ciphers”命令获取
# as the output of that command.
#ciphers

# =================================================================
# Persistence
# =================================================================

# 消息自动保存的间隔时间
#autosave_interval 1800

# 消息自动保存功能的开关
#autosave_on_changes false

# 持久化功能的开关
persistence true

# 持久化DB文件
#persistence_file mosquitto.db

# 持久化DB文件目录
#persistence_location /var/lib/mosquitto/

# =================================================================
# Logging
# =================================================================

# 4种日志模式:stdout、stderr、syslog、topic
# none 则表示不记日志,此配置可以提升些许性能
log_dest none

# 选择日志的级别(可设置多项)
#log_type error
#log_type warning
#log_type notice
#log_type information

# 是否记录客户端连接信息
#connection_messages true

# 是否记录日志时间
#log_timestamp true

# =================================================================
# Security
# =================================================================

# 客户端ID的前缀限制,可用于保证安全性
#clientid_prefixes

# 允许匿名用户
#allow_anonymous true

# 用户/密码文件,默认格式:username:password
#password_file

# PSK格式密码文件,默认格式:identity:key
#psk_file

# pattern write sensor/%u/data
# ACL权限配置,常用语法如下:
# 用户限制:user
# 话题限制:topic [read|write]
# 正则限制:pattern write sensor/%u/data
#acl_file

# =================================================================
# Bridges
# =================================================================

# 允许服务之间使用“桥接”模式(可用于分布式部署)
#connection
#address [:]
#topic [[[out | in | both] qos-level] local-prefix remote-prefix]

# 设置桥接的客户端ID
#clientid

# 桥接断开时,是否清除远程服务器中的消息
#cleansession false

# 是否发布桥接的状态信息
#notifications true

# 设置桥接模式下,消息将会发布到的话题地址
# $SYS/broker/connection//state
#notification_topic

# 设置桥接的keepalive数值
#keepalive_interval 60

# 桥接模式,目前有三种:automatic、lazy、once
#start_type automatic

# 桥接模式automatic的超时时间
#restart_timeout 30

# 桥接模式lazy的超时时间
#idle_timeout 60

# 桥接客户端的用户名
#username

# 桥接客户端的密码
#password

# bridge_cafile:桥接客户端的CA证书文件
# bridge_capath:桥接客户端的CA证书目录
# bridge_certfile:桥接客户端的PEM证书文件
# bridge_keyfile:桥接客户端的PEM密钥文件
#bridge_cafile
#bridge_capath
#bridge_certfile
#bridge_keyfile

 
 
下面说一下mosquitto的集群的安装:

mqtt集群较单机有很多的不同,我们采用在git上的一个开源mqtt集群%E9%9B%86%E7%BE%A4%E7%89%B9%E6%80%A7

安装起来很单机差不多,主要区别于安装的配置文件,在/etc/mosquitto/mosquitto.conf这个文件,加入集群的配置即可
 

node_name node1
node_address 192.168.1.101:1883

node_name node2
node_address 192.168.1.102:1883
 

node_name node3
node_address 192.168.1.103:1883
注意:在make之前编译mosquitto时把config.mk中的WITH_BRIDGE注释掉,把WITH_CLUSTER:=yes取消注释,然后make && make install安装mosquitto即可
---------------------  
作者:嫌疑人X的解忧杂货店  
来源:CSDN  
原文:https://blog.csdn.net/sullivan_jia/article/details/80089187  
版权声明:本文为博主原创文章,转载请附上博文链接!
阅读(4247) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~