0

I am new to docker and mongoDB, am using docker toolbox on widows 10 home machine.while I tried to follow a tutorial on medium on docker, .net core and mongodb am getting the following timeout error

A timeout occured after 30000ms selecting a server using CompositeServerSelector{ Selectors = MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 } }. Client view of cluster state is { ClusterId : "1", ConnectionMode : "ReplicaSet", Type : "ReplicaSet", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 1, EndPoint : "xxx.xxx.xx.xxx:27017" }", EndPoint: "xxx.xxx.xx.xxx:27017", State: "Disconnected", Type: "Unknown", HeartbeatException: "MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server. ---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond xxx.xxx.xx.xxx:27017
at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
at System.Net.Sockets.Socket.<>c.b__271_0(IAsyncResult iar) --- End of stack trace from previous location where exception was thrown --- at MongoDB.Driver.Core.Connections.TcpStreamFactory.ConnectAsync(Socket socket, EndPoint endPoint, CancellationToken cancellationToken) at MongoDB.Driver.Core.Connections.TcpStreamFactory.CreateStreamAsync(EndPoint endPoint, CancellationToken cancellationToken) at MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken cancellationToken) --- End of inner exception stack trace --- at MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken cancellationToken) at MongoDB.Driver.Core.Servers.ServerMonitor.HeartbeatAsync(CancellationToken cancellationToken)" }] }.

Configuration Class

namespace TodoApp.Common.Config
{
 public class MongoDbConfig
 {
    public string Database { get; set; }

    public string Host { get; set; }

    public int Port { get; set; }

    public string User { get; set; }

    public string Password { get; set; }

    public string ConnectionString
    {
        get
        {
            if (string.IsNullOrWhiteSpace(User) || string.IsNullOrWhiteSpace(Password))
            {
                return $@"mongodb://{Host}:{Port}";
            }
            return $@"mongodb://{User}:{Password}@{Host}:{Port}/{Database}?connect=replicaSet";
        }
      }
     }
    }

Context class

namespace TodoApp.Common.Models
{
 public class TodoContext : ITodoContext
 {
    private readonly IMongoDatabase _db;

    public TodoContext(IOptions<MongoDbConfig> config)
    {
        var client = new MongoClient(config.Value.ConnectionString);
        _db = client.GetDatabase(config.Value.Database);
    }

    public IMongoCollection<Todo> Todos => _db.GetCollection<Todo>("Todos");
  }
 }

appsettings

"MongoDB": {
 "Database": "TodoDB",
 "Host": "xxx.xxx.xx.xxx",
 "Port": "27017",
 "User": "root",
 "Password": "example"
},

docker-compose.yml

    version: '3.1'
     services:
      mongo:
        image: mongo
        restart: always
        environment:
         MONGO_INITDB_ROOT_USERNAME: root
         MONGO_INITDB_ROOT_PASSWORD: example
        ports:
         - 27017:27017
       mongo-express:
        image: mongo-express
        restart: always
        ports:
         - 8081:8081
        environment:
         ME_CONFIG_MONGODB_ADMINUSERNAME: root
         ME_CONFIG_MONGODB_ADMINPASSWORD: example
        depends_on:
         - mongo

Medium link : this

leox
  • 1,315
  • 17
  • 26
  • Did you already double-check the username (case sensitive) and the fact that your docker ports are correctly mapped? Docker port 27017 needs to be mapped to your container's published port. – Dennis Jan 30 '19 at 17:37
  • yes username is correct – leox Jan 30 '19 at 17:42
  • What about ports? Also, see if this helps: https://stackoverflow.com/questions/31314245/a-timeout-occured-after-30000ms-selecting-a-server-using-compositeserverselector – Dennis Jan 30 '19 at 17:44
  • Tried still not working – leox Jan 30 '19 at 18:13
  • 1
    Can you connect to your MongoDb cluster using mongo shell or Robomango (Robo 3T)? What I am getting at is you need to check the ports. I see you added docker-compose. Thanks – Dennis Jan 30 '19 at 18:19
  • Yes you are right it's not reachable with the port that I had given, how can I the port – leox Jan 31 '19 at 06:19
  • Am able to map the port. But now am facing with MongoDB.Driver.MongoAuthenticationException: Unable to authenticate using sasl protocol mechanism SCRAM-SHA-1. ---> MongoDB.Driver.MongoCommandException: Command saslStart failed: Authentication failed.. – leox Jan 31 '19 at 06:49
  • Am able to solve the issue with correcting the ID and modifying the connection string by removing the /{Database}?connect=replicaSet portion – leox Jan 31 '19 at 07:24

0 Answers0