Configuring Master Slave config for MongoDB on single Ubuntu Machine - mongodb

I want to setup Master Slave config for Mongo DB on my Ubuntu Machine. I can see the setting to do this in /etc/mongodb.conf but that would make my DB either Master or Slave.
Is there any way I can run two different server on same machine and use one as Master and other as slave. I want to do this for testing purpose.

The best way would be to not use /etc/init.d/* scripts and use the good old command line. Make sure mongodb is not running on your machine. Locate the folder where binaries are installed, and then from command line run:
./mongod --dbpath /path_to_master_db_files --master --logpath /path_to_logs/master.log --port 27017 --fork
Then open up another terminal, navigate to the same folder and run this:
./mongod --dbpath /path_to_slave_db_files --slave --logpath /path_to_logs/slave.log --port 27018 --source=localhost:27017 --fork
And there you go - you should have a master running on 27017, and slave running on 27018. Hope this helps.
BTW, I am assuming you are not running this configuration in production, and only want to try it out on your local instance.

For testing purposes you can start both instances manually, take a look at this link. Quoting from the docs:
IMPORTANT
Replica sets replace master-slave replication for most use cases. If possible, use replica sets rather than master-slave replication for all new production deployments. This documentation remains to support legacy deployments and for archival purposes only.
Locate the folder where the MongoDB binaries are located:
mkdir -p ../master
./mongod --dbpath ../master --master --logpath ../master/master.log --port 27017
Then open up another terminal, navigate to the same folder and run this:
mkdir -p ../slave
./mongod --dbpath ../slave --slave --logpath ../slave/slave.log --port 27018 --source=localhost:27017

Related

Problems with WiredTiger in order to deploy a config server in MongoDB

I'm trying to deploy a shared and replicated cluster of MongoDB.
I did the installation like it is indicated in the official page for Ubuntu 20.04: https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-ubuntu/.
But, when I executed:
mongod --configsvr --port 27001 --replSet config_server --dbpath mongo/data_patients/config/ --bind_ip localhost
I have the following trouble:
and I can't access to the configuration server.

Start replSet automatically

I am pretty new to installing MongoDB on Server. It was for a MERN stack project. And in order to use a library Pusher. I need to convert the Stand Alone Server to Replication set.
So I used the script sudo mongod --port 27017 --dbpath /var/lib/mongodb --replSet rs1 --bind_ip localhost,<my server ip> and the terminal will start logging. And then I can use another terminal to access the MongoDB database. But the problem is the moment I close the logging terminal (where I used the --replSet script) I will be prevented from accessing the MongoDB database.
So is there any way to automatically execute the --replSet script as soon as the server starts and keep it running?

How to disable remote connections to MongoDB?

Normally the answer to question is to set:
bindIp: 127.0.0.1
I have this set in /etc/mongod.conf. Unfortunately I am still allowed access to this database remotely. I have restarted the Mongo service a couple times, to no avail.
Does anyone have an idea as to why my database is still accessible remotely?
I'm using MongoDB version 3.0.9
Remoting in to mongod clients using bindIp = 127.0.0.1 is possible through an SSH tunnel because the shell session is seen as 127.0.0.1.
Enabling bind_ip = 127.0.0.1 should be sufficient. Restart MongoDB server after the changes are done.
References:
http://greenwireit.com/it-tech-support-articles/enable-remote-access-default-mongodb-installation/
https://www.mkyong.com/mongodb/mongodb-allow-remote-access/
http://wptrafficanalyzer.in/blog/enabling-and-disabling-remote-access-to-a-mongodb-server/
Perhaps you must specify the mongodb.conf file when loading your mongod instance. Like so:
mongod --fork --config /etc/mongodb.conf --logpath mongodblogs/mongodb.log --dbpath mongod
This is best variant in security aspect:
su <NOTROOTUSER>
mongod --dbpath data --bind_ip localhost
Create new user on you server then log in.
Root is not recommended for running mongo server.

How to run multiple mongod instances in windows?

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

How start Mongo Instance in sharded cluster

I want to deploy a sharded cluster for this i using MongoDB manual.
first i created 3 config servers.
mongod --configsvr --dbpath /data/configdb1 --port 27019 ,
mongod --configsvr --dbpath /data/configdb2 --port 27019 ,
mongod --configsvr --dbpath /data/configdb3 --port 27019
But in second step to Start the mongos Instances i find a command like
mongos --configdb cfg0.example.net:27019,cfg1.example.net:27019,cfg2.example.net:27019
but i can't understand the meaning of
cfg0.example.net
cfg1.example.net
cfg2.example.net
So Please explain meaning of above command and how can i use this according my hostname?
Please check "Deploy a Sharded Cluster", cfg[0-3].example.net means hostnames of 3 config servers, each of mongos server should be started with all config servers configured.
This should solve your issue
[ne#server1~]$mongos configdb"repl/localhost:57040,localhost:57041,localhost:57042" --fork --logpath log.mongos0 --port 27200
about to fork child process, waiting until server is ready for connections.
forked process: 2467
Hope it helps!!!
These are are the config servers connected to the the mongos server