Sunday, September 25, 2022

User administration MongoDB Shell

sudo dpkg -i mongodb-mongosh_1.6.0_amd64.deb

mongosh

Current Mongosh Log ID: 632f7956d170bc14bd1fb47d
Connecting to:          mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.6.0
Using MongoDB:          6.0.1
Using Mongosh:          1.6.0

For mongosh info see: https://docs.mongodb.com/mongodb-shell/


To help improve our products, anonymous usage data is collected and sent to MongoDB periodically (https://www.mongodb.com/leg
al/privacy-policy).
You can opt-out by running the disableTelemetry() command.

test>

////////////////////////////////////////////////////////

CREATE ADMIN USER

use admin

switched to db admin

db.createUser({user: "user", pwd: "user", roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]})

exit

////////////////////////////////////////////////////////

mongosh -u user -p --authenticationDatabase admin

show dbs

admin   132.00 KiB
config   12.00 KiB
local    88.00 KiB

////////////////////////////////////////////////////////

CREATE COLLECTION

use user

switched to db user

db.cards.insertOne({image: "http://127.0.0.1:3001/img/", link: "#1", title: "1 title", text: "1 Lorem ipsum dolor sit ns ectetur adipisicing elit. Similique architecto voluptatem dignissimos rerum doloribus vero dolore adipisci, maiores commodi u llam suscipit dolores cum. Vel, nemo!"})

{

 acknowledged: true,
 insertedId: ObjectId("633033768383cef7a400914f")
}

show collections

cards

db.cards.find()

[
 {
   _id: ObjectId("633033768383cef7a400914f"),
   image: 'http://127.0.0.1:3001/img/',
   link: '#',
   title: '11111111',
   text: '1Lorem ipsum dolor sit nsectetur adipisicing elit. Similique architecto voluptatem dignissimos rerum doloribus ver
o dolore adipisci, maiores commodi ullam suscipit dolores cum. Vel, nemo!'
 }
]

////////////////////////////////////////////////////////

DELETE USER

mongosh -u user -p --authenticationDatabase admin

show users

[]

CREATE NON-ADMIN USER

use user

db.createUser({user: "user", pwd: "user", roles: [ { role: "readWrite", db: "user" } ] })

{ ok: 1 }

db.dropUser("user", "comment")

{ ok: 1 }

db.dropUser("user", "writeConcern") // delete user

////////////////////////////////////////////////////////

https://stackoverflow.com/questions/23943651/mongodb-admin-user-not-authorized

use admin

db.grantRolesToUser("user", ["root"])

DELETE DATABASE

use user

switched to db user

db.dropDatabase()

{ ok: 1, dropped: 'user' }


show dbs //to list existing databases

use <dbname> //the <dbname> is the database you'd like to drop

db //should show <dbname> just to be sure I'm working with the right database

db.dropDatabase() //will delete the database & return { "dropped" : "<dbname>", "ok" : 1 }

No comments:

Post a Comment