Ubuntu - 22.04 - Server - MQTT ブローカ(mosquitto)


 クラウディア


1. 概要
2. インストール
3. 設定
4. 起動
5. 疎通テスト
6. 参考サイト

1. 概要

 「MQTT」を使用するのです。  ちなみに、「GitHUB」からソースをもってきて、インストールしようとしてみたのですが・・・。  ハードルが高すぎて、うまくいきませんでした。

2. インストール

 「MQTT」のサーバとクライアントをインストールします。

sudo apt install -y mosquitto mosquitto-clients

3. 設定

 設定ファイルは。

/etc/mosquitto/mosquitto.conf
 でありまして、下記のように記述されています。

# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example

pid_file /run/mosquitto/mosquitto.pid

persistence true
persistence_location /var/lib/mosquitto/

log_dest file /var/log/mosquitto/mosquitto.log

include_dir /etc/mosquitto/conf.d
 今んとこ、変更なしで、このままにしておきます。

4. 起動

 「root」ユーザ権限で。  再起動時の起動を有効化して。

sudo systemctl enable mosquitto
 起動します。

sudo systemctl start mosquitto
 起動を確認。

sudo systemctl status mosquitto
 およそ、下記のようなメッセージが表示されれば、起動できています。

● mosquitto.service - Mosquitto MQTT Broker
     Loaded: loaded (/lib/systemd/system/mosquitto.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2025-11-12 22:50:51 UTC; 46min ago
       Docs: man:mosquitto.conf(5)
             man:mosquitto(8)
   Main PID: 2272 (mosquitto)
      Tasks: 1 (limit: 4558)
     Memory: 1.4M
        CPU: 3.465s
     CGroup: /system.slice/mosquitto.service
             mq2272 /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf

Nov 12 22:50:51 ubuntu2204 systemd[1]: Starting Mosquitto MQTT Broker...
Nov 12 22:50:51 ubuntu2204 systemd[1]: Started Mosquitto MQTT Broker.

5. 疎通テスト

 疎通テストを行います。  端末を2台分用意して。  1台で、「subscriber」として待ち受けます。

mosquitto_sub -h localhost -t test/topic -u "myuser" -P "mypassword"
 もう1台で、「publisher」としてメッセージを送信します。

mosquitto_pub -h localhost -t test/topic -m "message send" -u "myuser" -P "mypassword"
 「subscriber」側に。

message send
 と表示されれば、疎通テストは「OK」です。

6. 参考サイト

 本ページは、下記のサイトおよび「ChatGPT」くんを参考にさせていただきました。
【Mosquitto】MQTTブローカの構築・動作確認 (ユーザ認証・TLS暗号化まで) #mqtt

EaseUS
AbemaTV 無料体験