CentOs 7 下 安装 mqtt 之 mosquitto

2019-09-26  本文已影响0人  骑着大象去上班

安装版本:mosquitto1.4.4
mosquitto官网:http://mosquitto.org/
关于mqtt协议可参考:http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html
libwebsockets下载地址 https://github.com/warmcat/libwebsockets/releases

系统版本: centos 6.7

1. 安装依赖包

yum install gcc gcc-c++
yum install openssl-devel
yum install c-ares-devel
yum install libuuid-devel
yum install wget
yum install cmake
yum install build-essential python quilt devscripts python-setuptools python3 
yum install libssl-dev libc-ares-dev uuid-dev daemon openssl-devel

下载并编译安装libwebsockets
wget https://github.com/warmcat/libwebsockets/archive/v3.2.1.tar.gz
tar zxvf v3.2.1.tar.gz
cd libwebsockets-3.2.1
mkdir build
cd build
cmake .. -DLIB_SUFFIX=64
make install
ldconfig

2. 下载并解压mosquitto

mkdir /usr/local/mqtt
cd /usr/local/mqtt
wget http://mosquitto.org/files/source/mosquitto-1.4.9.tar.gz
tar -xzvf mosquitto-1.4.9.tar.gz
cd mosquitto-1.4.9

3. 修改配置文件

config.mk包括了多个选项, 可按需关闭或开启,但一旦开启则需要先安装对应的模块

vim config.mk

选项 说明 make时的出错信息
WITH_SRV 启用c-areas库的支持,一个支持异步DNS查找的库,见http://c-ares.haxx.se missing ares.h
WITH_UUID 启用lib-uuid支持,支持为每个连接的客户端生成唯一的uuid missing uuid.h
WITH_WEBSOCKETS 启用websocket支持,需安装libwebsockets,对于需要使用websocket协议的应用开启 missing libwebsockets.h
  1. 安装c-areas
yum install c-ares-devel -y

  1. 安装lib-uuid
yum install uuid-devel -y
yum install libuuid-devel -y

  1. 安装libwebsockets
yum install openssl-devel -y

注意:若遇到以上模块无法安装的情况,可将对应模块选项关闭即可,但相应功能也将无法提供

4. 开始安装

 make
make install
程序文件将默认安装到以下位置
路径 程序文件
/usr/local/sbin mosquiotto server
/etc/mosquitto configuration
/usr/local/bin utility command
vim /etc/ld.so.conf.d/liblocal.conf

在文件中添加以下内容:

/usr/local/lib64
/usr/local/lib

刷新

ldconfig

5. 启动

  1. mosquitto默认以mosquitto用户启动(可以通过配置文件修改),需添加用户
groupadd mosquitto
useradd -g mosquitto mosquitto

  1. 修改配置文件
mv /etc/mosquitto/mosquitto.conf.example /etc/mosquitto/mosquitto.conf

# =================================================================
# 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 <username>
# 话题限制:topic [read|write] <topic>
# 正则限制:pattern write sensor/%u/data
#acl_file
# =================================================================
# Bridges
# =================================================================
# 允许服务之间使用“桥接”模式(可用于分布式部署)
#connection <name>
#address <host>[:<port>]
#topic <topic> [[[out | in | both] qos-level] local-prefix remote-prefix]
# 设置桥接的客户端ID
#clientid
# 桥接断开时,是否清除远程服务器中的消息
#cleansession false
# 是否发布桥接的状态信息
#notifications true
# 设置桥接模式下,消息将会发布到的话题地址
# $SYS/broker/connection/<clientid>/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

  1. 设置用户名和密码
    将配置文件中#allow_anonymous true去掉注释,设置为false,#password_file去掉注释并添加密码文件保存的位置
allow_anonymous false
password_file /etc/mosquitto/pwfile.example

mosquitto_passwd -c /etc/mosquitto/pwfile.example 用户名
之后需输入两次密码
注意如果想添加用户
mosquitto_passwd -b /etc/mosquitto/pwfile.example 用户名 密码

同样连续会提示连续输入两次密码。注意第二次创建用户时不用加 -c 如果加 -c 会把第一次创建的用户覆盖。

启动

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

测试

注意:输入密码时特殊字符需要用转义符“\”

问题汇总

出现./mosquitto_sub: error while loading shared libraries: libmosquitto.so.1: cannot open shared object file: No such file or directory\

执行命令:

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

ldconfig

php workerman使用
发布者

<?php
require __DIR__ . './vendor/autoload.php';

use Workerman\Worker;

$worker = new Worker();
$worker->onWorkerStart = function () {
    $qos = 0;
    $port = 8883;
    $keepalive = 90;
    $cleanSession = true;
    $clientId='test123456';
    $topic='test';
    $options = [
        'username' => 'test',
        'password' => '123456',
        'keepalive' => $cleanSession,
        'clean_session' => $keepalive,
        'client_id' => $clientId,
        'debug' => false,
        'ssl'=>[
            'local_pk'    => './mqtt_ssl/privkey.pem',
            'verify_peer' => false,
        ],
    ];
    $clicke = "mqtt://127.0.0.1:$port";
    $mqtt = new Workerman\Mqtt\Client($clicke, $options);
    $mqtt->onConnect = function ($mqtt) use ($topic, $qos) {
        $mqtt->publish(  'test', 'xini workerman mqtt', ['qos' => $qos]);
    };
    $mqtt->connect();
};
Worker::runAll();

订阅者

<?php
require __DIR__ . '/vendor/autoload.php';

use Workerman\Worker;

$worker = new Worker();
$worker->onWorkerStart = function () {
    $qos=0;
    $topic='test';
    $port = 8883;
    $keepalive = 90;
    $cleanSession = true;
    $clientId = 'test123456';
    $options = [

        'username' => 'test',
        'password' => '123456',
        'keepalive' => $cleanSession,
        'clean_session' => $keepalive,
        'client_id' => $clientId,
        'debug' => false,
        'ssl' => [
            'local_pk' => './mqtt_ssl/privkey.pem',
            'verify_peer' => false,
        ],
    ];
    $clicke = "mqtt://127.0.0.1:$port";
    $mqtt = new Workerman\Mqtt\Client($clicke, $options);
    $mqtt->onConnect = function ($mqtt) use ($topic, $qos) {
        $mqtt->subscribe('test', ['qos' => $qos]);
    };
    $mqtt->onMessage = function ($topic, $content) {
        var_dump($topic, $content);
    };
    $mqtt->connect();
};
Worker::runAll();
上一篇下一篇

猜你喜欢

热点阅读