登录
首页 >  Golang >  Go问答

本机数据库驱动程序连接 MongoDB 失败

来源:stackoverflow

时间:2024-04-15 18:33:35 425浏览 收藏

今天golang学习网给大家带来了《本机数据库驱动程序连接 MongoDB 失败》,其中涉及到的知识点包括等等,无论你是小白还是老手,都适合看一看哦~有好的建议也欢迎大家在评论留言,若是看完有所收获,也希望大家能多多点赞支持呀!一起加油学习~

问题内容

我正在尝试使用go语言通过原生mongodb驱动程序连接mongodb (参考)。

这是我的快照代码。

package main

import (
    "context"
    "fmt"
    "log"
    "time"

    "go.mongodb.org/mongo-driver/bson"
    "go.mongodb.org/mongo-driver/mongo"
    "go.mongodb.org/mongo-driver/mongo/options"
)

const (
    account               = "rootAdmin"
    password              = "12345678"
    iP                    = "127.0.0.1"
    port                  = 27017
    tlsCertificateKeyFile = "D:/cert/wa.pem"
)

type mongoStuff struct {
    ctx    context.Context
    client *mongo.Client
    cancel context.CancelFunc
}

func connectToMongoDB() *mongoStuff {
    uri := fmt.Sprintf("mongodb://%v:%v@%v:%v/?authSource=admin&tlsCertificateKeyFile=%v&tls=true",
        account,
        password,
        iP,
        port,
        tlsCertificateKeyFile)
    credential := options.Credential{
        AuthMechanism: "MONGODB-X509",
        Username:      account,
        Password:      password,
    }
    log.Println(uri)
    clientOpts := options.Client().ApplyURI(uri).SetAuth(credential)
    ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
    client, err := mongo.Connect(ctx, clientOpts)
    if err != nil {
        log.Println("Dead connect")
        log.Fatal(err)
    }
    return &mongoStuff{ctx, client, cancel}
}

func disconnectMongoDB(mongodb *mongoStuff) {
    cancel := mongodb.cancel
    client := mongodb.client
    ctx := mongodb.ctx
    defer cancel()
    defer func() {
        if err := client.Disconnect(ctx); err != nil {
            log.Println("Dead disconnect")
            panic(err)
        }
    }()
}

func insertExamples(mongodb *mongoStuff) {
    ctx := mongodb.ctx
    var db *mongo.Database = mongodb.client.Database("documentation_examples")
    coll := db.Collection("inventory_insert")
    err := coll.Drop(ctx)
    if err != nil {
        log.Println("Dead drop")
        log.Fatal(err)
    }
    {
        result, err := coll.InsertOne(
            ctx,
            bson.D{
                {"item", "canvas"},
                {"qty", 100},
                {"tags", bson.A{"cotton"}},
                {
                    "size", bson.D{
                        {"h", 28},
                        {"w", 35.5},
                        {"uom", "cm"},
                    }},
            })
        if err != nil {
            log.Println("Dead insertone")
            log.Fatal(err)
        }
        log.Printf("insertone success. id=%v", result.InsertedID)
    }
}

func main() {
    mongodb := connectToMongoDB()
    defer disconnectMongoDB(mongodb)
    insertExamples(mongodb)
}

每当我运行代码时,它都会出现以下错误。

连接握手期间发生连接()错误:身份验证错误:往返错误:(身份验证失败)未提供用户名

我不明白发生了什么。


解决方案


要使用 x.509 进行身份验证,用户名应该是证书的通用名称或为空。您似乎正在尝试密码和 x.509 身份验证的某种组合。

所有必需的选项都可以在 URI 中提供。请参阅 How can I connect with X509 by putting all options in the connection string in node.js driver for mongodb?

如果您坚持指定不在 URI 中的凭据,请参考描述如何对 x509 凭据执行此操作的驱动程序文档。

终于介绍完啦!小伙伴们,这篇关于《本机数据库驱动程序连接 MongoDB 失败》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>