Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongodb v4.0 Transaction, MongoError: Transaction numbers are only allowed on a replica set member or mongos

I've installed MongoDB v4.0 for the most amazing feature of it Transaction in Nodejs with mongodb 3.1 as a driver.

When I try to use a transaction session I've faced this error:

MongoError: Transaction numbers are only allowed on a replica set member or mongos.

What's that and how can I get rid of it?

Any suggestion is appreciated.

like image 680
jafar shemshadi Avatar asked Jul 22 '18 04:07

jafar shemshadi


People also ask

What is replica set in MongoDB?

A replica set in MongoDB is a group of mongod processes that maintain the same data set. Replica sets provide redundancy and high availability, and are the basis for all production deployments. This section introduces replication in MongoDB as well as the components and architecture of replica sets.

What is the maximum number of nodes in a MongoDB replica set?

MongoDB supports replica sets, which can have up to 50 nodes.

What is MongoDB transaction?

For situations that require atomicity of reads and writes to multiple documents (in a single or multiple collections), MongoDB supports multi-document transactions. With distributed transactions, transactions can be used across multiple operations, collections, databases, documents, and shards.


2 Answers

Transactions are undoubtedly the most exciting new feature in MongoDB 4.0. But unfortunately, most tools for installing and running MongoDB start a standalone server as opposed to a replica set. If you try to start a session on a standalone server, you'll get this error.

In order to use transactions, you need a MongoDB replica set, and starting a replica set locally for development is an involved process. The new run-rs npm module makes starting replica sets easy. Running run-rs is all you need to start a replica set, run-rs will even install the correct version of MongoDB for you.

Run-rs has no outside dependencies except Node.js and npm. You do not need to have Docker, homebrew, APT, Python, or even MongoDB installed.

Install run-rs globally with npm's -g flag. You can also list run-rs in your package.json file's devDependencies.

npm install run-rs -g 

Next, run run-rs with the --version flag. Run-rs will download MongoDB v4.0.0 for you. Don't worry, it won't overwrite your existing MongoDB install.

run-rs -v 4.0.0 --shell 

Then use replicaSet=rs in your connection string.

You find more details about it here.

like image 85
Majid Parvin Avatar answered Sep 17 '22 16:09

Majid Parvin


I got the solution, and it's just three lines configuration inside the MongoDB config file.

After switching from MongoDB atlas and installing MongoDB v 4.4.0 on my CentOS 7 VPS with WHM, I faced that issue also.

the run-rs solution does not work for me, but I managed to solve this issue without any third-party tool, following these steps:

1. turn off mongod.

the most efficient way is by entering the MongoDB shell with the command mongo checkout the method

db.shutdownServer() 

You will be no ability to use the MongoDB server. For me, the shutdown process took too long, and then I killed the process with the command:

systemctl stop -f mongod 

if you killed the mongod process,s probably you will need to run mongod --dbpath /var/db --repair

The var/db should point to your database directory.

2. setting replicaSet configuration.

for the replicaSet settings step, check out the /etc/mongod.conf file, look for the replication value line, and you should add the following lines as below:

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

use the replSetName value on the next step.

an example of those settings:

   oplogSizeMB: 2000    replSetName: rs0    enableMajorityReadConcern: false 

3. add your connection string URL.

add the value of replSetName to your connection URL &replicaSet=--YourReplicationSetName--

if you used the name rs0 from our example, then you should add to your DB connection URL query replicaSet=rs0

4. turn on mongod again

enter the command: systemctl start mongod

5. Access your replicaSet database

enter MongoDB shell with the command mongo, enter the command rs.initiate() now you should be in your replicaSet database.

like image 43
Oren Hahiashvili Avatar answered Sep 17 '22 16:09

Oren Hahiashvili