'mosquitto-client obtain refused connection

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.



Solution 1:[1]

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)

Solution 2:[2]

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

Solution 3:[3]

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"

Solution 4:[4]

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 v.1.7 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).

Solution 5:[5]

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

Solution 6:[6]

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

Solution 7:[7]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user2085368
Solution 2 Jadam
Solution 3 FraggaMuffin
Solution 4
Solution 5 Tim Woocker
Solution 6 Good man
Solution 7 Shashank Ks