Issue Connecting to MongoDB with Go (mgo): "No Reachable Servers"

127 views
Skip to first unread message

SEMIA GUESMI

unread,
Feb 17, 2025, 1:53:02 PMFeb 17
to golang-nuts
Dears,

Hi,

I have a question regarding the MongoDB package for Go (mgo).

I have installed MongoDB and confirmed that it is running. However, when I try to connect to it using Go, I consistently get the following error: "No reachable servers"

I would appreciate any guidance on how to resolve this issue.

Best,

Semia

package main

import (
"fmt"
"log"

)

func main() {
fmt.Println("Attempting to connect to MongoDB...")

session, err := mgo.Dial("mongodb://localhost:27017")
if err != nil {
log.Fatal("MongoDB connection failed: ", err)
}
defer session.Close()

fmt.Println("MongoDB connected successfully!")
}


Uzondu Enudeme

unread,
Feb 17, 2025, 3:33:40 PMFeb 17
to SEMIA GUESMI, golang-nuts
Hello Semia,

The mgo library you’re referencing has this text at top of the repository:


The MongoDB driver for Go. UNMAINTAINED - SEE BELOW


The implication of that statement is that the library may not work for newer versions of MongoDB and the error you’re experiencing could be a result of that. 

The package author recommends other packages to use listed in the repository such as a community fork of mgo.

Another recommended solution is to use the official Go driver for MongoDB at 

The official documentation has a guide for connecting to MongoDB here 


Kind regards, 

Uzondu 


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/golang-nuts/b7e7c5c9-700d-44b9-a46c-e7b64a4fadcdn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages