在 Mac OS X 上有什么干净利落的方法可以阻止上帝?

我正在运行 mongo1.8.2,试图看看如何在 Mac 上干净利落地关闭它。

在我们的 ubuntu 服务器上,我可以使用以下命令从 mongo shell 干净利落地关闭 mongo:

> use admin
> db.shutdownServer()

但在我的 Mac 上,它并没有终止蒙古进程。输出显示它“应该”关闭,但是当 i ps-ef | grep mongo 时,它显示了一个活动进程。而且,我仍然可以打开一个 mongo shell 并查询我的 dbs,就像它从来没有关闭过一样。

我的 db.shutdownServer ()本地的输出是:

MongoDB shell version: 1.8.2
connecting to: test
> use admin
switched to db admin
> db.shutdownServer()
Tue Dec 13 11:44:21 DBClientCursor::init call() failed
Tue Dec 13 11:44:21 query failed : admin.$cmd { shutdown: 1.0 } to: 127.0.0.1
server should be down...
Tue Dec 13 11:44:21 trying reconnect to 127.0.0.1
Tue Dec 13 11:44:21 reconnect 127.0.0.1 failed couldn't connect to server 127.0.0.1
Tue Dec 13 11:44:21 Error: error doing query: unknown shell/collection.js:150

我知道我可以取消这个过程,但我想干净利落点。

135202 次浏览

Check out these docs:

http://www.mongodb.org/display/DOCS/Starting+and+Stopping+Mongo#StartingandStoppingMongo-SendingaUnixINTorTERMsignal

If you started it in a terminal you should be ok with a ctrl + 'c' -- this will do a clean shutdown.

However, if you are using launchctl there are specific instructions for that which will vary depending on how it was installed.

If you are using Homebrew it would be launchctl stop homebrew.mxcl.mongodb

It's probably because launchctl is managing your mongod instance. If you want to start and shutdown mongod instance, unload that first:

launchctl unload -w ~/Library/LaunchAgents/org.mongodb.mongod.plist

Then start mongod manually:

mongod -f path/to/mongod.conf --fork

You can find your mongod.conf location from ~/Library/LaunchAgents/org.mongodb.mongod.plist.

After that, db.shutdownServer() would work just fine.

Added Feb 22 2014:

If you have mongodb installed via homebrew, homebrew actually has a handy brew services command. To show current running services:

brew services list

To start mongodb:

brew services start mongodb-community

To stop mongodb if it's already running:

brew services stop mongodb-community

Update*

As edufinn pointed out in the comment, brew services is now available as user-defined command and can be installed with following command: brew tap gapple/services.

I prefer to stop the MongoDB server using the port command itself.

sudo port unload mongodb

And to start it again.

sudo port load mongodb

If you installed mongodb with homebrew, there's an easier way:

List mongo job with launchctl:

launchctl list | grep mongo

Stop mongo job:

launchctl stop <job label>

(For me this is launchctl stop homebrew.mxcl.mongodb)

Start mongo job:

launchctl start <job label>

This is an old question, but its one I found while searching as well.

If you installed with brew then the solution would actually be the this:

launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist

Simple way is to get the process id of mongodb and kill it. Please note DO NOT USE kill -9 pid for this as it may cause damage to the database.

1. get the pid of mongodb

$ pgrep mongo

you will get pid of mongo, Now

$ kill

You may use kill -15 as well

If you have installed mongodb community server via homebrew, then you can do:

brew services list

This will list the current services as below:

Name              Status  User          Plist
mongodb-community started <your_user_name> /Users/<your_user_name>/Library/LaunchAgents/homebrew.mxcl.mongodb-community.plist


redis             stopped

Then you can restart mongodb by first stopping and restart:

brew services stop mongodb
brew services start mongodb

If the service is running via brew, you can stop it using the following command:

brew services stop mongodb

The solutions provided by others used to work for me but is not working for me anymore, which is as below.

brew services stop mongodb
brew services start mongodb

brew services list gives

Name              Status  User      Plist
mongodb-community started XXXXXXXXX /Users/XXXXXXXXX/Library/LaunchAgents/homebrew.mxcl.mongodb-community.plist


So I used mongodb-community instead of mongodb which worked for me

brew services stop mongodb-community
brew services start mongodb-community