So starten Sie den Mongo-Daemon mit Authentifizierungsunterstützung

So starten Sie den Mongo-Daemon mit Authentifizierungsunterstützung

Ich möchte den MongoDB-Daemon mit Authentifizierungsunterstützung mithilfe des Mongo-Init-Skripts starten:

sudo /etc/init.d/mongod start

Ich habe der Datenbank auch Datenbankbenutzer zur Authentifizierung hinzugefügt. Ich arbeite mit zwei Dateien: /etc/init.d/mongod(für Init) und /etc/mongod.conf(für Konfiguration).

#mongod.conf:
dbpath=/var/lib/mongodb
logappend=true
port = 27017 
auth = true

Die Nicht-Daemon-Methode startet den Prozess ordnungsgemäß mit dem --authFlag:

mongod --auth

Der Fork funktioniert, aber das Init-Skript wird nicht verwendet:

mongod --fork --auth --logpath /var/log/mongod.log

Lesen Sie alle Dokumentationen und verwandten Beiträge, niemand schien eine funktionierende Lösung zu haben, um Authentifizierungsunterstützung zu erhalten

service mongod start

Links:

Update: Ich habe Debian/Mongo neu installiert und konnte es service mongod startmit auth = trueder Conf-Datei verwenden. Wahrscheinlich habe ich bei der ersten Installation/Konfiguration etwas kaputt gemacht.

Antwort1

Ich habe dies gerade mit einer Neuinstallation von Debian 7 und einer Neuinstallation von MongoDB getestet. Ich habe zuerst einen Benutzer (adam) hinzugefügt und dann die /etc/mongod.confDatei bearbeitet, um die auth = trueZeile zu entfernen. Dann habe ich den service mongod restartBefehl eingegeben und versucht, mich als Benutzer anzumelden, was mir gelungen ist – ich habe auch falsche Anmeldeinformationen versucht und bin gescheitert. Daher scheint die Authentifizierung einwandfrei zu funktionieren, es gab keine offensichtlichen Probleme bei der Verwendung der Konfigurationsdatei, um anzugeben, dass die Authentifizierung aktiviert ist.

Also, ein paar Fragen:

  • Wie testen Sie, ob die Authentifizierung aktiviert ist?
  • Gibt es in Ihrer Konfigurationsdatei möglicherweise mehr als eine Zeile mit Auth-/Noauth-Anweisungen?

Als Referenz finden Sie hier die meisten meiner Tests mit dem Feedback von der Shell usw.

Zuerst die Installation und das Einrichten des Erstbenutzers:

root@deb7:~# apt-get install mongodb-org
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following extra packages will be installed:
  mongodb-org-mongos mongodb-org-server mongodb-org-shell mongodb-org-tools
The following NEW packages will be installed:
  mongodb-org mongodb-org-mongos mongodb-org-server mongodb-org-shell
  mongodb-org-tools
0 upgraded, 5 newly installed, 0 to remove and 20 not upgraded.
Need to get 114 MB of archives.
After this operation, 287 MB of additional disk space will be used.
Do you want to continue [Y/n]? 
** SNIP  for brevity** 
Setting up mongodb-org-shell (2.6.1) ...
Setting up mongodb-org-server (2.6.1) ...
Adding system user `mongodb' (UID 104) ...
Adding new user `mongodb' (UID 104) with group `nogroup' ...
Not creating home directory `/home/mongodb'.
Adding group `mongodb' (GID 107) ...
Done.
Adding user `mongodb' to group `mongodb' ...
Adding user mongodb to group mongodb
Done.
[ ok ] Starting database: mongod.
Setting up mongodb-org-mongos (2.6.1) ...
Setting up mongodb-org-tools (2.6.1) ...
Setting up mongodb-org (2.6.1) ...
root@deb7:~# mongo
MongoDB shell version: 2.6.1
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
    http://docs.mongodb.org/
Questions? Try the support group
    http://groups.google.com/group/mongodb-user
> use admin
switched to db admin
> db.createUser(
...   {
...     user: "adam",
...     pwd: "password123",
...     roles:
...     [
...       {
...         role: "userAdminAnyDatabase",
...         db: "admin"
...       }
...     ]
...   }
... )                           
Successfully added user: {
    "user" : "adam",
    "roles" : [
        {
            "role" : "userAdminAnyDatabase",
            "db" : "admin"
        }
    ]
}

Als nächstes habe ich die Datei bearbeitet und das auskommentierte /etc/mongod.confentfernt (sonst habe ich keine Änderungen vorgenommen). Ich habe die Datei gespeichert und dann den Dienst neu gestartet. Als nächstes habe ich mich mit dem Benutzer verbunden, den ich hinzugefügt hatte, und überprüft, ob ich die richtigen Berechtigungen hatte:#auth = true

