How to properly shut down Mongo

898 views
Skip to first unread message

Jay Soma

unread,
May 8, 2012, 5:49:25 PM5/8/12
to mongodb-user
I have Mongo installed as a service on Windows. When my application
exits, I have tried calling both the Disconnect() and Shutdown()
methods, but the mongod.lock file always remains. It becomes a
problem when my system reboots, or comes out of Sleep/Hybernate mode.
I have to manually delete the mongod.lock file before the service will
restart successfully.

Kevin Matulef

unread,
May 9, 2012, 10:52:02 AM5/9/12
to mongod...@googlegroups.com
You should be able to cleanly exit using Ctrl-C or from the shell by typing
> use admin
> db.shutdownServer()


You shouldn't have to manually delete the lock file (which is definitely not recommended).  If these methods don't work for you, we can try to figure out the cause. 

GVP

unread,
May 10, 2012, 2:10:16 AM5/10/12
to mongod...@googlegroups.com
Obviously Kevin's Ctrl-C will not work for turning off a service. When running as a Windows Service, the best way to turn off is to actually stop the Windows Service.

There are three ways to do this:

  1. With a GUI: use the Services dialog box. Hit the windows key, type "service" and you will see an icon with two little gears. Click on this icon to open the services tab. This will give you a grid listing all of the services. Just find the MongoDB service, right click and select the Stop option.
  2. From the command prompt using the sc.exe tool. Hit the windows key, type "cmd" and hit enter. In the command window type "sc,exe stop MongoDB" and hit enter (MongoDB must be the name of the service in the control panel). Note that the SC command also has many other uses and should be a staple for windows admins.(http://ss64.com/nt/sc.html)
    Likewise, you can also do "NET START MongoDB" (http://ss64.com/nt/net_service.html)
  3. From PowerShell. From a PowerShell prompt, type "Get-Service MongoDB | Stop-Service".
My personal preference is that last one as it can be done using PowerShell remoting and it is easy to script, easy to understand.

Also, if you run journaling on the machine, you should not have problems with the mongod.lock and it should do a "fast repair" and come right back up.

If anyone at 10gen catches this post, please feel free to add some of this to the documentation page:

- Gates VP

Kevin Matulef

unread,
May 10, 2012, 2:29:41 AM5/10/12
to mongod...@googlegroups.com
Thanks, Gates!  Sorry I missed the "as a service" part, and thus totally the point of the question.  Thanks for the info, we'll make sure to update the documentation page accordingly. 

Tad Marshall

unread,
May 10, 2012, 5:04:59 AM5/10/12
to mongod...@googlegroups.com
Version 2.0 of MongoDB sometimes doesn't shut down properly when running as a service on a NET STOP or system shutdown or reboot.  It's a timing issue, where shutting down correctly depends on the order or two notifications, so it works fine on some machines and not on others.  This is fixed in 2.1.0.

Kevin's advice to use db.shutdownServer() will work every time in all versions.

Disconnecting from the server will not shut it down; the server is designed for multiple clients, so a disconnect of a client doesn't imply that the server should exit.  How are you calling "Shutdown()"?  Is this making the driver issue a shutdownServer() call?  That may be a separate issue.  Do you want to post sample code that tries to shut down the server and fails to do it?


On Tuesday, May 8, 2012 5:49:25 PM UTC-4, Jay Soma wrote:

Tad Marshall

unread,
May 10, 2012, 5:05:55 AM5/10/12
to mongod...@googlegroups.com
You meant "NET STOP MongoDB", not "NET START MongoDB".


On Thursday, May 10, 2012 2:10:16 AM UTC-4, GVP wrote:
Reply all
Reply to author
Forward
0 new messages