Setting up MongoDB replica set - mongodb

I have a fast Windows 7 PC with 8Gb RAM. I want to test this MongoDB replica set: http://www.mongodb.org/display/DOCS/Replica+Sets for my development. I dont want to buy 3 PCs though, as it's kind of expensive. Is there a way to use some kind of technology, like Hyper-V, to be able to set it up? If not, how many PC and what kind should I buy?

You can run multiple mongod processes on the same machine on different ports and pointing to different data directories and make them a part of the same replicaset.
http://www.mongodb.org/display/DOCS/Starting+and+Stopping+Mongo
mongod --dbpath c:/data1 --port 12345 --replSet foo
mongod --dbpath c:/data2 --port 12346 --replSet foo
and then connect to one of the mongod processes using the mongo console and add initiate the replica set using instructions outlined here:
http://www.mongodb.org/display/DOCS/Replica+Sets+-+Basics

On Ubuntu 18.04, MongoDb : shell version v4.2.6
In The Terminal (1) (Secure the port using a firewall since we are using 0.0.0.0)
sudo systemctl stop mongod
sudo systemctl status mongod
sudo mongod --auth --port 27017 --dbpath /var/lib/mongodb --replSet rs0 --bind_ip 0.0.0.0
Then open another instance of terminal (2) (Keep the previous one open)
mongo -u yourUserName -p (it will ask for password - follow on)
rs.initiate()
Then open yet another instance of terminal (3)
Here you will run server.js with your connection url like this :
const url = 'mongodb://' + user + ':' + password +
'#localhost:27017/?replicaSet=rs0'
MongoClient.connect(url, { useUnifiedTopology: true, authSource: 'admin' },
function (err, client) {
if (err) {
throw err;
}
});

you can create multiple mongod instances running on the same server on diff. ports.
for configuration and the way replica set works, refer to blog below. this will set up replica set as per the instruction on the same box.
http://pareshbhav.blogspot.com/2014/12/mongdb-replicaset-and-streaming.html

One super easy way is to set the MongoDB replica set by using Docker.
Within our Docker Host, we can create Docker Network which would give us the isolated DNS resolution across containers. Then we can start creating the MongoDB docker containers. They would initially be unaware of each other. However, we can initialise the replication by connecting to one of the containers and running the replica set initialisation command. Finally, we can deploy our application container under the same docker network.
Check out the MongoDB replica set by using Docker post on how to make this work.

There is no use in having replica set at the same single host since this contradicts the terms of redundancy and high availability. If your PC goes down or slows down, your replica set will be ruined or undergo degradation. But for sure it’s not cost-efficient to buy several PCs to evaluate replica set, so you can consider a possible scenario described in MongoDB Replica Set with Master-Slave Replication.
With respect to the number of replica set members, you’re right, the most common topology comprises 3 members, but I would suggest you also adding an Arbiter on the separate host. It is a lightweight process and doesn’t require a lot of resources but it plays an important role in maintaining a quorum in an election of new PRIMARY in case you got an even number of members once PRIMARY fails.

We will configure MongoDB replica set with 3 nodes.
Suposse we have 3 nodes with:
hostname Mongodb01, ip address 192.168.1.11 and MongoDB
installed with port no 27017
hostname Mongodb02, ip address 192.168.1.22 and MongoDB installed with port no 27017
hostname Mongodb03, ip address 192.168.1.33 and MongoDB
installed with port no 27017
Before initiating with this configuration, ensure that you have below points in place:
MongoDB service is installed and running in all the 3 nodes
All the 3 nodes are connected to each other via IP Address or Hostname
The default port nos 27017 and 28017 (or any other port no you are planning to use) are not blocked by any firewall or antivirus
Now, lets begin with configuration
Step 1: Modify the mongodb.conf file of each node to include replica set information.
replSet = myCluster
rest = true
replSet is the unique name of replica set and all the nodes must have same value for replSet parameter. rest is optional but used to enable rest interface for admin web page.
Step 2: Restart MongoDB service on all the 3 nodes
Step 3: Configure replica set on the node you plan to use as primary. In our case we will execute below commands in Mongodb01's mongo shell
rs.initiate()
Initiates replica set
rs.add("<hostname or ip-address>:<port-no>")
Adds secondary node in replica set.
e.g.; rs.add("Mongodb02:27017") or rs.add("192.168.1.22:27017")
rs.addArb("<hostname or ip-address>:<port-no>")
Adds arbiter node in replica set.
e.g.; rs.addArb("Mongodb03:27017") or rs.add("192.168.1.33:27017")
rs.status()
Checks whether all the nodes are added in the replica set. Other way of checking for the nodes in replica is use following URL in your browser address bar http://<hostname or ip-address>:<port>/_replSet
e.g.; http://localhost:27017/_replSet or http://Mongodb01:27017/_replSet or http://192.168.1.11:27017/_replSet.
This URL is accessible only when you set rest = true in mongodb.conf file

