jpmens / mosquitto-auth-plug

Authentication plugin for Mosquitto with multiple back-ends (MySQL, Redis, CDB, SQLite3)
Other
825 stars 496 forks source link

Refused connection #173

Closed FernandoGarcia closed 8 years ago

FernandoGarcia commented 8 years ago

Hi!

Firstly, thanks for this plugin!

I would like to know the reason to a refused connection but I can't find an error log for it.

I'm trying to publish and subscribe using the terminal and the result is like this:

root@MQTT-Server:~# mosquitto_sub -d -u fernandogarcia -P 123456 -t "topic/command" Error: Connection refused

root@MQTT-Server:~# mosquitto_pub -d -u fernandogarcia -P 123456 -t "topic/command" -m "hello" Error: Connection refused

My mosquitto.conf is like this:

auth_plugin

auth_plugin /etc/mosquitto/auth-plug.so

auth_opt_backends mysql

auth_opt_redis_host localhost

auth_opt_redis_port 12885

auth_opt_host localhost

auth_opt_port 3306

auth_opt_dbname myDB auth_opt_user mqtt_user

auth_opt_pass mypass auth_opt_userquery SELECT pw FROM mqtt_users WHERE username = '%s'

auth_opt_superquery SELECT COUNT(*) FROM mqtt_users WHERE username = '%s' AND super = 1

auth_opt_aclquery SELECT topic FROM mqtt_acls WHERE (username = '%s') AND (rw >= %d)

auth_opt_anonusername AnonymouS

Running this command:

/usr/local/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf

The result is:

1478615526: mosquitto version 1.4.10 (build date 2016-11-06 20:16:49+0000) starting 1478615526: Config loaded from /etc/mosquitto/mosquitto.conf. 1478615526: |-- * auth-plug: startup 1478615526: |-- Configured order: mysql

1478615526: |-- }}}} MYSQL 1478615526: Opening ipv4 listen socket on port 1883. 1478615526: Opening ipv6 listen socket on port 1883.

Using verbose mode (mosquitto -v) in a terminal I have this:

root@MQTT-Server:~# mosquitto_pub -d -u fernandogarcia -P 123456 -t "topic/command" -m "hello" Client mosqpub/9003-MQTT-Serve sending CONNECT Client mosqpub/9003-MQTT-Serve received CONNACK Client mosqpub/9003-MQTT-Serve sending PUBLISH (d0, q0, r0, m1, 'topic/command', ... (5 bytes)) Client mosqpub/9003-MQTT-Serve sending DISCONNECT

In another:

1478615833: mosquitto version 1.4.10 (build date 2016-11-06 20:16:49+0000) starting 1478615833: Using default config. 1478615833: Opening ipv4 listen socket on port 1883. 1478615833: Opening ipv6 listen socket on port 1883. 1478615842: New connection from 127.0.0.1 on port 1883. 1478615842: New client connected from 127.0.0.1 as mosqpub/9003-MQTT-Serve (c1, k60, u'fernandogarcia'). 1478615842: Sending CONNACK to mosqpub/9003-MQTT-Serve (0, 0) 1478615842: Received PUBLISH from mosqpub/9003-MQTT-Serve (d0, q0, r0, m0, 'topic/command', ... (5 bytes)) 1478615842: Received DISCONNECT from mosqpub/9003-MQTT-Serve 1478615842: Client mosqpub/9003-MQTT-Serve disconnected.

Can someone give me a suggestion about where to look to find the error? How can I enable the error log?

Thanks in advance.

jpmens commented 8 years ago

Connection refused means the service you're trying to access (localhost/1883) isn't listening (not started) or is being actively blocked by a firewall. I think you're experiments are in the wrong order: judging by your logs, Mosquitto is running so I think your publish occurred during a moment when Mosquitto wasn't (yet) running.

FernandoGarcia commented 8 years ago

Thanks for fast reply!

I did:

sudo ufw allow 1883

and a reboot I have checked the firewall and it's opened.

After the result is equal.

Typing "mosquitto" on terminal I can see it working, there

1478619266: mosquitto version 1.4.10 (build date 2016-11-06 20:16:49+0000) starting 1478619266: Using default config. 1478619266: Opening ipv4 listen socket on port 1883. 1478619266: Opening ipv6 listen socket on port 1883. 1478619268: New connection from 189.40.46.60 on port 1883. 1478619268: New client connected from 189.40.x.x as M-001 (c1, k15, u'fernandogarcia').

This client (M-001) connected is an ESP12 that I have running a test code here.

Publishing something on terminal

1478619422: New connection from 127.0.0.1 on port 1883. 1478619422: New client connected from 127.0.0.1 as mosqpub/1983-MQTT-Serve (c1, k60, u'fernandogarcia'). 1478619422: Client mosqpub/1983-MQTT-Serve disconnected.

So it's working but after type ctrl +c on terminal it doesn't works anymore.