root@deb7:~# vim /etc/mongod.conf 
root@deb7:~# service mongod restart
[ ok ] Restarting database: mongod.
root@deb7:~# mongo -u adam -p password123 --authenticationDatabase admin
MongoDB shell version: 2.6.1
connecting to: test
Error while trying to show server startup warnings: not authorized on admin to execute command { getLog: "startupWarnings" }

Wie Sie sehen, verfügt der von mir hinzugefügte Benutzer nicht über die Berechtigung, die Startwarnungen anzuzeigen. Um jedoch sicherzugehen, habe ich die Berechtigungen überprüft:

> use admin
switched to db admin
> db.runCommand(   {     usersInfo:"adam",     showPrivileges:true   } )
{
    "users" : [
        {
            "_id" : "admin.adam",
            "user" : "adam",
            "db" : "admin",
            "roles" : [
                {
                    "role" : "userAdminAnyDatabase",
                    "db" : "admin"
                }
            ],
            "inheritedRoles" : [
                {
                    "role" : "userAdminAnyDatabase",
                    "db" : "admin"
                }
            ],
            "inheritedPrivileges" : [
                {
                    "resource" : {
                        "db" : "",
                        "collection" : ""
                    },
                    "actions" : [
                        "changeCustomData",
                        "changePassword",
                        "createRole",
                        "createUser",
                        "dropRole",
                        "dropUser",
                        "grantRole",
                        "revokeRole",
                        "viewRole",
                        "viewUser"
                    ]
                },
                {
                    "resource" : {
                        "cluster" : true
                    },
                    "actions" : [
                        "authSchemaUpgrade",
                        "invalidateUserCache",
                        "listDatabases"
                    ]
                },
                {
                    "resource" : {
                        "db" : "",
                        "collection" : "system.users"
                    },
                    "actions" : [
                        "collStats",
                        "dbHash",
                        "dbStats",
                        "find",
                        "killCursors",
                        "planCacheRead"
                    ]
                },
                {
                    "resource" : {
                        "db" : "admin",
                        "collection" : "system.users"
                    },
                    "actions" : [
                        "collStats",
                        "dbHash",
                        "dbStats",
                        "find",
                        "killCursors",
                        "planCacheRead"
                    ]
                },
                {
                    "resource" : {
                        "db" : "admin",
                        "collection" : "system.roles"
                    },
                    "actions" : [
                        "collStats",
                        "dbHash",
                        "dbStats",
                        "find",
                        "killCursors",
                        "planCacheRead"
                    ]
                },
                {
                    "resource" : {
                        "db" : "admin",
                        "collection" : "system.version"
                    },
                    "actions" : [
                        "collStats",
                        "dbHash",
                        "dbStats",
                        "find",
                        "killCursors",
                        "planCacheRead"
                    ]
                },
                {
                    "resource" : {
                        "db" : "admin",
                        "collection" : "system.new_users"
                    },
                    "actions" : [
                        "collStats",
                        "dbHash",
                        "dbStats",
                        "find",
                        "killCursors",
                        "planCacheRead"
                    ]
                },
                {
                    "resource" : {
                        "db" : "admin",
                        "collection" : "system.backup_users"
                    },
                    "actions" : [
                        "collStats",
                        "dbHash",
                        "dbStats",
                        "find",
                        "killCursors",
                        "planCacheRead"
                    ]
                }
            ]
        }
    ],
    "ok" : 1
}                                        

Nur der Vollständigkeit halber hier ein Authentifizierungsfehler:

root@deb7:~# mongo -u root -p 12345678 --authenticationDatabase admin
MongoDB shell version: 2.6.1
connecting to: test
2014-05-11T18:04:39.793+0100 Error: 18 { ok: 0.0, errmsg: "auth failed", code: 18 } at src/mongo/shell/db.js:1210
exception: login failed   

Antwort2

Bearbeiten Sie /etc/mongod.conf und fügen Sie eine Zeile wie diese hinzu:

Für Mongo <3.0

auth=true

Dann:

service mongod restart

Für Mongo 3.x fügen Sie dies der Konfiguration hinzu

security:
  authorization: "enabled"

Antwort3

Das hat bei mir funktioniert (mit Mongo 3.2): Bearbeiten Sie /etc/mongod.conf und fügen Sie hinzu:

Sicherheit: Autorisierung: aktiviert

Ähnlich wie die andere Antwort, jedoch ohne Anführungszeichen um "aktiviert"

verwandte Informationen