如何使用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脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

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

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Go語言中使用RedisStream實現消息隊列時類型轉換問題在使用Go語言與Redis...

GoLand中自定義結構體標籤不顯示怎麼辦?在使用GoLand進行Go語言開發時,很多開發者會遇到自定義結構體標籤在�...

Go語言中用於浮點數運算的庫介紹在Go語言(也稱為Golang)中,進行浮點數的加減乘除運算時,如何確保精度是�...

Go爬蟲Colly中的Queue線程問題探討在使用Go語言的Colly爬蟲庫時,開發者常常會遇到關於線程和請求隊列的問題。 �...

Go語言中字符串打印的區別:使用Println與string()函數的效果差異在Go...

Go語言中結構體定義的兩種方式:var與type關鍵字的差異Go語言在定義結構體時,經常會看到兩種不同的寫法:一�...

Go語言中哪些庫是大公司開發或知名開源項目?在使用Go語言進行編程時,開發者常常會遇到一些常見的需求,�...

Go編程中的資源管理:Mysql和Redis的連接與釋放在學習Go編程過程中,如何正確管理資源,特別是與數據庫和緩存�...
