How to configure a MongoDB cluster which supports sessions?
Asked Answered
C

7

13

I want to explore the new transaction feature of MongoDB and use Spring Data MongoDB. However, I get the exception message "Sessions are not supported by the MongoDB cluster to which this client is connected". Any hint regarding the config of MongoDB 3.7.9 is appreciated.

The stacktrace starts with:

com.mongodb.MongoClientException: Sessions are not supported by the MongoDB cluster to which this client is connected at com.mongodb.MongoClient.startSession(MongoClient.java:555) ~[mongodb-driver-3.8.0-beta2.jar:na] at org.springframework.data.mongodb.core.SimpleMongoDbFactory.getSession(SimpleMongoDbFactory.java:163) ~[spring-data-mongodb-2.1.0.DATAMONGO-1920-SNAPSHOT.jar:2.1.0.DATAMONGO-1920-SNAPSHOT]

Cerargyrite answered 9/5, 2018 at 13:55 Comment(5)
You need a MongoDB "Server" version 3.6 in order to use sessions. An updated driver alone does not do the job without the server on the back end. That's what the error is telling you.Ragout
I'm having server version 3.7.9, i.e. the latest.Cerargyrite
There is no such thing as "server" 3.7.9. That's the Java driver version. The "server" means the thing you see when you connect via the mongo shell and type db.version(). Even if you were using a development branch server then the current "cut" is 3.7.5. So you're talking about the "driver" and I'm talking about the "server".Ragout
I downloaded http://downloads.mongodb.org/win32/mongodb-win32-x86_64-2008plus-ssl-3.7.9.zip?_ga=2.134458040.816321646.1525581231-1505195349.1522993255 and db.version() inside the mongo shell returns 3.7.9Cerargyrite
Well that is a development release but the driver certainly says the actual connection is not in fact a supported server, so you probably should check at least that you are actually connecting to the server instance you think you are connecting to. Also you probably should not be using a development release until you actually have working code against a "stable" release anyway. As already stated, you "should" be using a 3.6 series server in order to use sessions. Current driver and server releases in the "development" branch are in "flux" with various things in API likely to change.Ragout
S
15

I was having the same issue when I was trying to connect it to a single standalone mongo instance, however as written in the official documentation, that Mongo supports transaction feature for a replica set. So, I then tried to create a replica set with all instances on MongoDB 4.0.0, I was able to successfully execute the code. So, Start a replica set (3 members), then try to execute the code, the issue will be resolved.

NB : you can configure a replica set on the same machine for tests https://docs.mongodb.com/manual/tutorial/deploy-replica-set-for-testing/

Schlieren answered 18/7, 2018 at 8:9 Comment(0)
B
8

We were able to config in local as below

  • On Linux, a default /etc/mongod.conf configuration file is included when using a package manager to install MongoDB.

  • On Windows, a default <install directory>/bin/mongod.cfg configuration file is included during the installation

  • On macOS, a default /usr/local/etc/mongod.conf configuration file is included when installing from MongoDB’s official Homebrew tap.

Add the following config

replication:
   oplogSizeMB: 128
   replSetName: "rs0"
   enableMajorityReadConcern: true

sudo service mongod restart;

mongo;

rs.initiate({
      _id: "rs0",
      version: 1,
      members: [
         { _id: 0, host : "localhost:27017" }
      ]
   }
)

check for the config to be enabled

rs.conf()

we can use the connection URL as

mongodb://localhost/default?ssl=false&replicaSet=rs0&readPreference=primary

docs: config-options single-instance-replication

Boatyard answered 4/7, 2020 at 13:0 Comment(0)
S
5

Replica set is the resolution for the issue for sure

But doing replica of 3 nodes is not mandatory.

Solution 1 (for standalone setup)

For standalone mongo installation you can skip configuring 2nd or 3rd node as described on the official mongo documentations here

And you'll need to set a replSetName in the configuration

replication:
   oplogSizeMB: <int>
   replSetName: <string>
   enableMajorityReadConcern: <boolean>

and then run details of which are here

rs.initiate()

after this the connection string would be like below:-

mongodb://localhost:27017/<database_name>?replicaSet=<replSet_Name>

keys above that you need to replace:-

database_name = name of the database

replSet_Name = name of the replica set you setup in the above configuration

Solution 2 (only for docker based requirement)

