- connect to mongod using the localhost exception
>use admin >db.createUser( { user: "superuser", pwd: "12345678", roles: [ {role: "userAdminAnyDatabase", db: "admin"}, "root" ] } )
=========To create use in database===========
>use another_database
>db.createUser({user: "name", pwd: "password", roles: ['read', 'readWrite', 'dbAmin', 'dbOwner', 'userAdmin', ... http://docs.mongodb.org/v2.6/reference/built-in-roles/ ]})
====================End======================
- Close mongod
- Create keyFile
#openssl rand -base64 741 > mongodb-keyfile
#chmod 600 mongodb-keyfile
- Copy key file to each member of replica set
- Start mongod: mongod --auth --setParameter enableLocalhostAuthBypass=0 --setParameter enableTestCommands=0 --dbpath /your/path --replSet rs0 --smallfiles --oplogSize 128 --keyFile /path/to/mongodb-keyfile
Or edit in file config
dbpath=/var/lib/mongodblogpath=/var/log/mongodb/mongod.loglogappend=truebind_ip = 127.0.0.1auth = truesmallfiles = trueoplogSize = 128keyFile = /path/to/mongodb-keyfilereplSet = rs0setParameter=enableLocalhostAuthBypass=0setParameter=enableTestCommands=0
- Connect to mongod
#./mongo
>use admin
>db.auth('superuser', '12345678');
>rs.initiate()
- Add secondaries
>rs.add('host:port')
================================================================================
db.getCollectionNames().forEach(function(c) { if (c.indexOf("system.") == -1) db[c].drop();