1 回答
TA贡献1893条经验 获得超10个赞
有点晚了,但文档有所有答案。
https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo?tab=doc#example-Connect-SCRAM
基本上,您不应该将您的用户名和密码传递到连接 URI 中,而是将它们设置为选项(参见下面的完整示例)
// Configure a Client with SCRAM authentication (https://docs.mongodb.com/manual/core/security-scram/).
// The default authentication database for SCRAM is "admin". This can be configured via the
// authSource query parameter in the URI or the AuthSource field in the options.Credential struct.
// SCRAM is the default auth mechanism so specifying a mechanism is not required.
// To configure auth via URI instead of a Credential, use
// "mongodb://user:password@localhost:27017".
credential := options.Credential{
Username: "user",
Password: "password",
}
clientOpts := options.Client().ApplyURI("mongodb://localhost:27017").SetAuth(credential)
client, err := mongo.Connect(context.TODO(), clientOpts)
if err != nil {
log.Fatal(err)
}
_ = client
这个驱动程序有很多“问题”,在我决定真正查看文档后,它们都是可以解决的。
- 1 回答
- 0 关注
- 129 浏览
添加回答
举报