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
i am new to mongo db. And i do not understand why my monggo db is not starting, and instead it is shutting down.
I have an image below, that shows what is going on at the cmd
cmd, mongo db
Kill all other mongod instance before starting a new instance. The problem is clearly written in the cmd.
Or try to start mongo on another port using
mongod --port 27010 --dbpath c:\data\newDb
Also, make sure you have c:\data\newDb and c:\data\db folders created before starting the server.
I am trying to run mongod instances on port 27018 and 27019, while port 27017 already running using windows command line. However,I am unable to start as it is terminating saying, already a mongod instance is running. How do i achieve running multiple instances ?
You just need to create another folders
MongoDB2
MongoDB3
dbpath for the second and third instance,
and run it in different port(27018,27019)
The dbPath value controls the location of the mongod instance’s data directory. Ensure that each database has a distinct and well labeled data directory.
Also Refer doc
mongod --dbpath /usr/local/var/MongoDB2 --port 27018
If I'm writing an application which connects to mongodb then I can provide a seed list for a replicaset, and the driver will direct me to the master node, where I can run write commands.
How do I specify the seed list for a commandline mongo shell in order to conenct to a replicaset.
To connect to a replica set Primary use the mongo shell --host option:
mongo --host replicaSetName/host1[:porthost1],host2[:porthost1],host3[:porthost3],etc
For example:
$ mongo --host rs1/john.local:27019,john.local:27018
MongoDB shell version: v3.4.9
connecting to: mongodb://john.local:27019,john.local:27018/?replicaSet=rs1
2017-10-12T14:13:03.094+0000 I NETWORK [thread1] Starting new replica set monitor for rs1/john.local:27019,john.local:27018
2017-10-12T14:13:03.096+0000 I NETWORK [thread1] Successfully connected to john.local:27019 (1 connections now open to john.local:27019 with a 5 second timeout)
2017-10-12T14:13:03.096+0000 I NETWORK [thread1] Successfully connected to john.local:27018 (1 connections now open to john.local:27018 with a 5 second timeout)
rs1:PRIMARY> db
test
rs1:PRIMARY>
Note: From versions 3.4.2 to 3.4.10 there was a bug (SERVER-28072) which prevented specifying the db after when using --host or --port.
The answers above are for Mongo 3.2.
According to Mongo 3.4 documentation, the shell was changed a bit:
In 3.2:
mongo --host host1,host2,host3/myRS myDB
or,
mongo --host host1:27017,host2:27017,host3:27017/myRS myDB
In 3.4:
mongo "mongodb://host1,host2,host3/myDB?replicaSet=myRS"
or,
mongo "mongodb://host1:27017,host2:27017,host3:27017/myDB?replicaSet=myRS"
All you have to do is to use --host and give it one of your hosts in the replicaset but with the name of the replicaset as a prefix.
For example:
mongo --host my_mongo_server1
will connect to my_mongo_server1, it may just be yet another SECONDARY node.
But:
mongo --host my_repl_set_name/my_mongo_server1
will always connect to the PRIMARY node in the replica set, even if it's not my_mongo_server1.
Why? The answer is "replica set monitor".
In the example above, mongo shell would connect to the specified node, start a new replica set monitor for the replica set and will use the specified node just to seed it. From there, the monitor will figure out all nodes in the replica set and will switch the connection to the PRIMARY node.
Hope that helped.
You can use the "name/seed1,seed2,..." format:
> conn = new Mongo("myReplicaSet/A:27017,B:27017,C:27017")
> db = conn.getDB("test")
This should give you a connection to whichever node is currently primary and handle failover okay. You can specify one or more seeds and it'll find the rest.
Note that (AFAIK) the shell does not allow you to route reads to secondaries with a replica set connection.
To the best of my knowledge, the mongo command line client will not accept seeds to forward you to the master node, because you may often want to actually operate on the secondary node rather than being forwarded.
However, once connected to any node in the RS, you can discover the RS topology via rs.config() or db.isMaster(). You could then use this information to reconnect to the primary node. Depending on your shell, you might be able to use mongo --eval "db.isMaster()['primary']" to automatically connect to the master.
In the shell, you can first use:
mongo --nodb
to open a mongo session without connecting to mongo replicaset
Then, like kristina said, then you should be able to use
conn = new Mongo("myReplicaSet/A:27017,B:27017,C:27017")
to connect to a replicaset.
Or eventually put
conn = new Mongo("myReplicaSet/A:27017,B:27017,C:27017")
in your js file and
mongo --nodb yourcode.js
You can use the --host param to specify the replSet name and seed list, then mongo will automatically connect to the current primary host.
example:
mongo --host rs0/1.example.com:27017,2.example.com:27017,3.example.com:27017 [dbname]
Building on the answer by Chris Heald these two bash aliases let me connect to the master with one command (where db1.test.test is one member of the replica set, acme is the database name, mreppy is my account etc) It will fail of course if db1 is down, but it's still handy.
alias whichprimary='mongo db1.test.test/acme --username mreppy --password testtest --quiet --eval "db.isMaster()['"'primary'"']"'
alias connectprimary='mongo -u mreppy -p testtest `whichprimary`/acme'
The quoting in the eval alias is hard, I used How to escape single-quotes within single-quoted strings? for help :-)
I am using v3.4. Also new to mongodb stuff...
Although the help info from "man mongo" suggests to use "--host replicaSet/host:port,host:port" url, it does not work for me.
However, I can connect to my replicaSet according to official document, as below:
$ mongo "mongodb://c1m,c2m,c3m/?replicaSet=rs0"
MongoDB shell version v3.4.1
connecting to: mongodb://c1m,c2m,c3m/?replicaSet=rs0
2017-02-08T14:46:43.818+0800 I NETWORK [main] Starting new replica set monitor for rs0/c1m:27017,c2m:27017,c3m:27017
MongoDB server version: 3.4.1
Server has startup warnings:
2017-02-08T13:31:14.672+0800 I CONTROL [initandlisten]
2017-02-08T13:31:14.672+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-02-08T13:31:14.672+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2017-02-08T13:31:14.672+0800 I CONTROL [initandlisten]
rs0:PRIMARY>
So I guess the man page of my mongo is outdated (I am using CentOS 7.3).
mongodb://< dbuser >:< dbpassword >#example.com:< port >,example2.com:< port >/< dbname >?replicaSet=setname
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