Skip to content

Instantly share code, notes, and snippets.

@sarjarapu
Created May 16, 2018 14:42
Show Gist options
  • Save sarjarapu/761e4fc1ea7c5d46f1fb64d973d5a6bf to your computer and use it in GitHub Desktop.
Save sarjarapu/761e4fc1ea7c5d46f1fb64d973d5a6bf to your computer and use it in GitHub Desktop.
A bash script with MongoDB commands to create a user-defined role and a user.
mongo social --port 28000 -u user -p password --authenticationDatabase admin <<EOF
db.createRole({
role: "readWriteMinusDropRole",
privileges: [
{
resource: { db: "social", collection: ""},
actions: [ "collStats", "dbHash", "dbStats", "find", "killCursors", "listIndexes", "listCollections", "convertToCapped", "createCollection", "createIndex", "dropIndex", "insert", "remove", "renameCollectionSameDB", "update"]} ],
roles: []
}
);
use admin;
db.createUser({user: 'human_user', pwd: 'password', roles: [{role: 'readWriteMinusDropRole', db: 'social'}]})
EOF
# The output of above command
# MongoDB shell version v3.6.2
# connecting to: mongodb://127.0.0.1:28000/social
# MongoDB server version: 3.6.2
# {
# "role" : "readWriteMinusDropRole",
# "privileges" : [
# {
# "resource" : {
# "db" : "social",
# "collection" : ""
# },
# "actions" : [
# "collStats",
# "dbHash",
# "dbStats",
# "find",
# "killCursors",
# "listIndexes",
# "listCollections",
# "convertToCapped",
# "createCollection",
# "createIndex",
# "dropIndex",
# "insert",
# "remove",
# "renameCollectionSameDB",
# "update"
# ]
# }
# ],
# "roles" : [ ]
# }
# switched to db admin
# Successfully added user: {
# "user" : "human_user",
# "roles" : [
# {
# "role" : "readWriteMinusDropRole",
# "db" : "social"
# }
# ]
# }
# bye
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment