0

Docker compose with Mongo replica-set has been run and rs was initialized successfully.

I can connect to each container in standalone mode using mongodb://localhost:27017/db command, but can't do it when use spring.data.mongodb.uri.

spring.data.mongodb.uri: mongodb://localhost:27017,localhost:27018,localhost:27019/asc?replicaSet=rs0

#!/bin/bash

PRIMARY_HOST="mongo-master"
SECONDARY1_HOST="mongo-slave1"
SECONDARY2_HOST="mongo-slave2"
PORT=27017

HIGHEST_PRIORITY=2
STANDARD_PRIORITY=1

echo "Sleeping for 30 seconds with countdown:"
for i in {30..1}; do
  echo "$i seconds remaining..."
  sleep 1
done


# init replica-set
mongosh --host $PRIMARY_HOST:$PORT <<EOF
  rs.initiate({
    _id: "rs0",
    members: [
      { _id: 0, host: "$PRIMARY_HOST:$PORT", priority: $HIGHEST_PRIORITY },
      { _id: 1, host: "$SECONDARY1_HOST:$PORT", priority: $STANDARD_PRIORITY },
      { _id: 2, host: "$SECONDARY2_HOST:$PORT", priority: $STANDARD_PRIORITY }
    ]
  })
EOF

# Check the success of the initialization
if [ $? -eq 0 ]; then
  # Check for the presence of the "errmsg" string in the MongoDB output
  if echo "$result" | grep -q '"errmsg"'; then
    echo "Error initializing replication."
    echo "$result"  # Output details of the error
  else
    echo "Replication successfully initialized."
  fi
else
  EXIT_CODE=$?
  echo "Error initializing replication. Exit code: $EXIT_CODE"
fi

Replica-set has been initialized successfully.

Errors are next during stratup SpringBoot app and connect to it:

application.yaml file:

spring:
data: mongodb: uri: mongodb://localhost:27017,localhost:27018,localhost:27019/asc?replicaSet=rs0

com.mongodb.MongoSocketException: mongo-slave1: nodename nor servname provided, or not known
        at com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:221) ~[mongodb-driver-core-4.11.1.jar:na]
        at com.mongodb.internal.connection.ServerAddressWithResolver.getSocketAddresses(ServerAddressWithResolver.java:68) ~[mongodb-driver-core-4.11.1.jar:na]
        at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:100) ~[mongodb-driver-core-4.11.1.jar:na]
        at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:78) ~[mongodb-driver-core-4.11.1.jar:na]
        at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:211) ~[mongodb-driver-core-4.11.1.jar:na]
        at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.lookupServerDescription(DefaultServerMonitor.java:196) ~[mongodb-driver-core-4.11.1.jar:na]
        at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:156) ~[mongodb-driver-core-4.11.1.jar:na]
        at java.base/java.lang.Thread.run(Thread.java:1583) ~[na:na]
    Caused by: java.net.UnknownHostException: mongo-slave1: nodename nor servname provided, or not known
    
    com.mongodb.MongoSocketException: mongo-master: nodename nor servname provided, or not known
        at com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:221) ~[mongodb-driver-core-4.11.1.jar:na]
        at com.mongodb.internal.connection.ServerAddressWithResolver.getSocketAddresses(ServerAddressWithResolver.java:68) ~[mongodb-driver-core-4.11.1.jar:na]
        at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:100) ~[mongodb-driver-core-4.11.1.jar:na]
        at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:78) ~[mongodb-driver-core-4.11.1.jar:na]
        at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:211) ~[mongodb-driver-core-4.11.1.jar:na]
        at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.lookupServerDescription(DefaultServerMonitor.java:196) ~[mongodb-driver-core-4.11.1.jar:na]
        at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:156) ~[mongodb-driver-core-4.11.1.jar:na]
        at java.base/java.lang.Thread.run(Thread.java:1583) ~[na:na]
    Caused by: java.net.UnknownHostException: mongo-master: nodename nor servname provided, or not known
    
    com.mongodb.MongoSocketException: mongo-slave2
        at com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:221) ~[mongodb-driver-core-4.11.1.jar:na]
        at com.mongodb.internal.connection.ServerAddressWithResolver.getSocketAddresses(ServerAddressWithResolver.java:68) ~[mongodb-driver-core-4.11.1.jar:na]
        at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:100) ~[mongodb-driver-core-4.11.1.jar:na]
        at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:78) ~[mongodb-driver-core-4.11.1.jar:na]
        at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:211) ~[mongodb-driver-core-4.11.1.jar:na]
        at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.lookupServerDescription(DefaultServerMonitor.java:196) ~[mongodb-driver-core-4.11.1.jar:na]
        at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:156) ~[mongodb-driver-core-4.11.1.jar:na]
        at java.base/java.lang.Thread.run(Thread.java:1583) ~[na:na]
    Caused by: java.net.UnknownHostException: mongo-slave2

It looks like driver try to use containers name instead localhost name. How to figure out with that?

5
  • 1
    Not sure you need to specify replicaset name in uri. Have you tried with this uri pattern "mongodb://user:secret@localhost:27017,localhost:27018,localhost:27019/databasename" Commented Nov 29, 2023 at 19:19
  • @charlycou Issue the same Commented Nov 29, 2023 at 20:21
  • 1
    stackoverflow.com/a/68674898/9731186. This post explain why replicaset are not responding to localhost but with alias set in rs.initiate(). Also make sure that your docker network is accessible from the host if your java client is being hosted on a docker container. Commented Nov 29, 2023 at 20:48
  • 1
    *not being hosted Commented Nov 29, 2023 at 21:03
  • @charlycou It looks like the post that you have posted, has helped. Thanks!! Commented Nov 30, 2023 at 11:26

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.