连接到 MongoDB 容器版本 6.0.2 | amd64/mongo 时出现错误。

huangapple go评论99阅读模式
英文:

Error connecting to mongodb container version 6.0.2 | amd64/mongo

问题

我能够使用下面的代码连接到版本为4.X和5.X的mongo容器,使用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)

但是,当我尝试将容器升级到版本6.0.2时,来自amd64/mongo的连接失败,并显示以下错误:

:无法连接到数据库:在连接握手期间发生connection()错误:身份验证错误:sasl对话错误:无法使用机制“SCRAM-SHA-1”进行身份验证:(AuthenticationFailed)身份验证失败。

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

我需要在我的mongo运行脚本文件中设置一个mongoDB服务器参数2,像下面这样吗?

--authenticationMechanisms=SCRAM-SHA-1

我所尝试做的就是连接到数据库并使用以下代码更改管理员和数据库密码,不确定在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)

不确定我在哪里犯了错误,错误消息并不直接。有人可以帮助我吗?

英文:

Iam able to connect mongo container versions 4.X and 5.X with the below code using 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)

But when i try to upgrade the container to version 6.0.2 from amd64/mongo

It fails with the below error

: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.

I believe by default it tries to pick SCRAM-SHA-1

do I need to set a mongoDB server param in my mongo run script file like below ?

--authenticationMechanisms=SCRAM-SHA-1

All i'm trying to do is connect to db and change the admin and db password using below code , not sure even if this is depreciated now in the mongo version 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)

not sure where am i making mistake , the error message is not straight forward . Can anyone help me here ?

答案1

得分: 0

我能够解决这个问题,问题出在我的脚本中使用了 mongo,但在最新的 MongoDB 版本 6.0 中已经移除了该命令。所以我改用了 mongosh,当我尝试初始化 MongoDB 容器时,它起作用了。

你可以参考这个链接了解更多关于 mongosh 的信息:

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

英文:

So was able to resolve this , The problem was my scripts were using mongo but that is removed in the latest mongo version 6.0 , So i used mongosh, when I try to init the mongo container, that worked.

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

huangapple
  • 本文由 发表于 2022年11月7日 16:21:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/74343697.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定