Juergen Zimmermann
Juergen Zimmermann

Reputation: 2222

How to configure a MongoDB cluster which supports sessions?

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]

Upvotes: 14

Views: 25668

Answers (7)

Marius Tanasoiu
Marius Tanasoiu

Reputation: 701

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:

Upvotes: 0

Mukundhan
Mukundhan

Reputation: 3487

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

Upvotes: 8

Atul
Atul

Reputation: 3377

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()

Upvotes: 0

kakabali
kakabali

Reputation: 4033

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

Upvotes: 6

Piyush Verma
Piyush Verma

Reputation: 399

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/

Upvotes: 15

Guy
Guy

Reputation: 155

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.

Upvotes: -1

Juergen Zimmermann
Juergen Zimmermann

Reputation: 2222

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

Upvotes: -2

Related Questions