August 9, 2019 · mongodb gist

Fixing My Stupid Broken MongoDB Install in OSX

I'm writing this gist of an article because I'm almost certainly going to have this problem again.

Today I upgraded my local MongoDB installation using Homebrew from v3.2 straight to v4.0. Spoiler alert, it turns out that if you have database data under an old version (like I did) this ain't gonna work. The data itself also needs to go through the upgrade process and you can't upgrade from 3.2 directly to 4.0. You need to make smaller moves, first to 3.4, then to 3.6 then to 4.0. It's the Mongo way. In my case however, I didn't care about the data, but let's take a step back... I'm getting ahead of myself.

It all starts with:

$ brew uninstall mongodb@3.2
...
$ brew install mongodb

At this point, the MongoDB server can be started with the mongod command but I wanted it to fire up on startup and the installation kindly provides a launch agent for that. So I linked that .plist into my user's launch agents directory like so:

$ ln -sfv /usr/local/opt/mongodb/*.plist ~/Library/LaunchAgents

Then I restarted my machine. In an perfect world this would be everything. The launch agent should load on system start and mongod should be running. I was surprised, however, to get the following error when running mongo:

$ mongo
MongoDB shell version v4.0.3
connecting to: mongodb://127.0.0.1:27017
2019-08-09T15:26:36.579-0800 E QUERY    [js] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :
connect@src/mongo/shell/mongo.js:257:13
@(connect):1:6
exception: connect failed

The obvious cause here is that the mongo server is not running (see: ps aux | grep mongo). Hmm, maybe the launch agent didn't start properly? Let's try loading it by hand:

$ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist
/Users/carterbancroft/Library/LaunchAgents/homebrew.mxcl.mongodb.plist: service already loaded

Odd, it's already loaded.

What does homebrew.mxcl.mongodb.plist look like?

$ cat ~/Library/LaunchAgents/homebrew.mxcl.mongodb.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</string>
  <key>ProgramArguments</key>
  <array>
    <string>/usr/local/opt/mongodb/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>4096</integer>
  </dict>
  <key>SoftResourceLimits</key>
  <dict>
    <key>NumberOfFiles</key>
    <integer>4096</integer>
  </dict>
</dict>
</plist>

Seems legit.

So, then I try firing up the server manually via mongod and running mongo from a new tab. It connects just fine, wtf!?

I'm still actually confused why running mongod manually worked. Can someone explain?

Now I'm Googling. I come across this issue and after scrolling for a good long while there's finally the suggestion to check the logs and, low and behold, that's where the guy's problem lies. Perhaps mine lies there as well, let's find out...

$ cat /usr/local/var/log/mongodb/mongo.log
...
** IMPORTANT: UPGRADE PROBLEM: The data files need to be fully upgraded to version 3.6 before attempting an upgrade to 4.0; see http://dochub.mongodb.org/core/4.0-upgrade-fcv for more details.
...

Bingo. My data is stuck in the olden days of 3.2 and I'm trying to run 4.0. Turns out, I don't give a shit about my local data. For me the solution becomes clear. Nuke MongoDB from orbit and reinstall from scratch.

If you DO care about your data what follows is not for you. You'll need to read the docs in that screaming error and follow the upgrade process step by step.

That's where this short article saved me. Uninstall MongoDB (MACOS) completely. You, Rajan, are a saint. A more distilled version of what I specifically needed is as follows:

$ brew uninstall mongodb
...
$ rm -rf /usr/local/var/mongodb
...
$ brew install mongodb
...
$ ln -sfv /usr/local/opt/mongodb/*.plist ~/Library/LaunchAgents

The real key there is the rm -rf /usr/local/var/mongodb which deletes that old timey 3.2 data. I restarted my machine, ran mongo and... Success!

Now, if you've got old MongoDB .plist files in your LaunchAgents directory this would be the time to clean those up. As noted in Rajan's article, you might also have data stored in /data/db which you may or may not want to delete as well. Basically follow his instructions to the T if you want to be thorough in your cleanup.

Enjoy!

Comments powered by Disqus