简体   繁体   中英

Enable authentication in mongoDb in Ubuntu Desktop

I am usign Ubuntu 16.04 and trying to enable auth in mongodDb

I created a new User by

db.createUser({
user:"Sun",
pwd:"Sun",
roles:[{role:"userAdmin",db:"pet"}]
})

I checked the user by

db.getUsers();

But I am still able to modify database without providing username and password

I tried to enable authorizations by the commands provided in documentation but its not working for me.

In my system to start the mongodb i have to write

sudo service mongod start
mongod

Please tell me how to enable auth in mongoDb

It sounds like you may be missing a config directive in your mongod.conf file.

check step 4 in the docs

Re-start the MongoDB instance with access control.

Re-start the mongod instance with the --auth command line option or, if using a configuration file, the security.authorization setting .

So in your config file you should have the line;

security.authorization: enabled

From a test box with CentOS the file is located at /etc/mongod.conf and should look something like this (Checking you paths are correct etc)

systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

storage:
  dbPath: /var/lib/mongo
  journal:
    enabled: true

processManagement:
  fork: true
  pidFilePath: /var/run/mongodb/mongod.pid

net:
  port: 27017
  bindIp: 0.0.0.0

security.authorization: enabled

You may also find it easier to use one of the management tools to manage you users with a GUI (presuming your on a Windows client) as its much easier then doing everything via the command line...

Once you have updated your config remember to restart your Mongod instance and check the log file at /var/log/mongodb/mongod.log to make sure there are no errors on startup.

Remember you need to give your users access to any given database, I assign my users to each database, and not via the 'admin' db - I find it easier.

And lastly - If you find you have locked yourself out for some reason don't panic! just change the config line security.authorization: enabled to disabled and restart your instance you will the be able to log back in without any authentication!

The log file will also give you pointers as to access problems so check that while your testing/setting up your users.

And remember once finished secure your deployment and check you can't login or do anything without a valid login.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM