Always getting error 'server selection timeout'

2,308 views
Skip to first unread message

Valentin Sánchez

unread,
Jul 29, 2018, 10:27:41 PM7/29/18
to mongodb-go-driver
I was making a simple test on the driver, and literally just copy and paste the example in the documentation. However, I always get the error 'server selection timeout' with whichever operation I use (Find, FindOne, InsertOne, etc.). The program hangs when it gets to the statement "collection.InsertOne(context.Background(), map[string]string{"hello": "world"})"

My database is running on my localhost, and it doesn't have anything special in terms of configuration. In fact, I can connect using the mongo client

MongoDB server version: 3.6.5



client, err := mongo.NewClient("mongodb://127.0.0.1:27017")
    if err != nil {
      log.Fatal(err)
    }

    err = client.Connect(context.TODO())
    if err != nil {
      log.Fatal(err)
    }

    collection := client.Database("baz").Collection("qux")
// Execution never passes the following line.
    res, err := collection.InsertOne(context.Background(), map[string]string{"hello": "world"})
    if err != nil {
      log.Fatal(err)
    }
    id := res.InsertedID

    fmt.Println(id)

Kris Brandow

unread,
Jul 30, 2018, 2:27:48 PM7/30/18
to mongodb-go-driver
Hi Valentin,

What version of the driver are you using? Are you just running mongod or are there other flags you are passing to it when you start it?

Thanks,
Kris

Valentin Sánchez

unread,
Jul 30, 2018, 9:44:47 PM7/30/18
to mongodb-go-driver
I'm using the last alpha realease (alpha 10). I'm simply running mongod with no flags

Kris Brandow

unread,
Aug 1, 2018, 3:49:56 PM8/1/18
to mongodb-...@googlegroups.com
Hi Valentin,

What operating system are you using? And when you connect the shell as you giving it the same connection string?

—Kris

--
You received this message because you are subscribed to the Google Groups "mongodb-go-driver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-go-driver+unsubscribe@googlegroups.com.
To post to this group, send email to mongodb-go-driver@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-go-driver/7d368216-0b39-4e67-ad47-3f0e7fa629ff%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Valentin Sánchez

unread,
Aug 4, 2018, 2:33:20 AM8/4/18
to mongodb-go-driver

I'm using Linux Mint 19. My local mongo database has no special configurations. I usually just run

service mongod start
mongo

The last command gives me:

MongoDB shell version v3.6.5
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.5

Database is working fine. I have been running some scripts with PHP and doing some queries through the CLI client.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-go-dri...@googlegroups.com.
To post to this group, send email to mongodb-...@googlegroups.com.

Kris Brandow

unread,
Aug 6, 2018, 8:06:43 AM8/6/18
to mongodb-...@googlegroups.com
If you start Mongo on a different port and then update the connection string used in the Go program, do you get the same error?

Kari Patana

unread,
Aug 22, 2018, 8:43:46 AM8/22/18
to mongodb-go-driver
Perhaps totally unrelated, but I got the same error when accessing MongoDB on VirtualBox host from the guest. Mongo had a replica set.

The address I used in the Golang client on the guest was 10.0.2.2. However, Mongo seemed to suggest instead to use the primary address 127.0.0.1. That of course didn't work from the VirtualBox guest, and collection.Find failed. I got it working by adding clientopt.Single(true).

rne...@eirsystems.net

unread,
Dec 1, 2018, 8:01:26 PM12/1/18
to mongodb-go-driver
What import statement did you run for the clientopt.Single(true) command?

Wan Bachtiar

unread,
Dec 6, 2018, 11:54:39 PM12/6/18
to mongodb-go-driver

What import statement did you run for the clientopt.Single(true) command?

Hi,

The original question was posted in July 2018 (alpha v0.0.10). There has been a lot of changes to mongo-go-driver since, currently it’s beta version v0.1. In the current version (v0.1), the import would be github.com/mongodb/mongo-go-driver/mongo/options. See also mongo/options/clientoptions.go#L248-L259

That of course didn’t work from the VirtualBox guest, and collection.Find failed. I got it working by adding clientopt.Single(true).

Worth mentioning that the original thread solved the problem by connecting to a single member of the replica set in a standalone mode. The problem was the replica set configurations contains host names that are not recognisable from the VirtualBox, hence connecting directly to only one member (without discovery of the other members via the config) works.

Although the error message may be similar to yours, it may be a completely different issue. Depending on the use case, you may want to solve it differently as well (i.e. connecting to a single member of replica set does not provide high availability).
If you still have issues, I would recommend to open a new thread along with:

  • MongoDB Go driver version
  • The problem that you’re experiencing
  • Snippet code that you’ve tried

Regards,
Wan.

Reply all
Reply to author
Forward
0 new messages