Linux build MQTT server tutorial

My linux version is ubuntu 5.4.0-148-generic (buildd@lcy02-amd64-056). If yours is not ubuntu, there may be a slight difference in the use of some subsequent commands.

1. Install the software

Enter the following commands to install one by one. If you already have these tools in your environment, you don't need to install them again.

apt-get install gcc
apt-get install c++
apt-get install cmake
apt-get install openssl
apt-get install libssl-dev

Create a new folder software in a certain path (not recommended in the root directory). The version I downloaded here is version 1.4.12. If you need other versions, you can change the version number.

mkdir software 
cd software
wget http://mosquitto.org/files/source/mosquitto-1.4.12.tar.gz
tar -xzf mosquitto-1.4.12.tar.gz

install other software

apt-get install uuid-dev
wget http://c-ares.haxx.se/download/c-ares-1.10.0.tar.gz
tar xvf c-ares-1.10.0.tar.gz
cd c-ares-1.10.0
./configure
make
sudo make install

Modify the configuration of mosquitto.

cd mosquitto-1.4.12
vi config.mk

Comment out the WITH_SRV:=yes and WITH_UUID:=yes with #. :wq save and exit

Compile and install mosquitto

make
sudo make install

2. Start the test

create user

sudo groupadd mosquitto
sudo useradd -g mosquitto mosquitto

configuration program

mv /etc/mosquitto/mosquitto.conf.example /etc/mosquitto/mosquitto.conf

starting program

mosquitto -c /etc/mosquitto/mosquitto.conf -d

The program default port is 1883

If you want to see the port of the program, remove -d when starting the program

mosquitto -c /etc/mosquitto/mosquitto.conf

Or you can use the netstat tool to query the port

netstat -anp | grep mosquitto

 Verify MQTT Subscription and Publishing

Subscription window:

mosquitto_sub -t hello

It may be reported that the dynamic library of libcares.so.2 is missing when executing the above command (the lack of XXX.so can be solved by the following methods).

  Use the find command to query where the libcare.so.2 dynamic library path is.

 

  Create a soft connection

ln -s ./c-ares-1.10.0/.libs/libcares.so.2 libcares.so.2 

Add environment variables

export LD_LIBRARY_PATH=./

subscribe again

mosquitto_sub -t hello

This is a successful subscription.

One disadvantage of solving this problem according to me above is that every time a window is reopened, the environment variables need to be reconfigured to subscribe and publish messages. The second method is to create a soft link under the default environment variable path, so that you don't need to reconfigure the environment variable every time.

Re-open a window and release the window: Execute the following command

mosquitto_pub -t hello -h localhost -m "hello world!"

Program screenshot

 At this point, the MQTT server has been configured, I hope it can help you.

Guess you like

Origin blog.csdn.net/m0_64655190/article/details/130601911