Posted on Leave a comment

How to add user and password in MongoDB

1) In mongo command line: (let say, set administrator)
  > use admin;
  > db.addUser('admin','123456');
2) Shutdown Server and exit
  > db.shutdownServer();
  > exit
3) Restart  Mongod with --auth
  $ sudo ./mongodb/bin/mongod --auth --dbpath /mnt/db/
4) Run mongo again in 2 ways:
  i)  run mongo first then login.
   $ ./mongodb/bin/mongo localhost:27017
   > use admin
   > db.auth('admin','123456');
  ii) run & login to mongo in command line.
   $ ./mongodb/bin/mongo localhost:27017/admin -u admin-p 123456

* The username & password will work the same for mongodump and mongoexport.
发表评论

电子邮件地址不会被公开。 必填项已用*标注