G
G
gremlintv22019-03-18 12:47:12
MongoDB
gremlintv2, 2019-03-18 12:47:12

What is the procedure for creating a user for remote access to mongodb?

I tried in different ways but it doesn't work: I
created a user in the admin database with the rights "userAdminAnyDatabase", "dbAdminAnyDatabase", "readWriteAnyDatabase":

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

Created a database:
use mydb
Trying to connect:
mongo 127.0.0.1 --port 27017 -u "admin" --authenticationDatabase "mydb"

Mistake:
MongoDB shell version v4.0.2
Enter password: 
connecting to: mongodb://127.0.0.1:27017/test
MongoDB server version: 4.0.2
2019-03-18T11:41:55.681+0200 E QUERY    [js] Error: Authentication failed. :
[email protected]/mongo/shell/db.js:1685:20
@(auth):6:1
@(auth):1:2
exception: login failed

I'm trying to connect:
mongo 127.0.0.1 --port 27017 -u "admin" --authenticationDatabase "admin"

It asks for a password and connects.
Config:
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log
storage:
  dbPath: /var/lib/mongo
  journal:
    enabled: true
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo
net:
  port: 27017
  bindIp: 127.0.0.1

Question 1: for what reason does it connect to test if I explicitly specified mydb ?
Question 2: Why does it throw an authentication error for admin when connecting to mydb ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Zamyatin, 2019-03-19
@gremlintv2

Look at the documentation, especially point 6: https://docs.mongodb.com/manual/tutorial/enable-au...
Please note that you create the admin user in the admin database with the userAdminAnyDatabase role, that is, he is the administrator of all databases on this server. The key "--authenticationDatabase" indicates that you are authorized through the database in which the user was created. When you try to fulfill

mongo 127.0.0.1 --port 27017 -u "admin" --authenticationDatabase "mydb"
mongo cannot find the specified user in mydb.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question