How to deploy three config server instances for sharding in mongodb? - mongodb

I am a newbie in MongoDB. Also I have not much knowledge in networking and servers. I am trying to deploy sharded cluster in mongodb using this article.
It says I need to deploy three configuration server instances and create data directories for each.
I can create a data directory using this command
mkdir /data/configdb
But how do I do this for all three of them?
Also I want to do this in only one machine.

As dev mentioned, for testing purposes a single config server is pretty much enough, and three config servers should be considered for production deployment.
But anyway, there is no problem creating separate directories for each config server on one machine. As you have mentioned, yes you can create directories, but data directory for any mongo instance (mongod, configsvr, etc.) is not limited to any path. So that means you can create separate directories and launch mongod config servers by specifying path for each:
$ mkdir /data/configdb1
$ mkdir /data/configdb2
$ mkdir /data/configdb3
$ mongod --configsvr --dbpath /data/configdb1 ...
$ mongod --configsvr --dbpath /data/configdb2 ...
$ mongod --configsvr --dbpath /data/configdb3 ...

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 path for replica set different from root in windows doesnt work

As I created replicaSet outside of /data/db folder by mistake, so I would like to set path to folder which is not root where I created replica set, instead of starting again.
I tried this(inside mongodb-instance folder is 3 replica folders):
"D:\MongoDB\Server\3.4\bin\mongod.exe" --dbpath D:\mongodb-instance
then I try to run It:
mongod --replSet "rs0"
but still got the same problem:
Data directory C:\data\db\ not found.
For replication, you need to setup multiple servers. Then each of them will have their own data folders.
Then start the servers using command
mongod --host <hostname> --port <portNum> --replSet "nameOfReplicaSet"
Once all the servers are running you'll have to initiate the replication by connecting to any one of them and execute the rs.initiate(replConfig) command with whatever configuration you need.
You can refer to the docs here.

Start Mongodb server in two modes parallely

I have installed mongo db in my local system, i am aware that at any point in time we can start the mongo using mongod service.
in normal mode which will run on port 27017
in rest API mode where we can query to collections and db's which normally runs on mongo port + 1000
i want to start both mode together, any help would be appreciated.
Thanks
Amit
You should add modify your mongod config file to enable http.
add following config line, see https://docs.mongodb.org/manual/reference/configuration-options/
net:
http:
enabled: true
or add parameter in the command line
mongod --httpinterface
You can start multiple instances of mongod. You just have to make sure that they're using different ports and different dbpaths.
To run two separate instances of mongod
mongod
This will start a mongod instance on port 27017 and use dbpath /data/db
Start another command prompt and type in
mongod --port 27018 --dbpath /data/db2
Just make sure that you have a folder named db2 inside your data folder in your c drive. That's where it stores the data.
Additionally, if you're on Node.js, the MongoDB Node.js driver provides a server method where you can start a mongod instance programatically.
var mongo = require("mongodb");
var server = new mongo.Server('localhost', 27017, { auto_reconnect : true} );
This will create a server in what you are calling the Rest API mode.
And then you can simply start mongod from command prompt specifying some other port and dbpath.
I'm not sure which we can start two instance of mondo or not!. but try this:
Run one of then through service mongod start and second one directly by running binary file (e.g. /usr/bin/mondodb). (also you can run both of them directly from CLI.)
In second mode, you most give appropriate parameter for mondo (e.g. path for config file). If you don't know how to give/pass parameters to mondo's binary file, see man mongodb or go and read /etc/init.d/mongod (in Debian based distributions), it give you useful information)
Unfortunately I don't have an installed mongoDB on my machine, so I can't give you exact commands.

How to run mongodb on AWS

I'm looking for a little direction on how to set up services on AWS. I have an application that is build with Node.js and uses mongodb (and mongoose as the ODM). I'm porting everything over to AWS and would like to set up an autoscaling group behind a load balancer. What I am not really understanding, however, is where my mongodb instance should live. I know that using DynamoDB it can be fairly intuitive to set up to work with that, but since I am not, my question is this: Where and how should mongo be set up to work with my app? Should it be on the same ec2 instance with my app, and if so, how does that work with new instances starting and being terminated? Should I set up an instance dedicated only for mongo? In addition, to that question, how do I create snapshots and backups of my data?
This is a good document for installing MongoDB on EC2, and managing backups: https://docs.mongodb.org/ecosystem/platforms/amazon-ec2/
If you aren't comfortable doing all this yourself you might want to also look into MongoLab which is a MongoDB as a Service that can run on AWS.
Your database should definitely be in a separate instance than your app, from all aspects.
A very basic tier based application should comprise of the app server cluster in a scaling group behind a load balancer - in a public subnet, and a separate cluster (recommended in a different subnet which is not publicly accessible), which your app cluster will speak to. whether to use an ELB for Mongo or not actually depends on your mongo config (replica set).
In regards to snapshots (assume this will only be relevant for your DB), have a look at this.
You can easily install MongoDB in AWS Cloud 9 by using the below process
First create Cloud 9 environment in AWS then at the terminal
ubuntu:~/environment $ At the terminal you’ll see this.
Enter touch mongodb-org-3.6.repo into the terminal
Now open the mongodb-org-3.6.repo file in your code editor (select it from the left-hand file menu) and paste the following into it then save the file:
[mongodb-org-3.6]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/amazon/2013.03/mongodb-org/3.6/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc
* Now run the following in your terminal:
sudo mv mongodb-org-3.6.repo /etc/yum.repos.d
sudo yum install -y mongodb-org
If the second code does not work try:
sudo apt install mongodb-clients
Close the mongodb-org-3.6.repo file and press Close tab when prompted
Change directories back into root ~ by entering cd into the terminal then enter the following commands:
“ubuntu:~ $ “ - Terminal should look like this.
sudo mkdir -p /data/db
echo 'mongod --dbpath=data --nojournal' > mongod
chmod a+x mongod
Now test mongod with ./mongod
Remember, you must first enter cd to change directories into root ~ before running ./mongod
Don't forget to shut down ./mongod with ctrl + c each time you're done working
-if this error pops up while using command mongod
exception in initAndListen: IllegalOperation: Attempted to create a lock file on a read-only directory: /data/db, terminating
Then use the code:
sudo chmod -R go+w /data/db
Reference

Is it possible to install mongodb without root privilages?

How can I install mongodb on a Linux server without root privileges? is this possible? What would the steps be? Is there a way to configure mongo to not use /data/db?
You may download the binaries from MongoDB website.
There are a lot of options for the command mongod to change the default behavior, one of these is:
--dbpath arg directory for datafiles
Based on comments:
But it is preferable to specify all configurations in a configuration file and just invoke mongod with:
--config filename