我想为我的 MongoDB 实例设置用户名和密码身份验证,以便任何远程访问都会询问用户名和密码。我尝试了 MongoDB 网站上的教程并做了以下操作:
use admin
db.addUser('theadmin', '12345');
db.auth('theadmin','12345');
之后,我退出并再次运行 mongo。而且我不需要密码来访问它。即使我远程连接到数据库,也不会提示我输入用户名和密码。
更新这是我最终使用的解决方案
1) At the mongo command line, set the administrator:
use admin;
db.addUser('admin','123456');
2) Shutdown the server and exit
db.shutdownServer();
exit
3) Restart mongod with --auth
$ sudo ./mongodb/bin/mongod --auth --dbpath /mnt/db/
4) Run mongo again in 2 ways:
i) run mongo first then login:
$ ./mongodb/bin/mongo localhost:27017
use admin
db.auth('admin','123456');
ii) run & login to mongo in command line.
$ ./mongodb/bin/mongo localhost:27017/admin -u admin -p 123456
mongodump
和 mongoexport
的用户名和密码的工作方式相同。
哇,这里有很多复杂/令人困惑的答案。
这是 v3.4 。
简短的答案。
1.在没有访问控制的情况下启动mongoDB。
2.连接到实例。
3.创建用户。
Stop the MongoDB instance and start it again with access control.
mongod --auth --dbpath /data/db
Connect and authenticate as the user.
use some_db db.auth("myNormalUser", "xyz123") db.foo.insert({x:1}) use some_other_db db.foo.find({})
Long answer: Read this if you want to properly understand.
It's really simple. I'll dumb the following down https://docs.mongodb.com/manual/tutorial/enable-authentication/
If you want to learn more about what the roles actually do read more here: [https://docs.mongodb.com/manual/reference/built-in-roles/](https://docs.mongodb.com/manual/reference/built-in-roles/)
1.在没有访问控制的情况下启动mongoDB。
2.连接到实例。
3.创建用户管理员。
The following creates a user administrator in the
adminauthentication database. The user is a
dbownerover the
some_dbdatabase and NOT over the
admindatabase, this is important to remember.
Or if you want to create an admin which is admin over any database:
Stop the MongoDB instance and start it again with access control.
mongod --auth --dbpath /data/db
Connect and authenticate as the user administrator towards the
admin
authentication database, NOT towards thesome_db
authentication database.The user administrator was created in the
adminauthentication database, the user does not exist in the
some_dbauthentication database.
use admin db.auth("myDbOwner", "abc123")
You are now authenticated as a
dbOwnerover the
some_dbdatabase. So now if you wish to read/write/do stuff directly towards the
some_dbdatabase you can change to it.
More on roles: https://docs.mongodb.com/manual/reference/built-in-roles/
If you wish to make additional users which aren't user administrators and which are just normal users continue reading below.
6.创建普通用户。
This user will be created in the
some_dbauthentication database down below.
7.退出Mongo Shell,重新连接,作为用户身份验证。
最后但并非最不重要的是,由于用户未正确发布有关
--auth
标志的命令,如果您不希望将其设置为标志,则可以在MongoDB的配置文件中设置此值。