Tahmid Rafi
Tahmid Rafi

Reputation: 469

How to set default dbpath for mongoDB in 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 database in default C:\data\db\, I created a directory in the installation directory, i.e. C:\Program Files\MongoDB\data\db. Now I can run mongod server by the 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, apparently the default dbpath is set to C:\data\db\. Each time I run mongod, I have to explicitly specify --dbpath

Is there a way to override the default dbpath value? I have tried following this answer, which solved the issue. But as there were no default mongod.cfg file, now I have to tun the command:

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

Which doesn't help much, because now I have to explicitly specify --config each time. All I want to do is just type mongod, each time I want to start mongo server. How can this be done?

Upvotes: 12

Views: 22769

Answers (2)

Patrick McSweeney
Patrick McSweeney

Reputation: 101

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, make its contents:
mongod --dbpath "C:\Program Files\MongoDB\data\db"

Next save the file with a .bat extension then place it in the directory that mongod.exe is in. Whenever you would run mongod, instead run the batch file and you have effectively changed the default dbpath in windows.

Upvotes: 10

Allen Chou
Allen Chou

Reputation: 1237

You are almost close. In Windows, MongoDB can be installed as Service, the installation chapter of official document gives detailed instruction on how to get that done.

You mentioned "But as there were no default mongod.cfg file, now I have to run the command with --config". If you successfully install the MongoDB as service, you don't have to start and stop like that. The command will be as following:

 net start MongoDB

 net stop MongoDB

Actually, if you don't mind starting MongoDB when windows starts, you can also set MongoDB as auto-start service in Windows Service part.

Upvotes: 6

Related Questions