I am deploying my first ROR app with MongoDB. I’ve noticed that if the machine that I have it deployed on is reset then usually the database will be partially or fully gone. This was also happening before I had it explicitly stated in my Rails app that I was using MongoDB without active record, although it worked before I did.
I looked in the /data/db/ folder and there appears to be numerous database files: app-production.1, app-production.2, etc. In my mongo_config.rb initializer file I have the database specified as
MongoMapper.database = "app-#{Rails.env}"
I also launched the interative mongo console and noticed that there were 5+ app databases. So my question is, does MongoDB or Rails automatically create a new instance of the database when it shuts down? Does MongoDB use multiple files to span the database? Thanks!
MongoDB database is spanned across multiple files (app-production.1, app-production.2, …). Each new database resides in its own set of files.
MongoDB offers lazy database creation. If you write to a database and it doesn’t exist, it will be created. If a database with this name exists, it will be used.
It depends on your definition of “reset”. If you unplug power cord, then mongodb won’t have the time to shutdown properly. Recent versions ship with journaling turned on by default, so no major data loss should occur – in case of hard crash, mongodb will try to replay journal. Note: if you’re not running with journaling enabled, you should repair database after any unclean shutdown, or risk facing imminent database corruption.
If you reinstall the machine instead, then it’s no wonder that data is gone 🙂