在 Mac OS X 上停止 mongod 的干净方法是什么?
我正在运行 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
这可能是因为 launchctl 正在管理您的 mongod 实例。如果您想启动和关闭 mongod 实例,请先卸载它:
然后手动启动 mongod:
您可以从
~/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:
Then start mongod manually:
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
.如果您使用自制程序安装了 mongodb,则有一种更简单的方法:
使用 launchctl 列出 mongo 作业:
停止 mongo 作业:(
对我来说,这是
launchctl stop homebrew.mxcl.mongodb
)启动 mongo 作业:
If you installed mongodb with homebrew, there's an easier way:
List mongo job with launchctl:
Stop mongo job:
(For me this is
launchctl stop homebrew.mxcl.mongodb
)Start mongo job:
简单的方法是获取 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
如果您通过自制程序安装了 mongodb 社区服务器,那么您可以执行以下操作:
这将列出当前的服务,如下所示:
然后您可以通过先停止并重新启动来重新启动 mongodb:
If you have installed mongodb community server via homebrew, then you can do:
This will list the current services as below:
Then you can restart mongodb by first stopping and restart:
我更喜欢使用
port
命令本身来停止 MongoDB 服务器。并重新开始。
I prefer to stop the MongoDB server using the
port
command itself.And to start it again.
查看这些文档:
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
其他人提供的解决方案曾经对我有用,但不再对我有用,如下所示。
brew services list
给出所以我使用
mongodb-community
而不是对我有用的mongodb
The solutions provided by others used to work for me but is not working for me anymore, which is as below.
brew services list
givesSo I used
mongodb-community
instead ofmongodb
which worked for me这是一个老问题,但也是我在搜索时发现的。
如果您使用
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
如果该服务通过brew运行,您可以使用以下命令停止它:
If the service is running via brew, you can stop it using the following command: