卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章35034本站已运行393

如何使用atlas在go中连接mongodb?

如何使用atlas在go中连接mongodb?

php小编苹果将为你介绍如何在Go语言中使用Atlas连接MongoDB。Atlas是MongoDB官方的云托管服务,提供了可靠的分布式数据库解决方案。通过Atlas,你可以轻松地在Go语言中连接MongoDB,并进行数据的读写操作。下面将为你详细介绍具体的步骤和代码实现,让你更好地掌握在Go语言中使用Atlas连接MongoDB的技巧。跟着小编一起来学习吧!

问题内容

我在连接到 mongodb 时遇到服务器选择超时问题。如有任何帮助,我们将不胜感激。


selection error: server selection timeout, current topology: { type:
replicasetnoprimary, servers: [{ addr:
ac-pjyudwq-shard-00-01.1bnb2bm.mongodb.net:27017, type: unknown, last
error: dial tcp 3.6.207.111:27017: i/o timeout }, { addr:
ac-pjyudwq-shard-00-00.1bnb2bm.mongodb.net:27017, type: unknown, last
error: dial tcp 3.7.150.83:27017: i/o timeout }, { addr:
ac-pjyudwq-shard-00-02.1bnb2bm.mongodb.net:27017, type: unknown, last
error: dial tcp 3.7.137.42:27017: i/o timeout }, ] } exit status 1

我用于连接的代码

const MONGOURL = "mongodb+srv://sai:[email protected]/?retryWrites=true&w=majority"

var collection *mongo.Collection

func init() {
    fmt.Println("in the init function") var databasename string = "GOAPI"
    var collectionname string = "Movies"
    client, err: = mongo.Connect(context.TODO(), options.Client().ApplyURI(MONGOURL)) if err != nil {
        fmt.Println("unable to get mongo connection ") log.Fatal(err)
    }
    collection = ( * mongo.Collection)(client.Database(databasename).Collection(collectionname)) fmt.Println("sucessfully collection is created ") doc: = Movie {
        Name: "rrr",
        Watched: false,
        Rating: 9,
        Id: "12121"
    }
    result, err: = collection.InsertOne(context.Background(), doc) if err != nil {
        log.Fatal("hey unable to insert one ", err)
    }
    fmt.Println("sucessfully added : ", result.InsertedID)

    // mongo.Connect()`your text`
}

解决方法

我在 aws 上托管我的测试 atlas 集群,因此我希望拥有与 aws 流程类似的凭证管理。从 aws 凭证页面:

默认提供程序链按以下顺序查找凭据:

  1. 环境变量。
  2. 共享凭据文件。
  3. 如果您的应用程序在 amazon ec2 实例上运行,则为 amazon ec2 的 iam 角色。

因此,我想为我的 atlas 示例的简单登录实现可验证的环境。下面的代码假设已在命令行中发出以下行

export mongo_pw='<您的atlas管理员用户密码>'

然后以下程序将验证您的连接

package main

import (
    "context"
    "fmt"
    "os"

    "go.mongodb.org/mongo-driver/mongo"
    "go.mongodb.org/mongo-driver/mongo/options"
)

var username = "<username>"
var host1 = ""  // 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!")
}

从这里开始,我原来问题中链接的教程的其余部分将顺利进行。

卓越飞翔博客
上一篇: 理解Python字典的顺序
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