且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Grails - 使用身份验证连接到MongoDB数据库

更新时间:2023-09-20 20:35:46

我遇到了同样的问题,所以我可以帮助解释Mongo如何进行身份验证。你看到你做的是你在管理数据库中创建了一个管理员用户,这是伟大的。但是,您尝试直接连接到mydb与管理员用户是不允许的。声音混乱?这是因为它是。为了更好地说明这里是一个简单的练习:

I ran into this same issue so I can help explain how Mongo does authentication. You see what you did is you created an admin user in the admin database which is great. However you are trying to connect to "mydb" directly with the admin user which is not allowed. Sound confusing? It's because it is. To illustrate this better here is a simple exercise:


  1. 像上面一样为管理数据库创建一个用户。

  2. 退出mongo shell

  3. 运行以下




mongo
use myDBname
db.auth("adminlogin", "adminpwd")


这将失败。


mongo
use admin
db.auth("adminlogin", "adminpwd")
use myDBname


这将工作,因为你切换到这个db与管理上下文,并没有尝试直接连接到它。

This will work because you switched to this db with the admin context and didn't try to connect to it directly.

所有你需要做的,使这个工作直接连接到您想要的数据库,并在该数据库中创建一个用户权限,如下所示:

So all you need to do to make this work is connect directly to the DB you want and create a user right in that db like follows:


mongo
use myDBname
db.addUser("dblogin", "dbpwd")


使用这个更新你的grails配置文件,我敢打赌你会工作。

Update your grails config file with this and I bet you it will work.

注意,最后一部分是你的答案并解决了你的问题,但是因为我努力了这一点,并认为它的困难的方式我认为上下文真正有助于了解mongo auth更好。

Note that just the last part is your answer and solves your problem but since I struggled with this and figured it out the hard way I think the context really helps understand mongo auth better.

照顾