« Mongodb » : différence entre les versions

De Banane Atomic
Aller à la navigationAller à la recherche
Ligne 60 : Ligne 60 :
db.createUser(
db.createUser(
   {
   {
     user: "myuser",
     user: "tom",
     pwd: passwordPrompt(), // or cleartext password
     pwd: passwordPrompt(), // or cleartext password
     roles: [ { role: "readWrite", db: "demo" },
     roles: [ { role: "readWrite", db: "demo" },
Ligne 66 : Ligne 66 :
   }
   }
)
)
// change password
use admin
db.changeUserPassword("tom", "secretpassword")
db.changeUserPassword("tom", passwordPrompt())
// delete a user
db.dropUser("tom")
</kode>
</kode>



Version du 8 mars 2023 à 13:06

Connect and authenticate

Bash.svg
# connect
mongo mongodb://localhost:27017
# alias
mongo

# connect and authenticate, use an environment variable to hide the password
mongo "mongodb://user:${DBPASSWORD}@<host>:<port>/admin?authSource=admin"

Authenticate

Mongodb.svg
db.auth("username", "pwd")

db.auth("username", passwordPrompt())

Database

Mongodb.svg
// list the databases
show dbs

Backup and restore

Bash.svg
# restore the backup folder dump to the local mongodb instance
mongorestore dump/

don't know what to do with file while restore

This may happen because the mongorestore command point to a backup sub-folder not to the backup folder itself.

Authentication

Mongodb.svg
// list the users of the current database
db.getUsers()
db.runCommand('usersInfo')

// get info of a specific user
db.getUser("tom",
{
    showCredentials: true,
    showPrivileges: true,
    showAuthenticationRestrictions: true
})

// create admin
use admin
db.createUser(
  {
    user: "admin",
    pwd: "mypwd",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)

// create a user for the demo database
use demo
db.createUser(
  {
    user: "tom",
    pwd: passwordPrompt(), // or cleartext password
    roles: [ { role: "readWrite", db: "demo" },
             { role: "read", db: "finances" } ]
  }
)

// change password
use admin
db.changeUserPassword("tom", "secretpassword")
db.changeUserPassword("tom", passwordPrompt())

// delete a user
db.dropUser("tom")
/etc/mongod.conf
security:
authorization: "enabled"

Compass

Installation

Windows

Ps.svg
choco install mongodb mongodb-shell
Key Value
Service name MongoDB
Data directory C:\Program Files\MongoDB\Server\6.0\data\
Log directory C:\Program Files\MongoDB\Server\6.0\log\