Issue with persistent mongoDB data on EC2 - mongodb

I'm trying to store data in a mongoDB database on Amazon EC2. I'm using starcluster to configure and start the EC2 instance. I have an EBS volume mounted at /root/data. I installed mongoDB following the instructions here. When I log in to the EC2 instance I am able to type mongo, which brings me to the mongo shell with the test database. I have then added some data to a database, let's say database1, with various collections in it. When I exit the EC2 instance and terminate it, using starcluster terminate mycluster, and then create a new, different instance, the database1 data is no longer shown in the mongo shell.
I have tried changing the dbpath in the /etc/mongodb.conf file to /root/data/mongodb, which is the EBS volume, and then start and stop the mongodb service using sudo service mongodb stop and sudo service mongodb start. I then try mongo again and receive
MongoDB shell version: 2.2.2
connecting to: test
Sat Jan 19 21:27:42 Error: couldn't connect to server 127.0.0.1:27017 src/mongo/shell/mongo.js:91
exception: connect failed
An additional issue is that whenever I terminate the EC2 instance any changes I made to the config file disappear.
So my basic question is: how do I change where mongoDB stores its data on EC2 so that the data will remain when I terminate one EC2 instance and then start another EC2 instance.
Edit:
In response to the first answer:
The directory does exist
I changed the owner to mongodb
I then issued the command sudo service mongodb stop
Checked to see if the port is released using netstat -anp | grep 27017. There was no output.
Restarted mongodb using sudo service mongodb start
Checked for port 27017 again and receive no output.
Tried to connect to the mongo shell and received the same error message.
Changed the mongodb.conf back to the original settings, restarted mongodb as in the above steps, and tried to connect again. Same error.
The EBS volume is configured in the starcluster config to be reattached on each startup.

For the "connect failed" after you change /etc/mongodb.conf problem, you can check the log file specified in the /etc/mongodb.conf (probably at /var/log/mongodb/mongodb.log):
Check that the directory specified by dbpath exists.
Make sure it is writable by the "mongodb" user. Perhaps it's best to chown to mongodb.
Make sure mongod actually released the 27017 port before starting it using: netstat -anp | grep 27017
Wait a couple seconds for mongod to restart before launching mongo.
It's not clear from your question if you are using Starcluster EBS volumes for Persistent Storage. Note that Ordinary EBS volumes do not automatically persist and reattach when you terminate an instance and start another. You would need to attach and mount them manually.
Once you get that working you'll probably want to create a custom Starcluster AMI with mongo properly installed and /etc/mongodb.conf appropriately modified.

Related

Mongod vs MongoDB server running as windows service

I am trying to find out what is the difference when i start mongod db with mongod command vs starting mongo db server as a windows service.
I tried starting the server using mongod and inserted some records then i tried starting mongo db via windows service and inserted some records, results are different in the same database
You are using two different databases.
And you likely checked the "install mongod as a service" in the installation.
The mongod command directory is, by default, C:\data\db.
If you setup your mongod Windows service directory to any other directory, this means you potentially have two databases (data folder).
You can search for the file named mongod.lock. If you find the file on different directory. you have more than one mongo database.

How to login to mongodb console in server

I'm trying to create a new collection in an already existing mongodb database on an ubuntu server. I tried running the command mongod but it says the mongod not found
Make sure your mongo server is running:
you can start using command sudo service mongod restart . Once it started just type mongo , It will take you in mongo console.
Try mongo.
mongod is the one that starts the server. mongo is the client that connects to the server.
If your environment variable is set correctly, it should work.
If it still says command not found, try /usr/bin/mongo
You can find more info here: https://docs.mongodb.com/manual/mongo/

How to execute 'service mongod start' using additional parameters in CentOS?

I unable to start MonogoDB service after adding users into admin db as well as my db.
I setup MonogoDB and started service using following command i.e.
service mongod start
Using command prompt, I added few users like dbOwner, clusterAdmin, readWrite, read roles base users. Along with that I also changed configuration from /etc/mongod.conf. in that file, I changed port number, IP addresses, dbPath, and security.authorization: enabled.
Then I restarted mongod service using following command.
service mongod restart
After ran this command, mongod service stopped successfully, but failed to start with only 'FAILED' message.
I tried execute following command i.e.
mongod --port 27123 --dbpath /path/to/db --auth
It is working.
Question: How to execute 'service mongod start' using additional parameters in CentOS?
MonogoDB: 3.4
OS: CentOS 7
I got solution i.e.
mongod --config /etc/mongod.conf
Referred: https://docs.mongodb.com/manual/reference/configuration-options/#use-the-configuration-file
It starts child process and also I can stop mongod service using service mongod stop command.
But I don't know whether it is correct or not.
I can't certify exactly where the script that "service" command uses on CentOS 7, but in Ubuntu 18.04 mongod service script file is in
/lib/systemd/system/mongod.service
There you can change the user who executes the process and add any parameters you want, like --auth.
Said that, if you ever executed mongod as root, some files on where you store the db data will have the owner as root, making the database fail to start as another user. The fix I found for that is to manually chown to mongodb:mongodb (or the user you want to use) all the files that are owned by root inside the database.
Hope this helps.
mongod.service file from mongodb github

MongoDB - mongo command runs but mongod doesn't

I have a MEAN droplet on digital ocean and I've found that when I run the mongo command I connect to test successfully and have access to my other databases, but if I try to run the mongod command I get the following message:
*********************************************************************
ERROR: dbpath (/data/db) does not exist.
Create this directory or give existing directory in --dbpath.
See http://dochub.mongodb.org/core/startingandstoppingmongo
*********************************************************************
How is this possible? I thought mongo was connecting to a specific instance of mongod.
I will create the /data/db folder, but I feel like I might just be ignoring another problem with setup configuration that has allowed this to happen.
/data/db will be the place you store your database data. After you created that folder, you can run mongod as normal.
The mongod is a command to start mongodb server. And mongo is a command line interface to make you community with mongodb server.
So you should start the server -> community with server.

mongodb replica set on azure vms - configure to run as a service

I've completed this tutorial and successfully deployed a 3 node replica set. I can connect to it from other hosts and all is good. The question I have is that in the tutorial it states
Start MongoDB
Once the configuration files have been edited, start the database process manual:mongod on each instance by:
Log on onto the instance
Run the following command to start the process:
mongod --config /etc/mongod.conf
This should start the manual:mongod process
To me this seems as though the replica set is running as a user process and not as a system service as in the command
sudo service mongodb start
So what happens if one of the machines reboots? Is that process dead? How can I configure the whole replica set to run as a service?
On machine reboots, the mongod process will stop and you have to restart it.
In system scripts, I am not sure if on box restarts, mongod restart is automatically taken care of or not. But you can have service scripts for mongod process, which you get automatically, when you install using mongodb apt-get/yum packages.