Therefore the Mosquitto is not running continuously I think, how I can solve it?

jpmens commented 8 years ago

By hitting CTRL-C you are killing your Mosquitto instance. I assume you want to have it started at boot. (Look for something like service start mosquitto or systemctrl enable mosquitto -- this depends on your distribution.

On Tue, 8 Nov 2016 at 16:43, Fernando Garcia notifications@github.com wrote:

Thanks for fast reply!

I did:

sudo ufw allow 1883

and a reboot I have checked the firewall and it's opened.

After the result is equal.

Typing "mosquitto" on terminal I can see it working, there

1478619266: mosquitto version 1.4.10 (build date 2016-11-06 20:16:49+0000) starting 1478619266: Using default config. 1478619266: Opening ipv4 listen socket on port 1883. 1478619266: Opening ipv6 listen socket on port 1883. 1478619268: New connection from 189.40.46.60 on port 1883. 1478619268: New client connected from 189.40.x.x as M-001 (c1, k15, u'fernandogarcia').

This client (M-001) connected is an ESP12 that I have running a test code here.

Publishing something on terminal

1478619422: New connection from 127.0.0.1 on port 1883. 1478619422: New client connected from 127.0.0.1 as mosqpub/1983-MQTT-Serve (c1, k60, u'fernandogarcia'). 1478619422: Client mosqpub/1983-MQTT-Serve disconnected.

So it's working but after type ctrl +c on terminal it doesn't works anymore.

Therefore the Mosquitto is not running continuously I think, how I can solve it?

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/jpmens/mosquitto-auth-plug/issues/173#issuecomment-259171625, or mute the thread https://github.com/notifications/unsubscribe-auth/AADtIjiWNN79H9-pR7QHE1FKz7p41n72ks5q8JiUgaJpZM4Kshvu .

FernandoGarcia commented 8 years ago

I'm using Ubuntu.

I think that Mosquitto service doesn't exist here.

The result are:

root@MQTT-Server:~# sudo service mosquitto start Failed to start mosquitto.service: Unit mosquitto.service not found.

root@MQTT-Server:~# sudo /etc/init.d/mosquitto start sudo: /etc/init.d/mosquitto: command not found

So I have created a cron tab according to this tutorial like this:

/etc/systemd/system/mosquitto.service

[Unit] Description=Mosquitto MQTT Broker daemon ConditionPathExists=/etc/mosquitto/mosquitto.conf After=network.target Requires=network.target

[Service]

Type=forking RemainAfterExit=no StartLimitInterval=0 PIDFile=/var/run/mosquitto.pid ExecStart=/usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf -d ExecReload=/bin/kill -HUP $MAINPID Restart=on-failure RestartSec=2

[Install]

WantedBy=multi-user.target

Then:

root@MQTT-Server:~# systemctl daemon-reload

The results are:

root@MQTT-Server:~# sudo service mosquitto start Job for mosquitto.service failed because the control process exited with error code. See "systemctl status mosquitto.service" and "journalctl -xe" for details.

root@MQTT-Server:~# systemctl status mosquitto.service mosquitto.service - Mosquitto MQTT Broker daemon Loaded: loaded (/etc/systemd/system/mosquitto.service; enabled; vendor preset: enabled) Active: activating (auto-restart) (Result: exit-code) since Tue 2016-11-08 19:35:21 UTC; 1s ago Process: 2429 ExecStart=/usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf -d (code=exited, status=203/EXEC)

Nov 08 19:35:21 MQTT-Server systemd[1]: mosquitto.service: Control process exited, code=exited status=203 Nov 08 19:35:21 MQTT-Server systemd[1]: Failed to start Mosquitto MQTT Broker daemon. Nov 08 19:35:21 MQTT-Server systemd[1]: mosquitto.service: Unit entered failed state. Nov 08 19:35:21 MQTT-Server systemd[1]: mosquitto.service: Failed with result 'exit-code'.

So no luck, any suggestion?

FernandoGarcia commented 8 years ago

Hi!

This commands should solved this problem.

sudo update-rc.d mosquitto remove sudo git clone https://github.com/Dan-in-CA/mosquitto_unit_file.git sudo cp mosquitto_unit_file/mosquitto.service /etc/systemd/system/mosquitto.service sudo systemctl enable mosquitto.service sudo reboot

check if mosquitto is running

sudo mosquitto -v

Thanks for your time.

Best regards.

FernandoGarcia commented 8 years ago

I had errors with this script.

This one worked fine:

[Unit]
Description=Mosquitto MQTT Broker
Documentation=man:mosquitto(8)
Documentation=man:mosquitto.conf(5)
ConditionPathExists=/etc/mosquitto/mosquitto.conf
After=xdk-daemon.service

[Service]
ExecStart=/usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
ExecReload=/bin/kill -HUP $MAINPID
User=mosquitto
Restart=on-failure
RestartSec=10

[Install]
WantedBy=multi-user.target