mongodb之本地 db driver 连接 MongoDB 失败

98°冷暖 阅读:33 2025-06-02 22:19:02 评论:0

我正在尝试通过 native MongoDB 驱动程序与 go 语言连接 MongoDB
(ref)。
这是我的快照代码。

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) 
} 
每当我运行代码时,它都会出现以下错误。

connection() error occured during connection handshake: auth error: round trip error: (AuthenticationFailed) No user name provided


我不知道发生了什么事。

请您参考如下方法:

要使用 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
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号