mgo.v2无法连接MongoDB 5.0.x

mgo.v2无法连接MongoDB 5.0.x

问题描述:

MongoDB升级到5.0.1之后,使用gopkg.in/mgo.v2来连接MongoDB数据库时,发现验证时出现验证错误了。server returned error on SASL authentication step: BSON field ‘saslContinue.mechanism’ is an unknown field.。

问题分析:

5.0后会对saslContinue参数严格校验,不配置会报错BSON field ‘saslContinue.mechanism’ is an unknown field. 。该问题详细可见官网或者阿里云社区中的说明
在这里插入图片描述更多信息,请参见Compatibility Changes in MongoDB 5.0

解决方法:

思路1:使用较低版本的Mongodb

我在版本5.0.9上遇到此错误。如果您不需要使用最新版本,则可以尝试使用Lowwer版本(例如4.4.6)。

思路2: 切换go包。 mgo已经跟不上mongo版本了,很早之前就停止维护了,后面会有更多的兼容性问题。
选型1:MongoDB官方 go-mongo-driver。
go-mongo-driver 功能抽象没有 mgo 那么高级,用起来很零碎。
选型2:
七牛研发团队开源Go语言的MongoDB driver Qmgo。
它基于MongoDB官方 mongodb/mongo-go-driver 实现,但是有着更好的易用性,设计上参考了老牌的driver Mgo: go-mgo/mgo(比如Mgo的链式调用)。

Qmgo 更好用的Go语言 MongoDB driver

猜你喜欢

转载自blog.csdn.net/inthat/article/details/125905285