- 1、背景
- 2、安装mqtt服务器
- 3、查询mqtt是否正确运行
- 4、测试mqtt服务
博主在个人阿里云服务器上安装mqtt服务器,可以远程和客户调试代码。
阿里云操作系统:ubuntu18.04
采用ubuntu系统最直接的apt安装:
sudo apt install mosquitto
出现提示需要输入y,然后等到执行即可。
安装日志如下:
root@iZu2Z:~# sudo apt install mosquitto Reading package lists... Done Building dependency tree Reading state information... Done The following packages were automatically installed and are no longer required: libopts25 sntp Use 'sudo apt autoremove' to remove them. The following additional packages will be installed: libev4 libuv1 libwebsockets8 The following NEW packages will be installed: libev4 libuv1 libwebsockets8 mosquitto 0 upgraded, 4 newly installed, 0 to remove and 243 not upgraded. Need to get 279 kB of archives. After this operation, 771 kB of additional disk space will be used. Do you want to continue? [Y/n] y Get:1 http://mirrors.cloud.aliyuncs.com/ubuntu bionic/main amd64 libuv1 amd64 1.18.0-3 [64.4 kB] Get:2 http://mirrors.cloud.aliyuncs.com/ubuntu bionic/universe amd64 libev4 amd64 1:4.22-1 [26.3 kB] Get:3 http://mirrors.cloud.aliyuncs.com/ubuntu bionic/universe amd64 libwebsockets8 amd64 2.0.3-3build1 [71.8 kB] Get:4 http://mirrors.cloud.aliyuncs.com/ubuntu bionic-updates/universe amd64 mosquitto amd64 1.4.15-2ubuntu0.18.04.3 [116 kB] Fetched 279 kB in 0s (1,259 kB/s) debconf: unable to initialize frontend: Dialog debconf: (Dialog frontend requires a screen at least 13 lines tall and 31 columns wide.) debconf: falling back to frontend: Readline Selecting previously unselected package libuv1:amd64. (Reading database ... 112043 files and directories currently installed.) Preparing to unpack .../libuv1_1.18.0-3_amd64.deb ... Progress: [ 0%] [.........................................................................................] Unpacking libuv1:amd64 (1.18.0-3) .........................................................................] Progress: [ 10%] [########.................................................................................] Selecting previously unselected package libev4.............................................................] Preparing to unpack .../libev4_1%3a4.22-1_amd64.deb ... Unpacking libev4 (1:4.22-1) ...###.........................................................................] Progress: [ 24%] [#####################....................................................................] Selecting previously unselected package libwebsockets8:amd64...............................................] Preparing to unpack .../libwebsockets8_2.0.3-3build1_amd64.deb ... Unpacking libwebsockets8:amd64 (2.0.3-3build1) ............................................................] Progress: [ 38%] [#################################........................................................] Selecting previously unselected package mosquitto.######...................................................] Preparing to unpack .../mosquitto_1.4.15-2ubuntu0.18.04.3_amd64.deb ... Unpacking mosquitto (1.4.15-2ubuntu0.18.04.3) ...###########...............................................] Progress: [ 52%] [##############################################...........................................] Setting up libev4 (1:4.22-1) ...####################################.......................................] Progress: [ 62%] [#######################################################..................................] Processing triggers for ureadahead (0.100.0-21) ...##########################..............................] Setting up libuv1:amd64 (1.18.0-3) ... Progress: [ 71%] [###############################################################..........................] Processing triggers for libc-bin (2.27-3ubuntu1) ...#################################......................] Processing triggers for systemd (237-3ubuntu10.21) ... Processing triggers for man-db (2.8.3-2ubuntu0.1) ... Setting up libwebsockets8:amd64 (2.0.3-3build1) ... Progress: [ 81%] [########################################################################.................] Setting up mosquitto (1.4.15-2ubuntu0.18.04.3) ...############################################.............] Progress: [ 90%] [################################################################################.........] Processing triggers for libc-bin (2.27-3ubuntu1) ...##################################################.....] Processing triggers for ureadahead (0.100.0-21) ... Processing triggers for systemd (237-3ubuntu10.21) ... root@iZu2Z:~#3、查询mqtt是否正确运行
采用如下命令查看mosquitto MQTT是否正确运行:
sudo service mosquitto status
运行如下:
root@iZu2Z:~# sudo service mosquitto status
● mosquitto.service - LSB: mosquitto MQTT v3.1 message broker
Loaded: loaded (/etc/init.d/mosquitto; generated)
Active: active (running) since Tue 2021-12-07 15:09:41 CST; 3min 29s ago
Docs: man:systemd-sysv-generator(8)
Tasks: 1 (limit: 2217)
CGroup: /system.slice/mosquitto.service
└─23651 /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
Dec 07 15:09:41 iZu2Z systemd[1]: Starting LSB: mosquitto MQTT v3.1 message broker...
Dec 07 15:09:41 iZu2Z mosquitto[23638]: * Starting network daemon: mosquitto
Dec 07 15:09:41 iZu2Z mosquitto[23638]: ...done.
Dec 07 15:09:41 iZu2Z systemd[1]: Started LSB: mosquitto MQTT v3.1 message broker.
root@iZu2Z:~#
4、测试mqtt服务
4.1、在阿里云上安装了MQTT服务,然后将阿里云的TCP端口请打开,如下:
4.2、在我的本地ubuntu18.04台式机上,打开一个终端,
执行以下命令,订阅主题"jn10010537":
mosquitto_sub -h "159.176.23.180" -t "jn10010537" -v
4.3、在我的本地ubuntu18.04台式机上,打开另外一个终端,
执行以下命令,发布消息到主题 “jn10010537”:
mosquitto_pub -h "159.176.23.180" -t "jn10010537" -m "hello jn10010537"
可以看到mosquitto_sub对应的终端,将及时收到"hello jn10010537"消息。
注意:使用mosquitto_sub、mosquitto_pub需要安装mqtt客户端,即:
sudo apt install mosquitto-clients



