20

I want to use MQTT protocol using mosquitto library.

First of all, I want to do some test installing mosquitto-clients

 sudo apt-get install mosquitto-clients

This program provides two "method":

  • mosquitto_pub
  • mosquitto_sub

Following this instructions I'm trying to submit new topic:

mosquitto_sub -d -t newtopic/test

using default host/port [localhost/1883].

I obtain:

Error: Connection refused

Is too generic as error.. can anyone help me?
Could be is a firewall problem? In this case, how can I check if is this the problem?

I'm using linux ubuntu ( 3.8.0-42-generic #62~precise1-Ubuntu)

nb same behaviour writing custom program using libmosquitto.

Luca Davanzo
  • 21,000
  • 15
  • 120
  • 146

8 Answers8

23

Just edit Mosquitto configuration file ( /etc/mosquitto/conf.d/mosquitto.conf ) adding these lines...
allow_anonymous true
listener 1883 0.0.0.0

... and restart Mosquitto (as service or not).
$ sudo service mosquitto restart
or
$ mosquitto --verbose --config-file /etc/mosquitto/conf.d/mosquitto.conf

As informed here, since v1.7 (2022) allow_anonymous defaulted to false. It is also useful to check log messages ( /var/log/mosquitto/mosquitto.log ).

Finally, run Mosquitto subscriber/publisher using --host (-h) parameter and the host IP address (get if from ifconfig or ip -color addr command).

T77
  • 340
  • 2
  • 6
16

The default host:port combination for mosquitto_pub/sub is localhost:1883. If you do not have a broker running on your local computer then it will not be able to connect of course.

The solution is to either run the broker on your local computer, or to tell the utilities where to connect. For example:

mosquitto_sub -t newtopic/test -h test.mosquitto.org
ralight
  • 11,033
  • 3
  • 49
  • 59
13

None of the other answers worked for me. In my case, I had upgraded from mosquitto 1.X to mosquitto 2.0, which requires a new configuration to be added to your mosquitto.conf:

listener 1883

For clients other than localhost to connect (ie, via Docker)

user2085368
  • 695
  • 6
  • 11
  • 2
    For me the "right answer" is somewhat distributed over several posts here. I had to modify the configuration file located at `/etc/mosquitto/mosquitto.conf` and added two lines: `listener 1883` (which was not enough) plus `allow_anonymous true`. And don't forget to restart the service! – Matthias W. Apr 15 '22 at 10:55
  • all the clues I needed to get this working were in the mosquitto logs (in my case, easily viewable in docker). The logs clearly say that connections are only available locally, so go edit your mosquitto.conf file. A great example of useful logging – Greg Woods May 22 '22 at 19:48
9

For future googlers:

You can use a public host as mentioned above, but to start a local mosquitto broker, first make sure you have installed mosquitto in addition to mosquitto_sub. You can then start up the mosquitto broker by simply running the following:

mosquitto
Jadam
  • 1,650
  • 1
  • 19
  • 40
9

I experienced the same issue, for me it was in upgrading mosquitto for mqtt v5 support:

$ mosquitto --version
mosquitto version 2.0.14
mosquitto is an MQTT v5.0/v3.1.1/v3.1 broker.

However, the upgraded broker no longer supported anonymous connections:

$ mosquitto_pub -t mytopic -m "Hello World"
Connection error: Connection Refused: not authorised.
Error: The connection was refused.

This is probably a better default, but less friendly when experimenting. To configure the broker to allow anonymous connections:

$ cat /etc/mosquitto/conf.d/standard.conf
listener 1883
protocol mqtt
allow_anonymous true

$ sudo systemctl restart mosquitto.service

Then, hey presto :

$ mosquitto_pub -t mytopic -m "Hello World"
FraggaMuffin
  • 3,915
  • 3
  • 22
  • 26
  • 1
    I was moving a config over form ubuntu 18.04 to 22.04 and missed that mosquitto versions updated. thanks! – jshep321 Jun 12 '22 at 17:24
2

This is happening because you have installed only mosquitto clients on your system and not installed mosquitto on your system. please execute below command to install the MQTT Broker.

sudo apt-get install mosquitto

source: connection attempt failed bytesofgigabytes.com

Tim Woocker
  • 1,883
  • 1
  • 16
  • 29
1

Be sure Your mosquitto service installed and correctly running.

for install : sudo apt-get install mosquitto

after install : sudo service mosquitto stop , sudo service mosquitto start

Good man
  • 396
  • 4
  • 12
1

I had setup a username and password for my broker. Therefore had to use that while using mosquitto sub:

mosquitto_sub -u username -P password -t newtopic/test -h test.mosquitto.org
Shashank Ks
  • 450
  • 5
  • 9
  • How did you setup username/password for the broker? – SkyDev Jan 10 '22 at 20:05
  • http://www.steves-internet-guide.com/mqtt-username-password-example/#:~:text=You%20will%20need%20to%20copy,to%20set%20the%20password_file%20path. You van follow this tutorial – Shashank Ks Jan 15 '22 at 18:08