dt3674 2019-04-07 01:00
浏览 343
已采纳

您如何使用官方的mongo-go-driver连接到MongoDB Atlas [重复]

This question already has an answer here:

I'm looking at the tutorial offered in conjunction with the release of the official mongo-go-driver and the connection example uses a MongoDB server on localhost

// Set client options
clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")

However, the new hosted MongoDB service Atlas requires username and password to login. The connection string takes the format

mongodb://[username:password@]host1[/[database][?options]]

but there is no Golang example in the driver examples for Atlas.

So I'm wondering, what is the best way to log into Atlas without hard coding a password into a source file that will be posted on Github?

</div>
  • 写回答

1条回答 默认 最新

  • dsarttv037029 2019-04-07 01:00
    关注

    I am hosting my test Atlas cluster on AWS so I wanted to have similar credential management to the AWS process. From the AWS credentials page:

    The default provider chain looks for credentials in the following order:

    1. Environment variables.

    2. Shared credentials file.

    3. If your application is running on an Amazon EC2 instance, IAM role for Amazon EC2.

    Therefore, I wanted to implement the environment veriable for my simple login to Atlas example. Code below assumes that the following line has been issued at the command line

    export MONGO_PW='<your Atlas admin user password>'

    Then the following program will verify your connection

    package main
    
    import (
        "context"
        "fmt"
        "os"
    
        "go.mongodb.org/mongo-driver/mongo"
        "go.mongodb.org/mongo-driver/mongo/options"
    )
    
    var username = "<username>"
    var host1 = "<atlas host>"  // of the form foo.mongodb.net
    
    func main() {
    
        ctx := context.TODO()
    
        pw, ok := os.LookupEnv("MONGO_PW")
        if !ok {
            fmt.Println("error: unable to find MONGO_PW in the environment")
            os.Exit(1)
        }
        mongoURI := fmt.Sprintf("mongodb+srv://%s:%s@%s", username, pw, host1)
        fmt.Println("connection string is:", mongoURI)
    
        // Set client options and connect
        clientOptions := options.Client().ApplyURI(mongoURI)
        client, err := mongo.Connect(ctx, clientOptions)
        if err != nil {
            fmt.Println(err)
            os.Exit(1)
        }
    
        err = client.Ping(ctx, nil)
        if err != nil {
            fmt.Println(err)
            os.Exit(1)
        }
    
        fmt.Println("Connected to MongoDB!")
    }
    
    

    From here the rest of the tutorial linked in my original question goes smoothly.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?