Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save KazChe/6bcafbaf29e10a7f309d3ca2e2a0f706 to your computer and use it in GitHub Desktop.
Save KazChe/6bcafbaf29e10a7f309d3ca2e2a0f706 to your computer and use it in GitHub Desktop.
(from : https://simplifiedthinking.co.uk/2015/10/03/install-mqtt-server/ )
Installing Brew
The Mosquitto MQTT Server can be easily installed using Homebrew. If it’s not installed on your system already, then a quick visit to the homepage will give you all you need to get going. Homebrew is an OS X Package Manager for installing and updating non-Mac OS X utilities that are more commonly found in other variants of Linux. To install the basic package manager run the following command.
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Installing Mosquitto MQTT
Let’s use our new Homebrew installation to download and install the necessary Mosquitto binaries. This will also download additional libraries required to support secure access via OpenSSL.
brew install mosquitto
The install script finishes by providing the instructions to start the MQTT server on startup.
ln -sfv /usr/local/opt/mosquitto/*.plist ~/Library/LaunchAgents
Finally, to save a restart, the server can be started now by running
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mosquitto.plist
Now you can test the installation and ensure the server is running successfully. Open a new command window and start a listener.
mosquitto_sub -t topic/state
In another window, send a message to the listener.
mosquitto_pub -t topic/state -m "Hello World"
Nicely done.
Installing the Python Libraries
To create the link between Python and MQTT we need to install the Python Eclipse MQTT library. Visit here for the latest downloads and follow the link to download the required version. Specifically, I downloaded these Python Libraries.
Once downloaded, unpack the tar file and install the library
tar xvf org.eclipse.pho.mqtt.python-1.1.tar
cd org.eclipse.pho.mqtt.python-1.1
sudo python setup.py install
And that’s it. We’re ready to start sending and receiving MQTT messages around the home. There are a vast number of additional options that can be set up around the MQTT server – security is an obvious choice, Quality of Service, users, etc. I’m keeping it simple for now.
@Khalian
Copy link

Khalian commented Mar 29, 2017

launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mosquitto.plist
/usr/local/Cellar/mosquitto/1.4.11_2/homebrew.mxcl.mosquitto.plist: Operation not permitted

Please write a working script in case you wish to expose a gist as public.

@christophevg
Copy link

To initially launch mosquito and also have it restarted afterwards (e.g. on reboot) use the Homebrew supplied support:

$ brew services start mosquitto

@lon-io
Copy link

lon-io commented Feb 22, 2018

Thanks @christophevg

@anujdeshpande
Copy link

I had to run brew link mosquitto to get mosquitto_sub and mosquitto_pub to work.
also, in the latest brew you can simply start/stop the broker with brew services start mosquitto. Check the active services with brew services list

@ssaenz11
Copy link

ssaenz11 commented May 4, 2018

How do i change the port to 8083?

@daz
Copy link

daz commented Jun 26, 2018

@ssaenz11 change this line in /usr/local/etc/mosquitto/mosquitto.conf


# Port to use for the default listener.
#port 1883

@ugifractal
Copy link

for someone who got error when running brew link

Linking /usr/local/Cellar/mosquitto/1.5.4_1... 
Error: Could not symlink sbin/mosquitto
/usr/local/sbin is not writable.

try this:

  sudo mkdir -p /usr/local/sbin
  sudo chown -R $(whoami) /usr/local/sbin

@TheDanielz3
Copy link

nice tutorial i love it and solve my problems on my mac 🗡️

@mkormendy
Copy link

mkormendy commented Jan 20, 2019

I opted to have mine run on startup without a user logging in. To do so, I did:

sudo cp /usr/local/opt/mosquitto/*.plist /Library/LaunchDaemons
sudo chown root:wheel /Library/LaunchDaemons/homebrew.mxcl.mosquitto.plist

Then change the user in the mosquitto config file:

nano /usr/local/etc/mosquitto/mosquitto.conf

(look for line that has #user mosquitto, uncomment it and change mosquitto to the username that installed mosquitto with homebrew.

@Stokestack
Copy link

Stokestack commented Feb 22, 2021

So far this does not work on my system. The broker is running on my Mac, and I can subscribe from one terminal window and publish to it from another if I use "localhost" as the hostname. But if I use the real hostname as assigned by the OS or the IP address assigned by the router, I get "connection refused" from the second terminal window. I also get that error from any other machine, athough it's on the local network. However, from that other machine I can ping the broker Mac using its OS-given hostname. But publish attempts get "connection refused."

I even tried running Mosquitto as root. No change.

@patryck100
Copy link

patryck100 commented May 14, 2021

To initially launch mosquito and also have it restarted afterwards (e.g. on reboot) use the Homebrew supplied support:

$ brew services start mosquitto

You can also stop the mosquitto when running through brew by:
$ brew services stop mosquitto

@9bany
Copy link

9bany commented Feb 19, 2022

For someone who got error:

Error: Failure while executing; `cp -pR /private/tmp/d20220220-41397-yjfjvo/ca-certificates/. /usr/local/Cellar/ca-certificates` exited with 1. Here's the output:

Try this:

$ sudo chown -R $(whoami) /usr/local/Cellar

It's work for me.

@polyphonic13
Copy link

However, from that other machine I can ping the broker Mac using its OS-given hostname. But publish attempts get "connection refused."

@Stokestack did you ever get this working? MacOS has a firewall turned on by default which refuses connections even if they are on your local network. I've dealt with the same issue when attempting to connect a Raspberry Pi to a TCP server on my Mac. This might be the cause of the issue you'd encountered.

@Stokestack
Copy link

@polyphonic13 It's been a while, so I can't remember! I think I think I did, though. I also was using a Pi as the other node, and I don't think I gave up and ran the broker on the Pi.

Thanks a lot for the note!

@MetaverseMan
Copy link

MetaverseMan commented Oct 6, 2022

my problem is it's not running!
hanpeng@hanpeng ~ % sudo brew services stop mosquitto
Stopping mosquitto... (might take a while)
==> Successfully stopped mosquitto (label: homebrew.mxcl.mosquitto)
hanpeng@hanpeng ~ % sudo brew services start mosquitto
==> Successfully started mosquitto (label: homebrew.mxcl.mosquitto)
hanpeng@hanpeng ~ % sudo brew services info mosquitto
mosquitto (homebrew.mxcl.mosquitto)
Running: ✘
Loaded: ✔
Schedulable: ✘

why it's just connot running. and the version of mosquitto is as below

hanpeng@hanpeng ~ % brew upgrade mosquitto
Warning: mosquitto 2.0.14 already installed

@Huzaifa1025
Copy link

how to do logging of mosquitto connect and disconnect messages on Mac

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment