在 Mac OS X 上停止 mongod 的干净方法是什么?

发布于 2024-12-20 20:17:16 字数 888 浏览 1 评论 0原文

我正在运行 mongo 1.8.2 并试图了解如何在 Mac 上彻底关闭它。

在我们的 ubuntu 服务器上,我可以通过 mongo shell 干净地关闭 mongo:

> use admin
> db.shutdownServer()

但在我的 Mac 上,它不会杀死 mongod 进程。输出显示它“应该”关闭,但是当我 ps -ef | grep mongo 它向我展示了一个活跃的进程。另外,我仍然可以打开 mongo shell 并查询我的数据库,就像它从未关闭一样。

我的 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

我知道我可以终止该进程,但我想做得更干净。

i'm running mongo 1.8.2 and trying to see how to cleanly shut it down on Mac.

on our ubuntu servers i can shutdown mongo cleanly from the mongo shell with:

> use admin
> db.shutdownServer()

but on my Mac, it does not kill the mongod process. the output shows that it 'should be' shutdown but when i ps -ef | grep mongo it shows me an active process. also, i can still open a mongo shell and query my dbs like it was never shutdown.

the output from my db.shutdownServer() locally is:

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

i know i can just kill the process but i'd like to do it more cleanly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

芸娘子的小脾气 2024-12-27 20:17:16

这可能是因为 launchctl 正在管理您的 mongod 实例。如果您想启动和关闭 mongod 实例,请先卸载它:

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

然后手动启动 mongod:

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

您可以从 ~/Library/LaunchAgents/org.mongodb.mongod.plist 找到您的 mongod.conf 位置。

之后,db.shutdownServer() 就可以正常工作了。

2014 年 2 月 22 日添加:

如果您通过 homebrew 安装了 mongodb,那么 homebrew 实际上有一个方便的 brew services 命令。显示当前正在运行的服务:

brew services list

要启动 mongodb:

brew services start mongodb-community

要停止 mongodb(如果它已在运行):

brew services stop mongodb-community

更新*

正如 edufinn 在评论中指出的那样,brew services 现在可以作为用户定义的命令使用,并且可以使用以下命令进行安装: brew tap gapple/services

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.

清君侧 2024-12-27 20:17:16

如果您使用自制程序安装了 mongodb,则有一种更简单的方法:

使用 launchctl 列出 mongo 作业:

launchctl list | grep mongo

停止 mongo 作业:(

launchctl stop <job label>

对我来说,这是 launchctl stop homebrew.mxcl.mongodb

启动 mongo 作业:

launchctl start <job label>

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>
笛声青案梦长安 2024-12-27 20:17:16

简单的方法是获取 mongodb 的进程 id 并将其杀死。
请注意,请勿为此使用kill -9 pid,因为它可能会损坏数据库。

所以,
1. 获取mongodb的pid

$ pgrep mongo

你会得到mongo的pid,现在

$kill

你也可以使用kill -15

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.

so,
1. get the pid of mongodb

$ pgrep mongo

you will get pid of mongo, Now

$ kill

You may use kill -15 as well

ˇ宁静的妩媚 2024-12-27 20:17:16

如果您通过自制程序安装了 mongodb 社区服务器,那么您可以执行以下操作:

brew services list

这将列出当前的服务,如下所示:

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

redis             stopped

然后您可以通过先停止并重新启动来重新启动 mongodb:

brew services stop mongodb
brew services start mongodb

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
琉璃繁缕 2024-12-27 20:17:16

我更喜欢使用 port 命令本身来停止 MongoDB 服务器。

sudo port unload mongodb

并重新开始。

sudo port load mongodb

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
公布 2024-12-27 20:17:16

查看这些文档:

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

如果您开始在终端中,您应该可以使用 ctrl + 'c' ——这将完成一次干净的关闭。

但是,如果您使用 launchctl,则会有具体说明,具体说明会根据安装方式的不同而有所不同。

如果您使用的是 Homebrew,则为 launchctl stop homebrew.mxcl.mongodb

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

忘年祭陌 2024-12-27 20:17:16

其他人提供的解决方案曾经对我有用,但不再对我有用,如下所示。

brew services stop mongodb
brew services start mongodb

brew services list 给出

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

所以我使用 mongodb-community 而不是对我有用的 mongodb

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

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
乞讨 2024-12-27 20:17:16

这是一个老问题,但也是我在搜索时发现的。

如果您使用brew安装,那么解决方案实际上是这样的:

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

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

罪歌 2024-12-27 20:17:16

如果该服务通过brew运行,您可以使用以下命令停止它:

brew services stop mongodb

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

brew services stop mongodb
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文