How to configure the configuration file for MONGOS? - mongodb

I am not able to cofigure the configuration file for mongos. How do i set the configurations?

Step 1) Create a separate database for the config server.
mkdir /data/configdb
Step 2) Start the mongodb instance in configuration mode. Suppose if we have a server named Server D which would be our configuration server, we would need to run the below command to configure the server as a configuration server.
mongod –configdb ServerD: 27019
Step 3) Start the mongos instance by specifying the configuration server
mongos –configdb ServerD: 27019
Step 4) From the mongo shell connect to the mongo's instance
mongo –host ServerD –port 27017
Step 5) If you have Server A and Server B which needs to be added to the cluster, issue the below commands
sh.addShard("ServerA:27017")
sh.addShard("ServerB:27017")
Step 6) Enable sharding for the database. So if we need to shard the Employeedb database, issue the below command
sh.enableSharding(Employeedb)
Step 7) Enable sharding for the collection. So if we need to shard the Employee collection, issue the below command
Sh.shardCollection("db.Employee" , { "Employeeid" : 1 , "EmployeeName" : 1})

Related

MongoDB replication as a service on windows

I am trying to configure a replica set on windows as a service, means that even the PC will restart the mongod will run again automatically.
problem is that I run the mongod like this:
mongod --dbpath "C:\Program Files\MongoDB\Server\4.2\data" --logpath "C:\Program Files\MongoDB\Server\4.2\log\mongod.log" --port 27017 --storageEngine=wiredTiger --journal --replSet test_replica
And once I close the CMD running this command the service is killed. How do I run it correctly then?
Also, currenctly the service is navigating to the default cfg file but I see the replication there is marked with # (so the service is running as standalone). and when I try to add to replication the replSet: test_replica it won't start anymore.
You should put all your parameters into a configuration file. Then you can create the service like this:
mongod.exe --config c:\MongoDB\config\mongod.cfg --install
For a replica set you typically create several services, not just one. It is possible to run a replica set with just one member, however this is quite useless. Of course, when you create several services then each one needs his own config file (and also his own dbPath, port, etc.)
The next time your PC will boot, the mongo service should also start. Or start it manually with command net start <mongo service name>
You should install mongodb as windows service. Read the guide from the official documentation
Setting --replSet from command line or replication:replSetName in configuration file is not enough. Read this guide, in short: after mongodb process is started in replica set mode, you should run rs.initiate() in mongo shell.

setting up config server setup for mongo query router

I am using mongo v3.2 - I already have the config replica set as well as two shard replica sets running. When I try to launch the mongos (query router) with the test config file setting below, I get the error copied below - any ideas on how to fix this?
sharding:
configDB: config-set0/127.0.0.1:27019,127.0.0.1:27020,127.0.0.1:27021
error
Unrecognized option: sharding.configDB
I can see this setting in the mongodb docs at the URL below:
https://docs.mongodb.com/manual/tutorial/deploy-shard-cluster/
Ensure that the process is being launched as mongos and not mongod if the intent is to run the process as a query router (and not a config server or a shard).
Use this command to start the mongos.
sudo mongos -f <-- location of conf file -->

Minimum number of config servers needed for Mongo Cluster

I am trying to test sharding in MongoDB cluster.
I am folllowing MongoDB documentation .
They suggest to use 3 config servers. Can I set up a cluster with 1 or 2 servers? Or I need 3 or more config servers?
From the MongoDB docs:
The config server processes are mongod instances that store the cluster’s metadata. You designate a mongod as a config server using the --configsvr option. Each config server stores a complete copy of the cluster’s metadata.
Hence for test setup 1 config server should be fine , but for production it is recommended to use replicated 3 config servers.

mongodb cluster:after use keyFile for authentication ,it report socket exception for shard

I use mongodb --keyfile parameter to config a authenticated mongodb cluster.Yes, I at first add an admin user to admin db without the authentication ,and then ,I restart the mongodb with --keyFile.Yes, after that , the authentication takes effect.But when I tried to addUser() or show dbs or show collections or db.collection.find(), it reports error:
mongos> db.system.users.find()
error: {
"$err" : "socket exception [CONNECT_ERROR] for shard4/192.168.10.10:10004,192.168.10.12:10004",
"code" : 11002,
"shard" : "config"
}
sometimes, it is shared1,sometimes it is shard2 or shard3...
I checked every shard heath(I have 3 shards ,each shard have 3 replication set), all shard member's health status is 1,namely ok.
So ,any one can help me?
I checked the log on /var/data/master/log.log, it said:
the permission of keyfile is too open.That means,I shouldn't have given such a file too high permission.So ,I run command below for every mongodb cluster member:
sudo chmod 700 /var/data/keyfile
And I checked the log again , the permission problem solved,but the socked exception error remains.
But I can ping to these config server net port successfully,it proves that actually these config server is working properly.
So ,why these config server cannot be connected ? Finally I get the reason: it is because I firstly start the mongos process and then the 3 config servers' mongod instance .When mongos is started , it is trying to connect to the config server ,but the config server is started afterwards,so ,it cannot reach them.Only when I started these 3 config server at first and then started the mongos instance ,problem solved!

Cannot upgrade sharded mongoDB or stop the balancer

mongos is not running in the beginning. When tried to start the mongos I see the following log:
Fri Mar 22 17:43:13.383 [mongosMain] ERROR: error upgrading config
database to v4 :: caused by :: newer version 4 of mongo config
metadata is required, current version is 3, need to run mongos with
--upgrade
But with --upgrade parameter, I see the following log:
Fri Mar 22 17:43:39.273 [mongosMain] ERROR: error upgrading config
database to v4 :: caused by :: balancer must be stopped for config
upgrade
Now the problem is: I cannot stop the balancer by sh.stopBalancer() because I cannot start mongos. It's a deadlock to me now. Please help.
I found the the problem. I should connect to port 27019 for a configsrv. In this way I don't need to start mongos. Instead the sh.stopBalancer() could be executed simply in mongo interpreter.
I had the same problem just now. I updated my mongo database from verion 2.0.4 to 2.4.3.
I cannot connect to mongos because config server need to be upgraded. However, I can not stop the balancer using command stopBalancer() because my mongos is inactive. I did not find other solutions from stackoverflow. I tried many times.
My solution is:
1, ssh to the config server;
2, use config database;
maybe need to pass authentication;
3,
run db.settings.update( { _id: "balancer" }, { $set : { stopped: true } } , true )
to stop balancer;
4, I can run "mongos" with --upgrade option;
Assuming you followed the recommendation to always run 3 config server, I would try these steps:
Make sure all other mongos clients are stopped. If no mongos are running, no balancer should be keeping a lock either.
If you still get the error (after being certain that no mongos are connected to the config servers) I would stop all config servers but one and clear any remains of balancer locks in the admin database. After a successful try with this config server, I would reset the other two. If it was not successful, you still have two other copies.
I had the same problem. The solution for me was to connect to the configsvr with mongo shell
mongo --host ip_of_config_server_host --port 27019
and setting the balancer off from there with
sh.setBalancerState(false)
After this I could do the config server upgrade with
mongos --port 27017 --configdb ip_of_config_server_host --upgrade