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.

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

报告相同问题?

悬赏问题

  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致