我是 golang 和 MongoDB 的新手,我一直在關注 mongoDb 的官方博客,開始使用 go 的 mongo-db 驅動程序,但由于某種原因我無法連接到我的 mongodb 這是代碼,錯誤是“超出上下文期限”,thnx提前package mainimport ( "context" "fmt" "log" "time" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" // "go.mongodb.org/mongo-driver/mongo/readpref")func main() { client, err := mongo.NewClient(options.Client().ApplyURI("mongodb+srv://chandru:<Heregoesmypassword>@cluster0-9jkaf.mongodb.net/test?retryWrites=true&w=majority")) if err != nil { log.Fatal(err) } ctx, _ := context.WithTimeout(context.Background(), 20*time.Second) err = client.Connect(ctx) if err != nil { log.Fatal(err) } defer client.Disconnect(ctx) databases, err := client.ListDatabaseNames(ctx, bson.M{}) if err != nil { log.Fatal(err) } fmt.Println(databases)}
1 回答

富國滬深
TA貢獻1790條經(jīng)驗 獲得超9個贊
一個可能的原因是您沒有使用 MongoDB Atlas 將您的 IP 列入白名單。請參閱以下指南:https ://docs.atlas.mongodb.com/security-whitelist/#view-whitelist-entries
請記住,如果您從本地計算機連接,您的 IP 地址可能會更改。您可以從 ISP 獲取靜態(tài) IP 地址或通過另一臺計算機連接。
您也可以輸入 0.0.0.0/0 以允許從任何 IP 訪問,但這當然不太安全。
- 1 回答
- 0 關注
- 157 瀏覽
添加回答
舉報
0/150
提交
取消