Related

how to insert data when one of muliple binding ips is diconnected on mongodb?

I have 2 PC set including 2 lan ports for each PC.
I've made setting for using replSet.
PC No.1
mongod --bind_ip 127.0.0.1,192.168.0.231,192.168.0.241 --replSet repl --dbpath C:\mongodb\data --port 27017
PC No.2
mongod --bind_ip 127.0.0.1,192.168.0.232,192.168.0.242 --replSet repl --dbpath C:\mongodb\data --port 27017
config = {_id : "repl", members : [{_id:0, host:"192.168.0.231:27017", priority:1},{_id:1, host:"192.168.0.232:27017", priority:1}]};
rs.initiate(config);
mongosh --host 192.168.0.241 --port 27017
I can read all data with both IPs(192.168.0.231,192.168.0.241). but i can not receive any result with IP 192.168.0.241 when i use command for "insertOne()" with disconnected with IP 192.168.0.231
and i also can not read any data from my APP with mongo drive with same condition.
plese give your any advice.
I think there are a few different concepts being conflated here resulting in a bit of confusion and inconsistent behavior. The short answer is that the application should be using a connection string for the replica set rather than a standalone server in order to function properly.
The bind_ip setting describes what network interfaces the mongod processes should use to listen for connections. If the members of the replica set are communicating with each other (e.g. rs.initiate(config); was successful) and the client application can also connect to all members of the replica set, then
the binding is properly configured and there are no changes or problems there.
What is happening instead is that you are connecting directly to the members of the replica set individually. Assuming the replica set is functioning properly, one of the two members will be in the PRIMARY state and the other will be a SECONDARY. While both can serve read operations, only the PRIMARY can accept writes. This seems to be similar to the behavior that you are describing.
The fact that Mongosh can read data from both members but your application can only read from one probably relates to read preference. By default, drivers will use a read preference of primary but Mongosh will probably automatically configure reads from other members when connected directly.
Therefore you should configure your application to use an appropriate connection URI. This should specifically include the replica set options as well as read preference if you would like to read from any members. The connection string would probably look something like the following in your situation:
mongodb://192.168.0.231:27017,192.168.0.232:27017/?replicaSet=repl&readPreference=primaryPreferred

Mongodb standalone , listen to CRUD events

I am using standalone Mongodb and want to listen to any CRUD operation performed ,whether by code or done manually in mongo by console/GUI.
I was looking into change stream and mongo stitch , but change streams and mongo stitch are not provided in standalone Mongodb.
It is any event raising mechanism provided in standalone mongodb?
Convert your standalone MongoDB to a single node replica set. In this case, you will still be running with the single MongoDB instance, but instead of running it as standalone you will be running it as a replica set.
Shut down the standalone mongod instance.
Restart the instance. Use the --replSet option to specify the name of the new replica set. For example, the following command starts a standalone instance as a member of a new replica set named rs0.
mongod --port 27017 --dbpath /srv/mongodb/db0 --replSet rs0
Connect a mongo shell to the mongod instance. After connecting run the following command to initiate a replica set.
rs.initiate()
Now, you will be able to make use of the change stream functionality.

MongoDB Sharding - Unable to reach primary for set c1

