MongoDB Sharding - Unable to reach primary for set c1 - mongodb

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.

Related

mongo docker cluster update 3.4.1 - 3.6.6

I can't update mongo. mongo consists of three docker containers. now mongo version is 3.4.1, need to upgrade to 3.6.6.
To update, I execute the command on the master node db.adminCommand( { setFeatureCompatibilityVersion: "3.4" } ).
Then in the service I change the version of the image from 3.4.1 to 3.6.6.
Then I restart the daemon and service. Containers with the new mongi version are being raised. Containers are all secondary. The main node is not defined.
If I run the command rs.status().members in response I receive "lastHeartbeatMessage" : "Connection refused",. Checking via telnet is available. It was not possible to start the replica set manually.
After the update, I saw the message:
WARNING: This server is bound to localhost.
Remote systems will be unable to connect to this server.
Start the server with --bind_ip <address> to specify which IP
addresses it should serve responses from, or with --bind_ip_all to
bind to all interfaces. If this behavior is desired, start the
server with --bind_ip 127.0.0.1 to disable this warning.
I tried to add conf bind ip to mongo, but mongo with such a config could not start, I could not figure it out.
What am I doing wrong when updating?

Cannot access local MongoDB from sam local

I have Windows 10 machine where MongoDB is installed. I can connect it from a command line. I run NodeJS app with sam local. When I use a production environment, the app can access Mongo Atlas cloud instance. But when I switch to a dev environment with localhost MongoDB it fails to connect.
The sam command starts Docker so it is clear why it cannot connect Mongo running on windows localhost. I found relevant question: From inside of a Docker container, how do I connect to the localhost of the machine?. The problem is that I still cannot connect my local MongoDB, even if I try:
"MONGODB_URI": "mongodb://docker.for.win.localhost:27018/bud?retryWrites=true&w=majority"
or
"MONGODB_URI": "mongodb://host.docker.internal:27018/bud?retryWrites=true&w=majority"
Error:
Request failed { MongoNetworkError: failed to connect to server [docker.for.win.localhost:27018] on first connect [MongoNetworkError: connect ECONNREFUSED 192.168.65.2:27018]
Has anybody faced this issue as well and overcome it? Mongo is installed directly to windows, not in Docker.
If MongoDB is installed and running directly from windows, it should be accessible via localhost:27017. Default port for mongod is 27017, as described in mongoDB documentation page.
Try using:
"MONGODB_URI": "mongodb://localhost:27017/bud?retryWrites=true&w=majority"
If you are using NETWORKS_DRIVER other than bridge for your NodeJS docker container, which is set by default. Refer to Docker Network drivers
Other cases:
The default port for mongod is 27018 when running with --shardsvr command-line option or the shardsvr value for the clusterRole setting in a configuration file.
The default port for mongod is 27019 when running with --configsvr command-line option or the configsvr value for the clusterRole setting in a configuration file.
Remember, that localhost (or any name) is just for your convinience. Tcp stack works on ip addresses. If you configure dns service (e.g. via hosts file) to resolve name to 127.0.0.1 for container it doesn't mean your host, but 127.0.0.1 points to the container, always.
You could make mongo service to listen on your main ip and use it for docker app, but you can also leverage hyper-v virtual network cards and setup mongo to listen not only on host's loopback interface, but also on the virtual one and give docker app ip of that interface. It remains on your virtual lan, therefore it's not exposed to public. However, windows firewall might block it, so make sure you set it up as private network (it will be marked as unidentified and by default is public, which usually has stuff blocked).

Unable to connect specific mongo instance port

Today morning i'm able to create 2 different mongo instances on port 27010 and 37010 and i'm able to replicate using mongo-connector. But now after system restart i'm not able to connect to port 37010 using below command.
C:\Program Files\MongoDB 2.6 Standard\bin>mongo --port 37010
MongoDB shell version: 2.6.12
connecting to: 127.0.0.1:37010/test
2016-09-12T18:08:14.733-0500 warning: Failed to connect to 127.0.0.1:37010, reason: errno:10061 No connection could be made because the target machine actively refused it.
2016-09-12T18:08:14.756-0500 Error: couldn't connect to server 127.0.0.1:37010 (127.0.0.1), connection attempt failed at src/mongo/shell/mongo.js:148
exception: connect failed
It is working fine if i just give mongo where it is connecting only to default port of 27010. Morning also i faced same problem where i deleted all my mongo windows service and running without mongo windows service. But again i'm facing same problem. Kindly advise.
Below is my config files.
mongo.config.txt
##store data here
dbpath=C:\Program Files\MongoDB 2.6 Standard\data
##all output go here
logpath=C:\Program Files\MongoDB 2.6 Standard\log\mongo.log
logappend=true
#port number
port=27017
##log read and write operations
diaglog=3
#replica set name
replSet=rs1
# only run on localhost for development
bind_ip = 127.0.0.1
mongo2.config.txt
##store data here
dbpath=C:\Program Files\MongoDB 2.6 Standard\data2
##all output go here
logpath=C:\Program Files\MongoDB 2.6 Standard\log2\mongo.log
logappend=true
#port number
port=37017
##log read and write operations
diaglog=3
#replica set name
replSet=rs2
# only run on localhost for development
bind_ip = 127.0.0.1
I got answer to my problem by following the steps mentioned in below blog.
http://zdk.github.io/create-a-three-member-mongodb-replica-set
Somehow it didn't work for me with configuration file. But I resolved my problem by executing commands in command line .

Setting up MongoDB replica set

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

Trouble connecting to Mongo Config Server from mongos

Folks,
I am setting up a MongoDB Cluster. The problem is when I am starting up 'mongos' it fails to connect to Mongod Config Server. If I start 'mongos' from localhost with same server as Mongod Config Server it connects & works.
localhost-$ mongos --port 30000 --configdb server-02:20000 [Works]
server-01-$ mongos --port 30000 --configdb server-02:20000 [Does Not Work]
Since from localhost I am able to start mongos by connecting to mongo config server & execute queries, it appears that config server is running fine.
Not able to understand what prevents servers to connect to one of their peers. When I attemped to connect first time from localhost, I got a prompt from OS asking whether process 'mongos' should be allowed to accept incoming connections for which I clicked allow. Perhaps if server fails silently because of this permission issue, with no way to prompt.
Error I recieve:
Sun May 8 01:14:46 ./mongos db version v1.8.1, pdfile version 4.5 starting (--help for usage)
Sun May 8 01:14:46 git version: a429cd4f535b2499cc4130b06ff7c26f41c00f04
Sun May 8 01:14:46 build sys info: Linux bs-linux64.10gen.cc 2.6.21.7-2.ec2.v1.2.fc8xen #1 SMP Fri Nov 20 17:48:28 EST 2009 x86_64 BOOST_LIB_VERSION=1_41
uncaught exception in mongos main:
11002 socket exception [6] server [server-02:20000] mongos connectionpool error: couldn't connect to server server-02:20000
Well I did try clean install yet the problem persisted, so I moved my mongos to a different server with new install & it appears to connect to config server without any issue. Apparently there is some sort of socket contention issue with first server I tried to run mongos.
In all the directories of your mongodb's datas, you have a mongo.lock file. If this file is not empty, it seems to mean that one instance of mongodb executable (config, shard data server) is already running.
Just delete this file in each directory if not empty.
Check your bindip value, which is used by your mongo service. Take out the bindip flag. It works perfectly.