Tuesday, April 28, 2026

.::: Command remote access mongodb using mongosh, authenticationMechanism SCRAM-SHA-256, authenticationMechanism SCRAM-SHA-1 :::

 .
https://teguhth.blogspot.com/2024/04/backup-restore-database-mongodb-using.html

1. console admin 

mongosh -u admin -p admin --authenticationDatabase admin

[root@teguhth-all ~]# mongosh -u admin -p admin --authenticationDatabase admin
Current Mongosh Log ID: 69f05c29e551710562d805da
Connecting to:          mongodb://<credentials>@127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&authSource=admin&appName=mongosh+2.8.1
Using MongoDB:          8.0.20
Using Mongosh:          2.8.1
mongosh 2.8.2 is available for download: https://www.mongodb.com/try/download/shell

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

------
   The server generated these startup warnings when booting
   2026-04-28T14:01:22.306+07:00: For customers running the current memory allocator, we suggest changing the contents of the following sysfsFile
   2026-04-28T14:01:22.306+07:00: We suggest setting the contents of sysfsFile to 0.
   2026-04-28T14:01:22.306+07:00: We suggest setting swappiness to 0 or 1, as swapping can cause performance problems.
------

test>

 
2. check user
db.getUsers()

admin> db.getUsers()
{
  users: [
    {
      _id: 'admin.admin',
      userId: UUID('8beeb9cd-22b9-4ea4-ab03-60da4348b0b1'),
      user: 'admin',
      db: 'admin',
      roles: [ { role: 'root', db: 'admin' } ],
      mechanisms: [ 'SCRAM-SHA-1', 'SCRAM-SHA-256' ]
    }
  ],
  ok: 1
}
admin>


3. enable auth & restart mongodb

security:
  authorization: enabled

[root@teguhth-all ~]# more /etc/mongod.conf
# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.

security:
  authorization: enabled

[root@teguhth-all ~]#


4. create user auth 
mongosh -u admin -p admin --authenticationDatabase admin

use teguhth

db.createUser({
  user: "appuser",
  pwd: "app123",
  roles: [
    { role: "readWrite", db: "teguhth" }
  ],
  mechanisms: ["SCRAM-SHA-1", "SCRAM-SHA-256"]
})

db.getUser("appuser")

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

db.getUser("appno")


log  

test> use teguhth
switched to db teguhth
teguhth> db.createUser({
|   user: "appuser",
|   pwd: "app123",
|   roles: [
|     { role: "readWrite", db: "teguhth" }
|   ],
|   mechanisms: ["SCRAM-SHA-1", "SCRAM-SHA-256"]
| })
{ ok: 1 }
teguhth>

teguhth> db.getUser("appuser")
{
  _id: 'teguhth.appuser',
  userId: UUID('85aa514c-dfe8-43b4-84fe-7db6deab277b'),
  user: 'appuser',
  db: 'teguhth',
  roles: [ { role: 'readWrite', db: 'teguhth' } ],
  mechanisms: [ 'SCRAM-SHA-1', 'SCRAM-SHA-256' ]
}
teguhth>

test> use teguhth
switched to db teguhth
teguhth>
| db.createUser({
|   user: "appno",
|   pwd: "app123",
|   roles: [
|     { role: "readWrite", db: "teguhth" }
|   ]
| })
|
{ ok: 1 }
teguhth> db.getUser("appno")
{
  _id: 'teguhth.appno',
  userId: UUID('1a3e1d1c-ccbd-4b20-8e61-90964a4cec0f'),
  user: 'appno',
  db: 'teguhth',
  roles: [ { role: 'readWrite', db: 'teguhth' } ],
  mechanisms: [ 'SCRAM-SHA-1', 'SCRAM-SHA-256' ]
}
teguhth>


5. test login via remote 

mongosh -u appuser -p app123 --authenticationDatabase teguhth
mongosh --host 10.10.10.90 -u appuser -p app123 --authenticationDatabase teguhth
mongosh --host 10.10.10.90 -u appuser -p app123 --authenticationDatabase teguhth --authenticationMechanism SCRAM-SHA-1
mongosh --host 10.10.10.90 -u appuser -p app123 --authenticationDatabase teguhth --authenticationMechanism SCRAM-SHA-256

[root@teguhth-all ~]# mongosh --host 10.10.10.90 -u appuser -p app123 --authenticationDatabase teguhth
Current Mongosh Log ID: 69f05f56f6114ea1bfd805da
Connecting to:          mongodb://<credentials>@10.10.10.90:27017/?directConnection=true&authSource=teguhth&appName=mongosh+2.8.1
Using MongoDB:          8.0.20
Using Mongosh:          2.8.1
mongosh 2.8.2 is available for download: https://www.mongodb.com/try/download/shell

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

