【问题标题】:Mongo-Go-Driver Failing to ConnectMongo-Go-Driver 连接失败
【发布时间】:2019-07-13 16:21:00
【问题描述】:

所以我尝试使用https://github.com/mongodb/mongo-go-driver 连接到 golang 中的 mongo 数据库。

这是我的连接处理程序:

var DB *mongo.Database

func CreateConnectionHandler()(*mongo.Database, error){
    fmt.Println("inside createConnection in database package")
    godotenv.Load()
    fmt.Println("in CreateConnectionHandler and SERVER_CONFIG: ")
    fmt.Println(os.Getenv("SERVER_CONFIG"))
    uri:=""
    if os.Getenv("SERVER_CONFIG")=="kubernetes"{
        fmt.Println("inside kubernetes db config")
        uri = "mongodb://patientplatypus:SUPERSECRETPASSDOOT@
               mongo-release-mongodb.default.svc.cluster.local:27017/
               platypusNEST?authMechanism=SCRAM-SHA-1"
    }else if os.Getenv("SERVER_CONFIG")=="compose"{
        fmt.Println("inside compose db config")
        uri = "mongodb://datastore:27017"
    }
    ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
    client, err := mongo.Connect(ctx, uri)
    if err != nil {
        return nil, fmt.Errorf("mongo client couldn't connect: %v", err)
    }
    DB := client.Database("platypusNEST")
    return DB, nil
}

我得到的错误是:

api         | database/connection.go:29:30: cannot use uri (type 
string) as type *options.ClientOptions in argument to mongo.Connect

所以我尝试用这样的连接字符串替换uri

client, err := mongo.Connect(ctx, "mongodb://datastore:27017")

但我仍然收到错误。

将此与文档中的内容进行比较:

ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
client, err := mongo.Connect(ctx, "mongodb://localhost:27017")

而且完全一样!我真的不确定为什么会出现这个错误。有什么想法吗?

【问题讨论】:

    标签: database mongodb go database-connection mongo-go


    【解决方案1】:

    对于那些来搜索的人 - 文档在此发布时已过时,但他们在此处的最新推送:https://github.com/mongodb/mongo-go-driver/commit/32946b1f8b9412a6a94e68ff789575327bb257cf 让他们通过连接执行此操作:

    client, err := mongo.NewClient(options.Client().ApplyURI(uri))
    

    您现在还需要导入选项包。愉快的黑客攻击。

    编辑:感谢 vcanales 找到这个 - 你是一位绅士和学者。

    【讨论】:

    • 遵循 github 的最新步骤。使用 import "go.mongodb.org/mongo-driver/mongo" 后,出现错误 "options.Client().ApplyURI undefined (type *options.ClientOptions has no field or method ApplyURI)" ... options 取自 github .com/mongodb/mongo-go-driver/mongo/options
    • 答案已过时
    • 最新答案可以在这里找到:stackoverflow.com/questions/55157890/…
    【解决方案2】:

    除了已接受的答案 - 下面的这个 sn-p 可以通过使用环境变量传入 Mongodb URL 来改进。

    package main
    
    import (
       "context" //use import withs " char
       "fmt"
       "time"
       "go.mongodb.org/mongo-driver/mongo"
       "go.mongodb.org/mongo-driver/mongo/options"
       "go.mongodb.org/mongo-driver/mongo/readpref"
    )
    
    func ConnectMongo() {
    
       var (
           client     *mongo.Client
           mongoURL = "mongodb://localhost:27017"
       )
    
       // Initialize a new mongo client with options
       client, err := mongo.NewClient(options.Client().ApplyURI(mongoURL))
    
       // Connect the mongo client to the MongoDB server
       ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
       err = client.Connect(ctx)
    
       // Ping MongoDB
       ctx, _ = context.WithTimeout(context.Background(), 10*time.Second)
       if err = client.Ping(ctx, readpref.Primary()); err != nil {
           fmt.Println("could not ping to mongo db service: %v\n", err)
           return
       }
    
       fmt.Println("connected to nosql database:", mongoURL)
    
    }
    
    
    func main() {
    
       ConnectMongo()
    }
    

    关于 options 和 readpref 的更多信息: https://docs.mongodb.com/manual/reference/method/cursor.readPref/index.html https://docs.mongodb.com/manual/core/read-preference/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-04
      相关资源
      最近更新 更多