Configuring MongoDB on Windows

2019-03-26 18:04发布

问题:

I am trying to set up MongoDB on Windows, and the online docs seem far from accurate.

Under "Configure a Windows Service" part, step 1 mentions to create a config file. Then it mentions to fill in the file with a line in the format logpath="X:\path\mongo.log". However, following the link, the config file is said to be in YAML format, which renders the previous line unreadable in YAML.

I have created a basic mongodb.cfg(.cfg or .conf??) file:

systemLog:
   destination: file
   path: "P:\\Servers\\MongoDB\\logs\\mongodb.log"
   quiet: true
   logAppend: true
storage:
   dbPath: "P:\\Servers\\MongoDB\\data"
   journal:
      enabled: true
net:
   bindIp: 127.0.0.1
   port: 27017

However when I start mongod --config P:\Servers\MongoDB\mongodb.cfg, the service just won't give any output at all, and just hangs.

If I remove the dbPath line, it will just close itself with no message at all.

I have also tried to leave the mongodb.cfg file just like this:

logpath="P:\Servers\MongoDB\logs\mongodb.log"
dbpath="P:\Servers\MongoDB\data"

But execution aborts complaining about any of the 2 paths, even tho they exist. Tried with single backslashes and with escaped backslashes (\\) with no success.

The only way the service works and listens for connections is to manually pass --dbpath only, and ignore any config file and logpath at all. Obviously this is not serious, as I need to keep track of the logs and also might need to change config parameters at some later point.

This is nuts... Am I missing something very basic or this docs are a real mess?

回答1:

Here is my simple test MongoDB Config file for Windows. Note that I had to have 2 spaces before each property, e.g., path. When I had 3 spaces I got an error at startup.

I start the server with: mongod --config c:\tools\mongodb\db\mongod.cfg

systemLog:
  destination: file
  path: "C:\\tools\\mongodb\\db\\log\\mongo.log"
  logAppend: true
storage:
  dbPath: "C:\\tools\\mongodb\\db\\data"
security:
  authorization: enabled


回答2:

Here's an example of a mongodb.config file for Windows.

##store data here
dbpath=C:\mongodb\data\db

##all output go here
logpath=C:\mongodb\data\log\mongo.log

##log read and write operations
diaglog=3


回答3:

Bit late but I had the exact same issue today. If you use forward slashes for your paths within the config file it works fine.

systemLog: destination: file logAppend: true path: "e:/mongo_data/3.6/mongo.log" storage: dbPath: "e:/mongo_data/3.6/db" engine: "wiredTiger"



回答4:

check if you have any file in data\db path. Please remove all those files and try to restart. I exactly used your config file and able to start the service successfully with bindip and port



回答5:

You can find logs with reasons why it's not working in log file. In your case, read P:\Servers\MongoDB\logs\mongodb.log file.