MongoDB via Brew Services "undefined method `plist_startup'"

2.3k Views Asked by At

When I run MongoDB, I usually start it manually (i.e. it's not part of my login startup items) and I'm good about stopping the service before I shutdown.

I recently restarted my laptop and received an error upon running:

brew services run mongodb/brew/mongodb-community

The error message reads:

Error: undefined method `plist_startup' for #<Formula mongodb-community (stable) /usr/local/Homebrew/Library/Taps/mongodb/homebrew-brew/Formula/mongodb-community.rb>

I'm not entirely sure what happened. I haven't installed any major updates or made any modifications to my environment.

What I've Tried

I completely uninstalled MongoDB before installing it again:

# Uninstall each component of mongodb-community...
brew uninstall mongodb/brew/mongodb-community
brew uninstall mongodb/brew/mongodb-database-tools
brew uninstall mongosh

# Reinstall all of the above...
brew install mongodb/brew/mongodb-community

Current Install

mongod --version

db version v7.0.2
Build Info: {
    "version": "7.0.2",
    "gitVersion": "02b3c655e1302209ef046da6ba3ef6749dd0b62a",
    "modules": [],
    "allocator": "system",
    "environment": {
        "distarch": "x86_64",
        "target_arch": "x86_64"
    }
}

Could be Homebrew...?

I'll be honest, sometimes on these late nights, I'm on autopilot. I may or may not have run a brew upgrade at some point. I'll look into whether or not this happened. In the meantime, when I get the log for MongoDB in Homebrew, I don't even see a commit that would have impacted me:

brew log mongodb/brew/mongodb-community

# Yields...

commit f33a59b6642f6a9f47f84b390dd71c386998cce6
Author: Zack Winter <[email protected]>
Date:   Wed Oct 4 23:24:26 2023 +0000

    Update paths with mr script

commit 7f3db6dbe9231300cc61645c68686a970b575f1f
Author: Zack Winter <[email protected]>
Date:   Tue Oct 3 20:00:06 2023 +0000

    SERVER-80537 update formulas for 7.0.1 release

commit 5055c34131148681885ea2241abccfc603596295
Author: Alexander Neben <[email protected]>
Date:   Fri Aug 18 10:54:02 2023 -0700
6

There are 6 best solutions below

1
dmoody256 On BEST ANSWER

Recently Homebrew updated to 4.2, which changed the way it starts services. It required the mongodb formula to explicitly define a service block, even if just points to the service file.

The fix was merged in https://github.com/mongodb/homebrew-brew/commit/2d0bfe19214d1f5071decd238b29306a6e82fff2

3
Sparrow On

Start Script Changed

FAIL...see [UPDATE] at the bottom

I'm not sure how or why, but it seems like the startup script had to change. Here are the steps I took to get MongoDB back up and running again, though I would recommend just skipping down to trying an alternative to starting MongoDB, first.

Reinstall MongoDB

I first tried forcing a reinstall of MongoDB, via Homebrew. My efforts, of course, were to no avail:

brew reinstall mongodb/brew/mongodb-community

Uninstalled Homebrew

This was annoying because it basically disconnects itself from anything it ever installed. If you want to blast through updates as per usual, you'll have to reinstall everything again. It's easy enough to make a list and run a batch install:

brew leaves

But, be careful: that doesn't include all your casks. I forgot to make my list and check it twice this holiday season. If you want to have a nicely wrapped Homebrew, be sure to run this as well:

brew list --cask   #add the "-1" to the end if you want a vertical list.

Reinstalled An Old Version of Homebrew

I went back and found the installer package for Homebrew-4.1.23.pkg and just installed via the GUI when I couldn't figure out how to install a previous version from a script.

Unfortunately, before I ran any kind of brew install ... commands, I forgot to set this variable:

export HOMEBREW_NO_AUTO_UPDATE=1;

....soooooo, ya. Once I ran any brew command it upgraded me right back to where I started thinking there was an issue. Aren't you glad you skipped to the end instead of following these steps?

Alternate Method

FWIW - When it came time to reinstall MongoDB, I followed a different path than I'm used to; different than what I have scripted for system setups. I used the method described in the MongoDB docs:

brew install [email protected]

Change MongoDB Startup Scripts

In my profile script(s) I have a few functions for managing things like the start/stop of MongoDB. For example, my script to run MongoDB looks like this:

function mongo-run {
    pid=$(ps -A | grep '[mon]god' | awk '{printf "%d", $1}');
    if [[ -z $pid ]];then
        brew services run mongodb/brew/mongodb-community;
    else
        printf "
        ${txtblu}Mongo is already running with process ID ${bldblu}$pid${txtrst}\n";
    fi
}

...and all I did was change to this:

function mongo-run {
    pid=$(ps -A | grep '[mon]god' | awk '{printf "%d", $1}');
    if [[ -z $pid ]];then
        brew services run mongodb-community;
    else
        printf "
        ${txtblu}Mongo is already running with process ID ${bldblu}$pid${txtrst}\n";
    fi
}

In case you missed it:


# This...
brew services run mongodb/brew/mongodb-community;

# changed to this...
brew services run mongodb-community;

Pretty ridiculous, right?

Oh well. It's all working again, for now.

UPDATE

I don't know what I did, exactly, but I am running into that original error message again.

Oddly enough, after I ran the Homebrew command(s) to remove and cleanup the MongoDB-related stuff, I ran the brew services run mongodb-community command and got the SAME error message. ...as if the .rb script wasn't removed after I ran the uninstall and cleanup commands.

I eventually got it completely reinstalled again (MongoDB, that is - not Homebrew) and am still seeing the error.

  • I also made sure there were no other Ruby installs on my Mac, just the default v2.6.
1
Pamela On

I recently had this issue and went through the whole list above. This is what worked for me and my mac. I went to Finder and found MongoDB. When I clicked on the application, it states that my computer thought it was malicious. I had to right click and manually allow it to open. Then when I went back to Compass, I was able to connect.

3
mnsdn On

this dirty hack worked for me:

edit this file:

/opt/homebrew/Library/Taps/homebrew/homebrew-services/lib/service/formula_wrapper.rb

change the line:

@service_startup ||= formula.plist_startup.present?

to

@service_startup ||= formula.plist_name.present?

1
V.Villacis On

On a mac m1 a fix I did was to add the command to start up the database manually. With the following command mongod --config /opt/homebrew/etc/mongod.conf

To run automatically

Step 1: Find where your mongod.conf is, it should be in the etc folder, but depends on which chip you have. So do brew --prefix to check. I am in an m1, so mine is in the aforementioned, but yours may say /usr/local in that case check /usr/local/etc for it.

Step 2. Open up your .zshrc file. This should be in your home directory. If you are not on a zsh then check for a .bashrc file.

Step 3. At the end of your rc file add. mongod --config /opt/homebrew/etc/mongod.conf & this will run the command to start the database manually every time you open up a terminal. The & at the end is to make sure it runs in the background.

This seems to work, it can get annoying as if you want to use compass you need to open up a terminal to start the database. However, I see this as a workaround in the meantime until the devs push an update.

0
Sparrow On

Another Option

For now, this has been proving useful. I wanted to remove root as the executing user for the service. I'm not sure how useful it is to anyone else, but (to use the worst phrase any programmer could utter) "it works on my machine:"

# Be sure to insert your user name in the next line...
sudo brew services run mongodb-community --sudo-service-user <your.user.name>
brew services ls
sudo brew services stop mongodb-community
brew services start mongodb-community

Breakdown

After running the first command, I saw that the service had my user name under it, even though it was not running. In previous attempts, this would always say root:

brew services ls
# Yields...
Name              Status  User       File
docker-machine    none               
mongodb-community started <your.user.name>

Out of superstition I ran the third line, in the hopes that something needing cleaned up got cleaned up.

When I ran the last command, to start the service, and rebooted my machine, everything worked fine. I've had a couple reboots today and so far so good.

brew services ls
# After rebooting this yields...
Name              Status  User       File
docker-machine    none               
mongodb-community started <your.user.name> ~/Library/LaunchAgents/homebrew.mxcl.mongodb-community.plist

¯\(ツ)