Example Docker image with single node replica set acting as primary node for development environment is as below:-

I had hosted the docker image on the docker hub

docker pull krnbr/mongo:latest

Contents of the same Dockerfile are below:-

FROM mongo
RUN echo "rs.initiate({'_id':'rs0','members':[{'_id':0,'host':'127.0.0.1:27017'}]});" > /docker-entrypoint-initdb.d/replica-init.js
RUN cat /docker-entrypoint-initdb.d/replica-init.js
CMD [ "--bind_ip_all", "--replSet", "rs0" ]

Docker run command (replace with the image name that you build yoursef or use the on shared above i.e krnbr/mongo):-

without volume


docker run -d --name mongo -p 27017:27017 <Image Name> mongod --replSet rs0 --port 27017

with volume


docker run -d --name mongo -p 27017:27017 -v ~/.mongodb:/data/db <Image Name> mongod --replSet rs0 --port 27017

for supporting binding of any ip

docker run -d --name mongo -p 27017:27017 -v ~/.mongodb:/data/db <Image Name> mongod --bind_ip_all --replSet rs0 --port 27017
Subtonic answered 17/5, 2020 at 4:58 Comment(1)
@kakabaly I just tried to convert my standalone setup to a single node replica set following you're answer nut now I have ECONNREFUSED ::1:27012 when connecting to it. opened a question here #70322750 , if you could check it and see where I got it wrong I'd really appreciate it. PD I edited the mongod.conf file with Atom and saved it. Many thanksPhosphide
L
0

With the reference to the answer give by @kakabali, I have few a bit different scenario and configure it.

I am configure mongo with spring boot and try to use transactions management and getting the error:

com.mongodb.MongoClientException: Sessions are not supported by the MongoDB cluster to which this client is connected at

I follow few of the steps given by above answer and added few:

Change the mongo.cfg and added this

replication:
   oplogSizeMB: 128
   replSetName: "rs0"
   enableMajorityReadConcern: true

Restart the service as I am using Windows10.

Open mongo console and run rs.initilize()

Llovera answered 10/6, 2020 at 17:34 Comment(0)
Q
0

For those who deploy mongodb on docker the following link is all you need. No mongosh or mongodb CLI. Only the docker-compose.yml file. I recommend single-node replica set setup. Also mongo-express is a good tool for managing databases from a simple GUI.

https://medium.com/workleap/the-only-local-mongodb-replica-set-with-docker-compose-guide-youll-ever-need-2f0b74dd8384

My docker-compose.yaml file looks like this:

version: "3.8"

services:
  mongo1:
    image: mongo:7.0
    command: ["--replSet", "rs0", "--bind_ip_all", "--port", "27017"]
    ports:
      - 27017:27017
    extra_hosts:
      - "host.docker.internal:host-gateway"
    healthcheck:
      test: echo "try { rs.status() } catch (err) { rs.initiate({_id:'rs0',members:[{_id:0,host:'host.docker.internal:27017'}]}) }" | mongosh --port 27017 --quiet
      interval: 5s
      timeout: 30s
      start_period: 0s
      #start_interval: 1s
      retries: 30
    volumes:
      - "mongo1_data:/data/db"
      - "mongo1_config:/data/configdb"
  mongo-express:
    image: mongo-express
    restart: always
    ports:
      - 8084:8081
    environment:
      ME_CONFIG_MONGODB_URL: mongodb://mongo1:27017/

volumes:
  mongo1_data:
  mongo1_config:
Quechua answered 3/4 at 9:24 Comment(0)
F
-1

Make sure you're using the updated API - for example:

MongoClient mongoClient = MongoClients.create();
MongoDatabase dataBase = mongoClient.getDatabase("mainDatabase");
MongoCollection<Document> collection = dataBase.getCollection("entities");

Also make sure you have mongo.exe open.

Faroff answered 15/5, 2019 at 14:23 Comment(2)
how is this answer to this issue?Randarandal
@TundePizzle He asked a question which indicated that he used the old API, something that happened to me as well. Don't be too easy on the trigger :)Faroff
C
-2

I disabled TLS (inside Spring Data MongoDB), and now the transaction feature with the developement release 3.7.9 works fine.

Cerargyrite answered 11/5, 2018 at 13:34 Comment(1)
It is already disabled by default in my case. Then also, the error persists.Schlieren

© 2022 - 2024 — McMap. All rights reserved.