O'Reilly logo

PHP and MongoDB Web Development by Rubayeet Islam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action - adding user authentication in MongoDB

In this example, we will start the MongoDB server in authentication mode, and then add an admin user to the database. We will use this admin user account to create users for specific databases.

  1. Launch the monogd process in authentication mode by passing in the --auth flag:
    $ ./mongodb/bin/mongod --auth
    Sat Aug 27 23:11:56 [initandlisten] MongoDB starting : pid=603 port=27017 dbpath=/data/db/ 32-bit
    
    
  2. Launch the mongo shell and switch to admin database:
    $ ./mongodb/bin/mongo
    MongoDB shell version: 1.8.1
    connecting to: test
    > use admin
    switched to db admin
    >
    
  3. Add a user named dbadmin to the admin database and set an arbitrary password:
    > db.addUser(‘dbadmin', ‘mysecretpass') { "user" : "dbadmin", ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required