连接mongodb容器版本6.0.2 | amd64/ mongodb时出错



我可以连接mongo容器版本4。X和5。使用amd64/mongo

    auth := options.Credential{
        AuthSource: admin,
        Username:   userName,
        Password:   pass,
    }
    opts := options.Client().ApplyURI(URI).SetAuth(auth).SetTLSConfig(&config)
    client, err := mongo.Connect(ctx, opts)

但是当我尝试从amd64/mongo升级到6.0.2版本时

失败,显示如下错误

:Unable to connect to thedatabase :connection() error occurred during connection handshake: auth error: sasl conversation error: unable to authenticate using mechanism "SCRAM-SHA-1": (AuthenticationFailed) Authentication failed.

我相信默认情况下它会选择SCRAM-SHA-1

我需要设置一个mongoDB服务器参数在我的mongo运行脚本文件如下?

--authenticationMechanisms=SCRAM-SHA-1

我所要做的就是连接到db并使用下面的代码更改admin和db密码,不确定即使这是贬值现在在mongo版本6.0.2

res := struct{ Ok int }{}
opts := options.RunCmd().SetReadPreference(readpref.Primary())  
command := bson.D{{"updateUser", usrName}, {"pwd", pass}}
err = client.Database(db).RunCommand(context.TODO(), command, opts).Decode(&res)

不确定我在哪里犯了错误,错误信息不是直截了当的。有人能帮我一下吗?

所以能够解决这个问题,问题是我的脚本使用mongo,但在最新的mongo版本6.0中被删除,所以我使用mongosh,当我尝试初始化mongo容器时,它起作用了。

https://www.mongodb.com/docs/mongodb-shell/

最新更新