test> use teguhth
switched to db teguhth
teguhth> show collections
barang
customer
mahasiswi
pasok
pembelian
suplier
teguhth> db.mahasiswi.find();
[
  {
    _id: ObjectId('69f01adeba3124f00dd805db'),
    nim: '11223344',
    nama: 'aaa',
    alamat: 'bandung'
  },
  {
    _id: ObjectId('69f01adeba3124f00dd805dc'),
    nim: '11223355',
    nama: 'bbb',
    alamat: 'jakarta'
  },
  {
    _id: ObjectId('69f01adeba3124f00dd805dd'),
    nim: '11223366',
    nama: 'ccc',
    alamat: 'bogor'
  }
]
teguhth>


6. test login via mongosh 

mongosh "mongodb://appuser:app123@10.10.10.90:27017/teguhth?authSource=teguhth"
mongosh "mongodb://appuser:app123@10.10.10.90:27017/teguhth?authSource=teguhth" --authenticationMechanism SCRAM-SHA-1
mongosh "mongodb://appuser:app123@10.10.10.90:27017/teguhth?authSource=teguhth&authMechanism=SCRAM-SHA-1"
mongosh "mongodb://appuser:app123@10.10.10.90:27017/teguhth?authSource=teguhth&authMechanism=SCRAM-SHA-256"


[root@teguhth-all ~]# mongosh "mongodb://appuser:app123@10.10.10.90:27017/teguhth?authSource=teguhth"
Current Mongosh Log ID: 69f05fa82486cd4e63d805da
Connecting to:          mongodb://<credentials>@10.10.10.90:27017/teguhth?authSource=teguhth&directConnection=true&appName=mongosh+2.8.1
Using MongoDB:          8.0.20
Using Mongosh:          2.8.1
mongosh 2.8.2 is available for download: https://www.mongodb.com/try/download/shell

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

teguhth>


7. access 

mongosh --host 10.10.10.90 -u appuser -p appno --authenticationDatabase teguhth --authenticationMechanism SCRAM-SHA-1

[root@teguhth-all ~]# mongosh --host 10.10.10.90 -u appuser -p appno --authenticationDatabase teguhth --authenticationMechanism SCRAM-SHA-1
Current Mongosh Log ID: 69f061011178784056d805da
Connecting to:          mongodb://<credentials>@10.10.10.90:27017/?directConnection=true&authSource=teguhth&authMechanism=SCRAM-SHA-1&appName=mongosh+2.8.1
MongoServerError: Authentication failed.

[root@teguhth-all ~]#
{
  "time": "2026-04-28 14:26:23",
  "level": "INFO",
  "component": "ACCESS",
  "message": "Failed to authenticate",

  "connection": "conn84",
  "client": "10.10.10.90:55282",

  "user": "appuser",
  "database": "teguhth",
  "mechanism": "SCRAM-SHA-1",

  "error": "AuthenticationFailed: SCRAM authentication failed, storedKey mismatch",
  "resultCode": 18,

  "driver": "mongosh 2.8.1",
  "node": "Node.js v24.14.0",
  "os": "Linux x64"
}


8. access using mongosh 

mongosh "mongodb://appuser:app123@10.10.10.90:27017/teguhth?authSource=teguhth" --authenticationMechanism SCRAM-SHA-1
mongosh "mongodb://appuser:app123@10.10.10.90:27017/teguhth?authSource=teguhth&authMechanism=SCRAM-SHA-1"

mongosh "mongodb://appuser:app123@10.10.10.90:27017/teguhth?authSource=teguhth" --authenticationMechanism SCRAM-SHA-256
mongosh "mongodb://appuser:app123@10.10.10.90:27017/teguhth?authSource=teguhth&authMechanism=SCRAM-SHA-256"


[root@teguhth-all ~]# mongosh "mongodb://appuser:app123@10.10.10.90:27017/teguhth?authSource=teguhth&authMechanism=SCRAM-SHA-1"
Current Mongosh Log ID: 69f06e5bab89b52954d805da
Connecting to:          mongodb://<credentials>@10.10.10.90:27017/teguhth?authSource=teguhth&authMechanism=SCRAM-SHA-1&directConnection=true&appName=mongosh+2.8.1
Using MongoDB:          8.0.20
Using Mongosh:          2.8.1
mongosh 2.8.2 is available for download: https://www.mongodb.com/try/download/shell

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

teguhth> exit
[root@teguhth-all ~]#
[root@teguhth-all ~]# mongosh "mongodb://appuser:app123@10.10.10.90:27017/teguhth?authSource=teguhth&authMechanism=SCRAM-SHA-256"
Current Mongosh Log ID: 69f06e64d5017f560dd805da
Connecting to:          mongodb://<credentials>@10.10.10.90:27017/teguhth?authSource=teguhth&authMechanism=SCRAM-SHA-256&directConnection=true&appName=mongosh+2.8.1
Using MongoDB:          8.0.20
Using Mongosh:          2.8.1
mongosh 2.8.2 is available for download: https://www.mongodb.com/try/download/shell

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

teguhth>

No comments:

Post a Comment

Popular Posts