Skip to content

Instantly share code, notes, and snippets.

@sarjarapu
Created October 5, 2018 04:49
Show Gist options
  • Save sarjarapu/7c38093a8810bb2529006871f2a64190 to your computer and use it in GitHub Desktop.
Save sarjarapu/7c38093a8810bb2529006871f2a64190 to your computer and use it in GitHub Desktop.
A bash script using mongo client to create replica set and user privileges on $external database
mongo --quiet admin <<EOF
rs.initiate();
sleep(10000);
db.createUser({user: 'superuser', pwd: 'superuser', roles: ['root']});
db.auth('superuser', 'superuser');
use \$external;
db.createUser({user: '[email protected]', roles: [{ role: 'root', db: 'admin'}]});
db.createUser({user: '[email protected]', roles: [{ role: 'readWrite', db: 'social'}]});
EOF
# {
# "info2": "no configuration specified. Using a default configuration for the set",
# "me": "ip-172-31-10-102.us-west-2.compute.internal:27017",
# "ok": 1
# }
# Successfully added user: {
# "user": "superuser",
# "roles": ["root"]
# }
# 1
# switched to db $external
# Successfully added user: {
# "user": "[email protected]",
# "roles": [{
# "role": "root",
# "db": "admin"
# }]
# }
# Successfully added user: {
# "user": "[email protected]",
# "roles": [{
# "role": "readWrite",
# "db": "social"
# }]
# }
# bye
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment