Friday, September 11, 2015

mongo replica with authentication

- Have not to create anything before
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/mongodb
logpath=/var/log/mongodb/mongod.log
logappend=true
bind_ip = 127.0.0.1
auth = true
smallfiles = true
oplogSize = 128
keyFile = /path/to/mongodb-keyfile
replSet = rs0
setParameter=enableLocalhostAuthBypass=0
setParameter=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();