数日前にLinuxにmqttサーバーをデプロイする必要があります。情報を確認したところ、デプロイメントテストは成功しました。これで、操作プロセスがここにまとめられました。
サーバ
1.必要な依存関係をインストールします
安装编译工具
1、yum install gcc-c++
跨平台的安装(编译)工具
2、yum install cmake
第三方软件开发时使用的Lib包
3、yum install openssl-devel
4、yum install libuuid-devel
5、yum install c-areas
6、yum install uuid-devel
2.インストールパッケージをダウンロードします
(1)libwebsocketsをダウンロードする
wget https://github.com/warmcat/libwebsockets/archive/v1.5-chrome47-firefox41.tar.gz
(2)mosquittoをダウンロードする
wget http://mosquitto.org/files/source/mosquitto-1.6.8.tar.gz
3.解凍、コンパイル、インストール
(1)libwebsocketsを解凍、コンパイル、およびインストールします
tar zxvf libwebsockets-1.5-chrome47-firefox41.tar.gz
cd libwebsockets-1.5-chrome47-firefox41
mkdir build
cd build
cmake .. //编译
make //编译
make install //安装
(2)mosquittoの構成config.mkを解凍して変更し、websocketをサポートします(websocketはデフォルトではサポートされていません)
tar zxvf mosquitto-1.8.8.tar.gz
cd mosquitto-1.8.8
//将config.mk 的WITH_WEBSOCKETS:=NO 修改 为 WTIH_WEBSOCKETS:=yes
vim config.mk
(3)mosquittをコンパイルしてインストールします
make
make install
(4)mosquittoの構成ファイルを変更します
cd /etc/mosquitto
cp mosquitto.conf.example mosquitto.conf
vim mosquitto.conf
ポートの競合がない限り、ポートとリスナーの両方をカスタマイズできます。
(5)mosquittoユーザーを追加し、mosquittoサービスを開始します
adduser mosquitto // mosquitto -c / etc / mosquitto / mosquitto.conf
フォアグラウンド起動メソッド
mosquitto-c /etc/mosquitto/mosquitto.conf -d //バックグラウンド起動メソッド(推奨)
启PIN报错:
共有ライブラリのロード中にエラーが発生しました:libwebsockets.so.5:共有オブジェクトファイルを開くことができません:そのようなファイルまたはディレクトリはありません
インストールされているlibwebsocketsライブラリが見つからないため、次のソフトリンクコマンドを実行します。
ln -s /usr/local/lib/libwebsockets.so.5 /usr/lib64/libwebsockets.so.5
ldconfig
mosquitto -c /etc/mosquitto/mosquitto.conf -d
これまでのところ、mqttサーバーは正しく開くことができます
クライアント
まず、問題を解決します。mosquittoクライアントプログラムを実行すると、動的ライブラリが見つかりません:libmosquitto.so.1
mosquitto_sub:共有ライブラリのロード中にエラーが発生しました:libmosquitto.so.1:共有オブジェクトファイルを開くことができません:そのようなファイルまたはディレクトリはありません
操作:
ln -s /usr/local/lib/libmosquitto.so.1 /usr/lib/libmosquitto.so.1
ldconfig
1.匿名クライアント
別のサーバーウィンドウを開き、(サブスクリプション)ウィンドウに入ります。
mosquitto_sub -t hello
別の(リリース)ウィンドウに入る:
mosquitto_pub -t hello -h localhost -m "hello world"
効果画像:
2.アカウントとパスワードのクライアントを作成します
(1)ユーザー名とパスワードの設定、匿名ログインは許可されていません
mosquitto.confファイル(/etc/mosquitto/mosquitto.conf)を開き、allow_anonymousノードを見つけます。このノードの機能は、匿名ユーザーログインを有効にするかどうかです。前の#を削除し、falseに変更します。651行目あたり
(2)ユーザーパスワードファイルパス
を設定しますpassword_fileノードを見つけます。このノードは、構成するユーザーが保存される場所をサーバーに通知します。この構成を開き、行669の周りにpwfile.exampleファイルのパス(絶対パスであることに注意してください)を指定します。
password_file /etc/mosquitto/pwfile.example
(3)ユーザー名を作成する
端末を入力します。最後はユーザー名adminです。その後、パスワードと確認パスワードが自動的にポップアップ表示されます。入力するだけです。(2回目のユーザー作成時に-cを追加する必要はありません。-cを追加すると、最初に作成したユーザーが上書きされます。)
mosquitto_passwd -c /etc/mosquitto/pwfile.example admin
(4)サーバーを再起動します。
1 打印并查看mosquitto 的进程id :
ps -ef | grep mos
2 停止线程服务:
kill -9 9307
3 再次启动mqtt:
mosquitto -c /etc/mosquitto/mosquitto.conf -d
次に
、2つのウィンドウを開き、最初にサブスクライブします
mosquitto_sub -h localhost -t mtopic -u admin -P admin
アナウンサー
mosquitto_pub -h localhost -t mtopic -u admin -P admin -m "test"