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.
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.
MongoDB supports replica sets, which can have up to 50 nodes.
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.
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.
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:
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.
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
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
enter the command: systemctl start mongod
enter MongoDB shell with the command mongo
, enter the command rs.initiate()
now you should be in your replicaSet database.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With