MQTT--linux installation and deployment (CentOS)

OS environment: CentOS6.5

1. Install dependencies

yum -y install gcc gcc-c++ openssl-devel c-ares-devel libuuid-devel wget cmake

2. Add websocket support for mosquitto, install the text socket library file  , and decompress it

# tar zxfv libwebsockets-v1.6-stable.tar.gz

  

3. Run websocket

# cd libwebsockets-v1.6-stable

# mkdir bulid

# cd bulid

# cmake ..

# make && make install

  

4. Install mosquitto

Download mqtt---mosquitto-1.4.8.tar.gz,  unzip

# tar zxfv mosquitto-1.4.8.tar.gz

  

5. Modify the config.mk file to make the later compiled mosquitto file support websocket

# cd /mosquitto-1.4.8 

  


In the config.mk file in the mosquitto-1.4.8 directory, change WITH_WEBSOCKETS:=no in the config.mk file to yes

After saving, execute

# make && make install

# ln -s /usr/local/lib/libwebsockets.so.6 /usr/lib64/libwebsockets.so.6

# groupadd mosquitto

# useradd -g mosquitto mosquitto

 

6. Create mosquitto.conf and pwfile files

# cd /etc/mosquitto/

 

If there is no mosquitto.conf and pwfile in this directory, execute 

# cp mosquitto.conf.example mosquitto.conf

# cp pwfile.example pwfile

  

 

Then modify the file mosquitto.conf,

511 allow_anonoymous true
.
.
.
537 password_file /etc/mosquitto/pwfile

  

 

and add it at the end of the file

port 1883
listener 8080
protocol websockets

  

 

7. Start mqtt

# mosquitto -c /etc/mosquitto/mosquitto.conf

  

 

8. Test open two other terminals Terminal 1:  This is actually to subscribe to a message with the topic "mqtt". "mqtt" is the topic name. If a client publishes a message with the topic "mqtt", the terminal will receive the content of the message. 
 
 

mosquitto_sub -t mqtt

  

 

Terminal 2:  Here is a message with the subject "mqtt" and the content "hello mqtt". 

mosquitto_pub -h localhost -t mqtt -m "hello mqtt"

  

 

Then the terminal will receive:hello mqtt

9. Error resolution

During installation, or during testing, you may encounter errors:

mosquitto_sub: error while loading shared libraries: libmosquitto.so.1: cannot open shared object file: No such file or directory

Solution:

# cat /etc/ld.so.conf

# echo "/usr/local/lib">>/etc/ld.so.conf

# ldconfig

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325023007&siteId=291194637