I have a huge amount of data, therefore I am going to use sharding on MongoDB. I have exactly followed the same steps mentioned here, but getting the warning Unable to reach primary for set c1 when I execute the command mongos --configdb c1/mongodb-test-config on the front-end machine.
Also, when I execute the next command mentioned in the linked tutorial mongo localhost:27017 on the front-end machine, getting the error couldn't connect to server localhost:27017.
Notes in advance:
I have already configured iptables to allow connections on the port 27017
I have set bindIp to 0.0.0.0 in the mongod.conf file
MongoDB Version: 4.0.2
Ubuntu Version: 16.04.5
All the machines (2 shards, 1 config server, and 1 front-end) are located in the same network, and successfully connect with each other.

Unable to launch mongos

I am attempting a simple sharding set up (on a single host without any replica set). However I am unable to go any further because this is what happens when i try to start mongos:
C:\>mongos --configdb localhost:27010 --port 27011
I get:
BadValue: configdb supports only replica set connection string
try 'mongos --help' for more information
I am failing to see what is lacking. I tried mongos --help, but according to that valid arguments for --configdb are <config replset name>/<host1:port>, <host2:port>, etc. But this is what I've done.
I have not done anything else than starting the config server:
mongod --configsvr --port 27010
which is the one I am trying to connect the mongos to.
Any ideas on how this can be resolved?
Thankful for any advice in advance.
You have set up the config server as a standalone mongod process, but as of MongoDB 3.4 that isn't supported: it must be a replicaset:
config servers: Config servers store metadata and configuration settings for the cluster. As of MongoDB 3.4, config servers must be deployed as a replica set (CSRS).
The minimum setup is to have a single mongod process, configured as a 1-member replica set; then your mongos process connects to the replica set:
mongos --configdb replsetname/localhost:27010 --port 27011

Google Cloud Mongo DB: External IP not connecting

I have created a ready to go MongoDB server on Google Cloud using the default parameters. Everything is working fine between them (there is communication and I can add DBs and collections). However, I can't connect to MongoDB on any external machine.
I created the firewall rules in GCP allowing all the connections ("0.0.0.0./0") on the port 27017.
I am running the command:
giuseppe#ubuntu:~$ mongo --host rs0/104.154.xx.xxx,173.255.xxx.xxx,104.197.xxx.xxx
giuseppe#ubuntu:~$ mongo --host rs0/104.154.xxx.xxx:27017,173.255.xxx.xxx:27017,104.197.xxx.xxx:27017
I'm getting the same error on both of them. I don't know how to resolve this issue.
connecting to: rs0/104.154.41.xxx,173.255.xxx.xxx,104.197.22.xxx:27017/test
2015-03-18T19:47:33.770-0500 starting new replica set monitor for replica set rs0 with seeds 104.154.41.xxx:27017,104.197.22.1xx:27017,xx.255.114.xxx:27017
2015-03-18T19:47:33.770-0500 [ReplicaSetMonitorWatcher] starting
2015-03-18T19:47:34.119-0500 changing hosts to rs0/mongo-db-jff3:27017,mongo-db-vnc4:27017 from rs0/104.154.41.246:27017,1xx.197.22.xxx:27017,173.255.1xx.xx:27017
2015-03-18T19:47:34.493-0500 getaddrinfo("mongo-db-vnc4") failed: Name or service not known
2015-03-18T19:47:34.511-0500 getaddrinfo("mongo-db-jff3") failed: Name or service not known
2015-03-18T19:47:34.512-0500 Error: connect failed to replica set rs0/104.154.xxx.xxx:27017,173.2xx.xxx.68:27017,104.197.22.xxx:27017 at src/mongo/shell/mongo.js:148
EDIT:
Here are my firewall settings.
Did you
configure the firewall rule in Google cloud console
provide a tag in your firewall rule
tag your instance with the same tag as the firewall rule
?
I explained how to open a port to the outside world in detail over here. Replace with your own port number.
I belive the issue here is that the ReplicaSetMonitorWatcher is changing hosts to rs0/mongo-db-jff3:27017, where mongo-db-jff3 is not reachable from your network. You need to configure the hosts in the replica set to something that you can reach (static IP or URL).
https://docs.mongodb.com/manual/tutorial/change-hostnames-in-a-replica-set/
Quick example, mongo into your PRIMARY (SECONDARY if you want to do it no downtime):
cfg = rs.conf()
cfg.members[0].host = "mongodb0.example.net:27017"
cfg.members[1].host = "mongodb1.example.net:27017"
rs.reconfig(cfg)