如何使用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 凭证页面:
默认提供程序链按以下顺序查找凭据:
- 环境变量。
- 共享凭据文件。
- 如果您的应用程序在 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 = "<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!") }
从这里开始,我原来问题中链接的教程的其余部分将顺利进行。
以上是如何使用atlas在go中连接mongodb?的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

Go语言中用于浮点数运算的库介绍在Go语言(也称为Golang)中,进行浮点数的加减乘除运算时,如何确保精度是�...

Go爬虫Colly中的Queue线程问题探讨在使用Go语言的Colly爬虫库时,开发者常常会遇到关于线程和请求队列的问题。�...

Go语言中哪些库是大公司开发或知名开源项目?在使用Go语言进行编程时,开发者常常会遇到一些常见的需求,�...

Go语言中结构体定义的两种方式:var与type关键字的差异Go语言在定义结构体时,经常会看到两种不同的写法:一�...

Go语言中使用RedisStream实现消息队列时类型转换问题在使用Go语言与Redis...

Go语言中字符串打印的区别:使用Println与string()函数的效果差异在Go...

Go指针语法及viper库使用中的寻址问题在使用Go语言进行编程时,理解指针的语法和使用方法至关重要,尤其是在...

为什么Go语言中的map迭代会导致所有值变成最后一个元素?在Go语言中,面对一些面试题时,经常会遇到关于map�...
