Linux上MQTT搭建

linux系统为Ubuntu16.04 server

1、安装软件

输入以下指令,逐个安装

apt-get install gcc
apt-get install cmake
apt-get install openssl
apt-get install openssl-dev

新建software文件夹,下载mosquitto,下载并解压:

mkdir software
cd software
wget http://mosquitto.org/files/source/mosquitto-1.6.8.tar.gz
tar -xzvf mosquitto-1.6.8.tar.gz

2、可选择下列软件安装

安装c-areas(支持异步DNS查找的库)

wget http://c-ares.haxx.se/download/c-ares-1.14.0.tar.gz
tar xvf c-ares-1.14.0.tar.gz
cd c-ares-1.14.0
./configure
make
sudo make install

安装lib-uuid(支持为每个连接用户端生成唯一uuid)

apt-get install libuuid-dev

(此步骤没经过测试)

安装libwebsockets(支持需使用websocket的应用):

wget https://github.com/warmcat/libwebsockets/archive/v1.3-chrome37-firefox30.tar.gz
tar zxvf v1.3-chrome37-firefox30.tar.gz
cd libwebsockets-1.3-chrome37-firefox30
mkdir build
cd build
cmake .. -DLIB_SUFFIX=64
make install

(此步骤没经过测试)

上面安装不成功不影响下列的操作

编译安装mosquitto

make
sudo make install

注意:如果在后续使用过程中找不到libmosquitto.so.1的话,在software下输入以下指令修改一下libmosquitto.so的位置:

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

3、启动测试

创建用户

sudo groupadd mosquitto
sudo useradd -g mosquitto mosquitto

程序配置

mv /etc/mosquitto/mosquitto.conf.example /etc/mosquitto/mosquitto.conf

启动程序

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

默认端口为1883


最后再另外打开一个terminal,在一个窗口(订阅)输入:

mosquitto_sub -t hello

另一个窗口(发布)输入:

mosquitto_pub -t hello -h localhost -m "hello world!"

猜你喜欢

转载自www.cnblogs.com/zonkidd/p/12108782.html