1
votes

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:

  1. The directory does exist
  2. I changed the owner to mongodb
  3. I then issued the command sudo service mongodb stop
  4. Checked to see if the port is released using netstat -anp | grep 27017. There was no output.
  5. Restarted mongodb using sudo service mongodb start
  6. Checked for port 27017 again and receive no output.
  7. Tried to connect to the mongo shell and received the same error message.
  8. 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.

1

1 Answers

1
votes

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):

  1. Check that the directory specified by dbpath exists.
  2. Make sure it is writable by the "mongodb" user. Perhaps it's best to chown to mongodb.
  3. Make sure mongod actually released the 27017 port before starting it using: netstat -anp | grep 27017
  4. 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.