服務器操作系統爲CentOS7。
添加yum源
1、在服務器下載並重命名。
http://download.opensuse.org/repositories/home:/oojah:/mqtt/CentOS_CentOS-7/home:oojah:mqtt.repo
2、手動創建編輯並上傳。
到官網下載repo資源。
http://download.opensuse.org/repositories/home:/oojah:/mqtt/CentOS_CentOS-7/home:oojah:mqtt.repo
本地新建文件mosquitto.repo,將上面鏈接中的內容拷貝到文件中並保存。上傳到服務器/etc/yum.repos.d/目錄中。
安裝
yum search all mosquitto
yum install mosquitto mosquitto-clients
中間會有兩次選擇,直接默認的輸入y確定就好了。
配置
安裝完成後,配置文件在/etc/mosquitto/目錄下。
Mosquitto配置文件名爲mosquitto.conf。
[root@centos7_148 conf.d]# cat /etc/mosquitto/mosquitto.conf
# Place your local configuration in /etc/mosquitto/conf.d/
pid_file /var/run/mosquitto.pid
persistence true
persistence_location /var/lib/mosquitto/
#log_dest file /var/log/mosquitto/mosquitto.log
include_dir /etc/mosquitto/conf.d
[root@centos7_148 conf.d]#
查看配置文件內容。引入了/etc/mosquitto/conf.d/文件夾。自定義的配置文件就是放在這個文件夾中。
將默認配置文件拷貝到自定義配置文件夾中並重命名爲.conf文件。
/etc/mosquitto/mosquitto.conf.example爲默認配置文件,拷貝到/etc/mosquitto/conf.d/文件夾下,並重命名爲mosquitto.conf。
然後根據需要配置文件。
[root@centos7_148 ~]# cd /etc/mosquitto/conf.d
[root@centos7_148 conf.d]# ll
total 40
-rw-r--r--. 1 root root 36806 Jul 4 17:28 mosquitto.conf
-rw-r--r--. 1 root root 142 Jun 21 22:47 README
[root@centos7_148 conf.d]# ll /etc/mosquitto/
total 52
-rw-r--r--. 1 root root 230 Jun 21 22:47 aclfile.example
drwxr-xr-x. 2 root root 19 Jul 4 17:22 ca_certificates
drwxr-xr-x. 2 root root 19 Jul 4 17:22 certs
drwxr-xr-x. 2 root root 40 Jul 4 17:28 conf.d
-rw-r--r--. 1 root root 235 Jun 21 22:47 mosquitto.conf
-rw-r--r--. 1 root root 36806 Jun 21 22:47 mosquitto.conf.example
-rw-r--r--. 1 root root 23 Jun 21 22:47 pskfile.example
-rw-r--r--. 1 root root 355 Jun 21 22:47 pwfile.example
[root@centos7_148 conf.d]#
啓動/停止
啓動 sudo /etc/init.d/mosquitto start
停止 sudo /etc/init.d/mosquitto stop
使用
打開兩個連接,分別啓動訂閱和發佈。
訂閱: mosquitto_sub -t mqtt (mqtt topic爲mqtt)
發佈: mosquitto_pub -h localhost -t mqtt -m “hello” (mqtt topic爲mqtt; 發送消息爲hello)