Start replSet automatically - mongodb

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?

Related

Mongo db not start

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.

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

Mongodb problem_could not connect to server

I use ubuntu 14.04LTS and with mongodb version 2.4.9.When I type "mongo" in terminal, it always show "connecting to: test
Tue Jun 3 10:03:57.911 Error: couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:145
exception: connect failed".
But if i run mongod in one terminal, and run mongo in another, all is ok.
If I close the window of mongod, the connection fail again.
I need to use mongodb without open a terminal of mongod, how can I do with it ?
Thank you!
You need to start mongodb as a service or daemon(depending on the OS,in your case daemon), check the documentation
Documentation
sudo service mongod start
Or
To run a mongod process as a daemon (i.e. fork), and write its output to a log file, use the --fork and --logpath options. You must create the log directory; however, mongod will create the log file if it does not exist.
The following command starts mongod as a daemon and records log output to /var/log/mongodb.log.
mongod --fork --logpath /var/log/mongodb.log

Configuring Master Slave config for MongoDB on single Ubuntu Machine

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