I have been happily running MongoDB for years on my mac (currently sonoma 14.6.1), installing via brew, using
$ brew install mongodb-community
(and variants) in the terminal and then starting it using
$ brew services start mongodb/brew/mongodb-community
and getting a nice message:
Successfully started
mongodb-community
(label: homebrew.mxcl.mongodb-community)
Worked for years, through various upgrades. However, I am running MongoDB as part of a Node application which I have been updating, and this forced me to upgrade my MongoDB setup. And somehow, now, despite the warm "successfully started" message it has NOT started MongoDB. MongoBD should be running on port 27017. It is not:
$ lsof -i tcp:27017
returns nothing. Attempting to connect to MongoDB through Robo 3T, or running mongosh in the terminal, gives some form of this message:
MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017
(from mongosh, in the terminal). Investigating further:
$ brew services list
returns this:
Name Status User File
mongodb-community error 4 peter ~/Library/LaunchAgents/homebrew.mxcl.mongodb-community.plist
So I go to look at the plist:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>homebrew.mxcl.mongodb-community</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/opt/mongodb-community/bin/mongod</string>
<string>--config</string>
<string>/usr/local/etc/mongod.conf</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<false/>
<key>WorkingDirectory</key>
<string>/usr/local</string>
<key>StandardErrorPath</key>
<string>/usr/local/var/log/mongodb/output.log</string>
<key>StandardOutPath</key>
<string>/usr/local/var/log/mongodb/output.log</string>
<key>HardResourceLimits</key>
<dict>
<key>NumberOfFiles</key>
<integer>64000</integer>
</dict>
<key>SoftResourceLimits</key>
<dict>
<key>NumberOfFiles</key>
<integer>64000</integer>
</dict>
</dict>
</plist>
It appears the problem is the line
/usr/local/opt/mongodb-community/bin/mongod
Yes, there is a mongod in that directory. Trying to run it from the terminal gives this:
$ mongod
Illegal instruction: 4
I see that in MongoDB documentation at https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-os-x/ there is a note: "macOS Prevents mongod From Opening". I am guessing that is what is happening here. Unfortunately the documentation’s advice on fixing this is outdated, and other attempts to remedy this through changing permissions also fail. Any suggestions most welcome.
Update: August 26, after repeated cycles of install, uninstall of various versions of brew install mongodb-community, I have progressed to the point that running brew services start mongodb/brew/mongodb-community now does NOT say "successfully started". Instead I get this
error:Bootstrap failed: 5: Input/output error
Try re-running the command as root for richer errors.
Error: Failure while executing;/bin/launchctl bootstrap gui/503 /Users/pmr906_1/Library/LaunchAgents/homebrew.mxcl.mongodb-community.plist
exited with 5.
Now moving the question over the DBA Stack Exchange.
Update: after doing a complete removal of mongoDB and reinstalling outside homebrew, and then running
mongod –config /usr/local/etc/mongod.conf
I finally got this output in the log file:
"This version of MongoDB is too recent to start up on the existing data files. Try MongoDB 4.2 or earlier."
At last! So I have to go back and upgrade my ancient database through multiple MongoDB releases. Oh well. I don’t understand why none of the logs from the homebrew installations did not give this feedback, but anyway..
2
Answers
After many hours of investigation -- I located the problem. It was to do with the database itself, not MongoDB. I don't understand quite how this happened. The database was running perfectly fine with mongodb 4.4 until a few days ago. Then somehow, it began to fail to load, with the error (sometimes)
This led me to all kinds of investigations, none of which got me anywhere. I tried out different mongoDB configurations, I also went and got different versions of the database data and tried with that. None of it worked.
Finally: what DID work. I emptied out the database data folder. And lo! MongoDB started! I happened to have an old version of the database in a mongodump. So I restored that and, lo, I had my database back again!
There are still many mysterious things here. This worked fine with 4.4. However, when I first tried to run mongodb-community, which sets up 7.0, mongodb again failed when I followed the same sequence that worked for 4.4 (that is: removing the database files and running the community version). Weird. So then I went back and ran [email protected], [email protected], [email protected] and mongodb-community in sequence -- and it worked in 7.0!
So the lesson -- if you are finding MongoDB is not running on your homebrew installation, the problem may be somehow in your database. Emptying out the database and then restoring it from an earlier dump file might be what you need.
I was getting a similar error in MacOS Sequoia:
ChatGPT actually helped me out with some suggested commands:
Manually remove the service:
Add the service again:
It should all be on one line and replace [username] with your username.
After that I was able to
brew service start mongodb-community
again.Note that you can manually run
/usr/local/Cellar/mongodb-community/8.0.1/bin/mongod --dbpath=/usr/local/var/mongodb
to troubleshoot errors.