如何使用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, lasterror: dial tcp 3.6.207.111:27017: i/o timeout }, { addr:ac-pjyudwq-shard-00-00.1bnb2bm.mongodb.net:27017, type: unknown, lasterror: dial tcp 3.7.150.83:27017: i/o timeout }, { addr:ac-pjyudwq-shard-00-02.1bnb2bm.mongodb.net:27017, type: unknown, lasterror: 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.Collectionfunc 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 凭证页面:

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

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

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

export mongo_pw=”

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

package mainimport (    "context"    "fmt"    "os"    "go.mongodb.org/mongo-driver/mongo"    "go.mongodb.org/mongo-driver/mongo/options")var username = ""var host1 = ""  // of the form foo.mongodb.netfunc 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!")}

登录后复制

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

以上就是如何使用atlas在go中连接mongodb?的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2484292.html

(0)
上一篇 2025年3月4日 21:31:14
下一篇 2025年3月1日 20:10:34

AD推荐 黄金广告位招租... 更多推荐

相关推荐

发表回复

登录后才能评论