45

I run mongod.exe in server with this option in cmd.exe:

mongod.exe --dbpath=path --auth

And now, how can I do this in a config file?
my mongod.cfg:

dbpath=D:\Program Files\MongoDB 2.6 Standard\data

security=
authorization= enabled

I get this error :

Error parsing INI config file: unknown option security.

4

9 Answers 9

84

For mongod version 2.4 (ini config file)

auth = true

https://docs.mongodb.com/v2.4/reference/configuration-options/#auth

For mongod versions 2.6+ (yaml config file)

security:
    authorization: enabled

https://docs.mongodb.com/v3.2/reference/configuration-options/#security.authorization https://docs.mongodb.com/v3.0/reference/configuration-options/#security.authorization https://docs.mongodb.com/v2.6/reference/configuration-options/#security.authorization

5
  • 1
    I have the problem on v3.2.18. Where --auth works.. but security:authorization: enabled done not, giving UserNotFound. Any thoughts?
    – aSmater.Me
    Commented May 10, 2018 at 13:33
  • 1
    @codesmarter, are you sure your config file is being used? e.g. You have the -f or --config option specified when starting mongod, or have the config file in the correct location if using a system default install? You also could check that you are authenticating against the correct database and that the database has the users you expect (e.g. check output of db.getUsers()).
    – Gary
    Commented May 16, 2018 at 15:32
  • Thanks, You are right that the config was reading from a different database.
    – aSmater.Me
    Commented May 16, 2018 at 15:35
  • 1
    As stated in other answers the right config was wrapping with quotes "enabled" Commented Jul 18, 2018 at 21:11
  • 2
    @CarloRodríguez, being YAML, the quotes should not matter is this case. See also: stackoverflow.com/questions/19109912/…
    – Gary
    Commented Jul 19, 2018 at 2:53
32

mongodb version 3.2

this is the correct config

security:
  authorization: "enabled"

with quotes since the value is a string as per the documentation

2
11
security:
   authorization: "enabled"

This is correct as stated above, one thing to note is if it still doesn't work make sure you didn't use a tab for the authorization line it won't work you need to just use spaces.

1
  • that's right, but you have to check log telling what you are dealing with error in YAML(Error parsing YAML). And one more thing - 2 spaces from the start of the string authorization, if none then error in yaml
    – Ivan Vovk
    Commented Jun 11, 2019 at 13:04
8

Just needs to say

auth=true

You don't need any of the other options!

2
  • I changed the config file and put your configuration auth=true and when I execute mongo I got a free access. Do I need to do any other configuration?
    – Robert
    Commented Aug 5, 2015 at 23:22
  • can you share the mongod.cfg fully after that update? Commented Dec 21, 2015 at 16:46
6

If you are use YAML

security:
  authorization: "enabled"

working for 2.6 or high.

But if you don't use YAML.

auth: true

I'm use mongodb v3.0.2, v3.6.5, v4.0.3, both files are working correctly.

INI CONFIG

# mongodb.conf

# Where to store the data.
dbpath=/var/lib/mongodb

#where to log
logpath=/var/log/mongodb/mongodb.log

logappend=true

bind_ip = 0.0.0.0
port = 27017

journal=true
auth = true

YAML CONFIG

# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# Where and how to store data.
storage:
  dbPath: /var/lib/mongodb
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0


# how the process runs
processManagement:
  timeZoneInfo: /usr/share/zoneinfo

security:
  authorization: "enabled"

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options:

#auditLog:

#snmp:
0

On DigitalOcean you get the 3.6.3 version but with 2.* style config files. Which makes it confusion when reading all the comments above.

So, instead of following the DigitalOcean Guide of installing MongoDB (this one), you'd better take the official MongoDB Guide (this one)

0

Really late to they party.

For me nothing of the above worked (on windows, on Linux "authorization: enabled" works fine as long as permissions are right). Ended up creating a service of my own with --dbpath and --auth options using a third party service manager. Works like a charm.

I perfectly aware that this is not a direct answer but I hope, this will help someone.

Side note though, I could not use "C:\Program Files\MongoDB\4.2\Server\data" folder. No write permissions.

1
  • this is not an answer. Please make it as a new question. Commented Oct 22, 2019 at 9:21
0
db.adminCommand(
    {
        setParameter: 1, 
        security.authorization: "enabled" 
    }
);
-1

For me the key thing that was causing the fault/error was noted in Robert Walters response - Use of the TAB in the config file.

Changing this to 4 whitespaces fixed the issue whilst keeping the config file looking neat/readable.

1
  • Please do not use answers as a way to thank other users. Simply restating his answer isn't an answer in and of itself.
    – Zev
    Commented Jul 1, 2018 at 6:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.