How to configure a MongoDB cluster which supports sessions?

前端 未结 6 739
刺人心
刺人心 2020-12-16 17:32

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 cl

6条回答
  •  难免孤独
    2020-12-16 17:50

    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: 
       replSetName: 
       enableMajorityReadConcern: 
    

    and then run details of which are here

    rs.initiate()
    

    after this the connection string would be like below:-

    mongodb://localhost:27017/?replicaSet=
    

    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  mongod --replSet rs0 --port 27017
    

    with volume

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

    for supporting binding of any ip

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

提交回复
热议问题