我想为我的 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
的用户名和密码的工作方式相同。
最佳答案
你需要在设置用户后使用--auth
选项启动mongod
。
来自 MongoDB 站点:
Run the database (mongod process) with the
--auth
option to enable security. You must either have added a user to the admin db before starting the server with--auth
, or add the first user from the localhost interface.
MongoDB Authentication
https://stackoverflow.com/questions/4881208/