How to set default dbpath for mongoDB on Windows 7?

I just installed mongoDB in my windows machine

MongoDB version       : Windows 64-bit 2008 R2+ release 3.0.4
OS Version            : Microsoft Windows 7 Ultimate 64-bit
Installation Directory: "C:\Program Files\MongoDB"

      

instead of creating the default database C:\data\db\

, I created a directory in the install directory i.e. C:\Program Files\MongoDB\data\db

... Now I can start the server with the mongod

command

mongod --dbpath "C:\Program Files\MongoDB\data\db"

      

If I run only mongod

, it throws an exception saying

[initandlisten] exception in initAndListen: 29 Data directory C:\data\db\ not found., terminating

      

So the default dbpath is set to C:\data\db\

. Every time I start mongod I have to explicitly specify--dbpath

Is there a way to override the default dbpath

? I tried the following this answer which solved the problem. But since there was no default file mongod.cfg

, now I need to configure the command:

mongod --config "C:\Program Files\MongoDB\mongod.cfg"

      

It doesn't help much because now I have to explicitly specify --config

each time. All I want to do is just type mongod

, every time I want to start the mongo server. How can I do that?

+3


source to share


2 answers


You are almost close. On Windows MongoDB can be installed as a Service, the installation chapter of the official document gives detailed instructions on how to do this.

You mentioned "But since there was no default mongod.cfg file, now I need to run the command with --config". If you successfully install MongoDB service as a service, you don't need to start and stop like this. The command will be as follows:



 net start MongoDB

 net stop MongoDB

      

In fact, if you don't mind starting MongoDB on Windows startup, you can also install MongoDB as an autostart service in the Windows Service part.

+2


source


Another way to solve this is to simply make a batch file that runs the mongod command with the specified parameters. To do this, open a text file,
add its contents: mongod --dbpath "C: \ Program Files \ MongoDB \ data \ db"



Then save the file with a .bat extension, then place it in the directory where mongod.exe is located. Whenever you start mongod, run a batch file instead, and you've effectively changed the default dbpath on Windows.

+3


source







All Articles