Unable to connect to mongodb dedicated cluster with auto scaling from go driver

DB String - > mongodb://<user>:<password>@rycaocluster-shard-00-02.apwt4.mongodb.net:27017/

please note that I have MongoDB Atlas Dedicated Server with Auto Scaling and I have used the primary node of the cluster in the string.

Error -
2022/03/17 20:18:18 server selection error: server selection timeout, current topology: { Type: Unknown, Servers: [{ Addr: rycaocluster-shard-00-02.apwt4.mongodb.net:27017, Type: Unknown, Last error: connection() error occured during connection handshake: connection(rycaocluster-shard-00-01.apwt4.mongodb.net:27017[-64]) socket was unexpectedly closed: EOF }, ] }

I searched about this and found a solution -

The solution says to provide a tlsCAfile in the DB String, but I am not sure how to configure a tls certificate in MongoDB Atlas. Please tell me if you know how to do that, or if you have any other solutions.

Hi @VIBHANSHU_RATHOD and welcome in the MongoDB Community :muscle: !

You are supposed to connect to the entire Replica Set, not just the primary. Use the connect button to retrieve the correct connection string starting with mongodb+srv://....

As this actually points to a DNS seedlist, this will redirect your connection to the right servers if your cluster ever scales up or down due to the auto scaling.


1 Like

it doesn’t work, mongodb doesn’t work with the srv link, any solution? also the same MongoDB worked on a vultr server but is not working on an aws ec2 server.

It could be different reasons. Here are the usual one I know about:

  • Forgot to add the IP address of the EC2 server in the IP Access List. That being said, it would be better to use the AWS Peering or even better the AWS Private Link. Both are available in Atlas (M10 and above only because it needs a dedicated env)
  • Root certificated too old. Usually it’s because you are running on an old OS that’s not up-to-date.
  • Forgot to install the DNS dependency that some drivers don’t include by default. For example for python you need to install pymongo[srv] and not just pymongo.

Can you connect from a mongosh from this same EC2 machine? This would be the first thing to check.


1 Like

I am aware about pymongo[srv], what is the similar dependency for go driver?

It doesn’t look that there is one in Go. But I never tried myself. I didn’t notice you where using Go earlier. You should be good to go with just the Go Driver.

Can you connect from mongosh from the same PC you are trying to connect from the Go driver?


I have the same issue even after I configure AWS Peering.

my uri: mongodb+srv://< user >:< password >@< cluster >/?retryWrites=true&w=majority
go.mongodb.org/mongo-driver v1.11.4

func GetClient() (*mongo.Client, error) {
	uri := os.Getenv("MONGODB_URI")
	logrus.Info("uri: ", uri)
	if mgClient != nil {
		return mgClient, nil
	serverAPI := options.ServerAPI(options.ServerAPIVersion1)
	clientOptions := options.Client().ApplyURI(uri).SetServerAPIOptions(serverAPI)
	mgClient, err := mongo.NewClient(clientOptions)
	if err != nil {
		return nil, errors.Wrap(err, "can't create mongo client")
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()
	err = mgClient.Connect(ctx)
	if err != nil {
		return nil, errors.Wrap(err, "can't connect mongo client")
	return mgClient, err

I have another lambda (same account, call the same uri) writing with nodejs work perfectly without pb connection handshake. But lambda golang throw this error.
Do you have any idea?

It seems like you are trying to connect to a MongoDB Atlas cluster from an AWS Lambda function using the Go driver. Based on the error you provided and the code snippet, here are a few suggestions to help resolve the issue:

  1. Whitelist your AWS Lambda function’s IP address in your MongoDB Atlas cluster’s IP Access List. If you don’t know the IP address of your Lambda function, you can whitelist all IP addresses ( for testing purposes, but be sure to restrict it to only the necessary IP addresses for security reasons in a production environment.
  2. Make sure you are using the latest version of the Go MongoDB driver. At the time of writing, the latest version is v1.11.4. You can update the driver using the following command:
go get -u go.mongodb.org/mongo-driver/mongo
  1. Your connection string should use the mongodb+srv:// format. Ensure that you have the required DNS package by running the following command:
go get github.com/miekg/dns
  1. Try adding a tls=true parameter to your connection string. Your connection string should look like:
  1. Test your connection using the MongoDB Shell (mongosh) from the same environment where your Lambda function is running. This can help you identify any connection issues outside of your Go code.

If you have followed these suggestions and still face issues, please provide more details about the error messages you are encountering and any additional information about your environment.

1 Like