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.

shA.t
  • 16,580
  • 5
  • 54
  • 111
mlibre
  • 2,460
  • 3
  • 23
  • 32

9 Answers9

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

Gary
  • 3,425
  • 25
  • 18
  • 1
    I have the problem on v3.2.18. Where `--auth` works.. but `security:authorization: enabled` done not, giving UserNotFound. Any thoughts? – aSmater.Me May 10 '18 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 May 16 '18 at 15:32
  • Thanks, You are right that the config was reading from a different database. – aSmater.Me May 16 '18 at 15:35
  • 1
    As stated in other answers the right config was wrapping with quotes `"enabled"` – Carlo Espino Jul 18 '18 at 21:11
  • 2
    @CarloRodríguez, being YAML, the quotes should not matter is this case. See also: https://stackoverflow.com/questions/19109912/do-i-need-quotes-for-strings-in-yaml – Gary Jul 19 '18 at 02: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

keithics
  • 8,576
  • 2
  • 48
  • 35
10
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.

Robert Walters
  • 1,367
  • 14
  • 10
  • 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 Jun 11 '19 at 13:04
8

Just needs to say

auth=true

You don't need any of the other options!

Malcolm Murdoch
  • 1,075
  • 6
  • 9
  • 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 Aug 05 '15 at 23:22
  • can you share the mongod.cfg fully after that update? – Amr Eladawy Dec 21 '15 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:
ThisClark
  • 14,352
  • 10
  • 69
  • 100
Burak Dalkıran
  • 141
  • 2
  • 2
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)

BertC
  • 2,243
  • 26
  • 33
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.

0
db.adminCommand(
    {
        setParameter: 1, 
        security.authorization: "enabled" 
    }
);
Vladimir
  • 441
  • 1
  • 4
  • 14
-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.

Harkole
  • 11
  • 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 Jul 01 '18 at 06:21