root@ros-OptiPlex-3050:~# sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa

More info: https://launchpad.net/~mosquitto-dev/+archive/ubuntu/mosquitto-ppa
Press [ENTER] to continue or ctrl-c to cancel adding it

gpg: keyring `/tmp/tmpdwr_iinr/secring.gpg' created
gpg: keyring `/tmp/tmpdwr_iinr/pubring.gpg' created
gpg: requesting key 262C4500 from hkp server keyserver.ubuntu.com
gpg: /tmp/tmpdwr_iinr/trustdb.gpg: trustdb created
gpg: key 262C4500: public key "Launchpad mosquitto" imported
gpg: Total number processed: 1
gpg: imported: 1 (RSA: 1)
OK

root@ros-OptiPlex-3050:~# sudo apt-get update

root@ros-OptiPlex-3050:~# sudo apt-get install mosquitto

root@ros-OptiPlex-3050:~# sudo apt-get install mosquitto-dev

root@ros-OptiPlex-3050:~# sudo apt-get instaroot@ros-OptiPlex-3050:~# sudo apt-get install mosquitto-clientsll mosquitto-clients

root@ros-OptiPlex-3050:~# sudo service mosquitto status
● mosquitto.service - Mosquitto MQTT v3.1/v3.1.1 Broker
Loaded: loaded (/lib/systemd/system/mosquitto.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:mosquitto.conf(5)
man:mosquitto(8)

#打开一个终端,执行以下命令订阅主题"mqtt"
mosquitto_sub -h localhost -t "mqtt" -v

#打开另外一个终端,发布消息到主题 “mqtt”
mosquitto_pub -h localhost -t "mqtt" -m "Hello MQTT"

#现在你会看到消息被显示在前一个终端上了.

#通过以下命令,可以定于到官放所有测试的mqtt信息,当然你发布的信息别人也能订阅到
mosquitto_sub -h test.mosquitto.org -t "#"   -v

#也可以在官方mqtt服务下发布自己专属的信息
mosquitto_sub -h test.mosquitto.org -t  "myxyz123"  -v

#订阅自己的信息
mosquitto_pub -h test.mosquitto.org -t "myxyz123"  -m  "hello mqtt"
12-22 06:47