1

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?

ZacSketches
  • 363
  • 2
  • 5
  • 13
  • 1
    There may not be a GoLang example, but the connection URI format is handled **exactly the same** for **ALL** languages. Common *"language tutorials"* generally only give examples on the presumption that you are connecting to a local instance of MongoDB. The majority of answers I post here also only show a local connection string and without credentials, but that does not make them *invalid* for usage with a remote host and with credentials. You simply swap in the contents of the URI. And **that** in fact has **many** existing answers and examples. – Neil Lunn Apr 07 '19 at 01:21
  • @NeilLunn I appreciate that the answer you link to is similar, but in many ways it is different & I think this question stands on its own as a contribution to both Atlas and the mongo-go-driver. Specific differences: (1) the other question uses the mgo driver in the question and the mongo-go-driver is only discussed in one of the answers; (2) the other question about a more complex connection using SRV whereas this question is about a basic connection that serves beginners more clearly, (3) this question provides a fully worked answer. I kindly ask that you remove your duplicate assignment. – ZacSketches Apr 07 '19 at 01:42
  • 1
    Not very different at all. In fact it's the official MongoDB Go driver ( second part of answer even thought the question uses mgo) and actually the recommended way to connect. And like I said it's just **one of many**. People have been using a connection URI for many years now. – Neil Lunn Apr 07 '19 at 01:47

1 Answers1

4

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.

Community
  • 1
  • 1
ZacSketches
  • 363
  • 2
  • 5
  • 13
  • any way to auto-discover the host? I have a feeling Mongo would prefer us to use the atlas connection string and the host that has, vs trying to create individual host connections. i.e. what if you have a more than 1 mongo host? – Cmag Oct 07 '19 at 05:22