Product Navigation

Repair MongoDB Database using Mongo Shell repairDatabase


MongoDB is one of the leading NoSQL Database which is freely available. It provides high availability, scalability, and disaster recovery. In this article, we will discuss about MongoDB features, common errors & their resolution, and methods to repair mongoDB database.

Introduction to MongoDB


MongoDB is an open-source document oriented database. It is the fastest growing database which uses BSON format (binary representation of JSON-like documents) rather than table and rows as in relational database. This cross platform program is made up of collections & documents. Documents consists of key-value pairs and Collections comprises sets of documents.

repair oracle

Key Features of MongoDB Database


MongoDB, referred as NoSQL database, is more scalable and provide better performance than relational database. It supports dynamic schema design which means documents in a collection have different fields and structure. MongoDB has plenty of features that are described below:

  • Horizontal Scalability – MongoDB has auto sharding feature which scales the database horizontally. It automatically distributes data in a collection across multiple servers. The database partitions the collection with the shard key which is chosen by a user. By distributing the data across several machines, MongoDB automatically balances the data & query load to keep the servers running even if the hardware fails.
  • Redundancy and Data Availability – MongoDB handles replication with replica sets (a group of MongoDB servers). A replica set contains multiple copies of data. Each replica set act as a primary or secondary replica. By default, writes and reads operations are done on the primary replica. Besides, secondary replicas stores a copy of the data using replication. If primary replica fails then replica set automatically determine which secondary replica will become the next primary. This feature is highly useful to repair mongoDB database.
  • GridFS File Storage – MongoDB has an inbuilt feature GridFS which provides virtual file system for storing and retrieving data. It supports files larger than 16MB and each file is divided into 255KB chunks. The file system stores the files in two collection, chunks are stored in fs.chunks and metadata about the files is stored in fs.files. GridFS automatically synchronize files and metadata when data is distributed across multiple systems.

Components of MongoDB Database


  • mongod : It is a daemon process which handles data requests, and performs background operations.
  • mongos : Improper termination of the process. It was forced to disconnect from the instance.
  • Mongo : Check the alert log for the instance detail. And retry the operation.

Common Errors & Their Resolution


Error 1: Child process failed, exited with error number 100

Cause : Insufficient free space for journal files.

Solution : To resolve the issue, follow the below steps :

Step 1 : Execute: [root@server ]# mongodb –smallfiles

Step 2 : Add the ‘true’ value for nojournal directive in the configuration file(/etc/mongod.conf)

vi /etc/mongod.conf

nojournal = true

Step 3: Save and Restart

[root@sever ]# service mongod restart


Error 2: JavaScript execution failed: could‘t connect to server couldn't connect to server 127.0.0.1 shell/mongo.js

Cause : Due to improper shutdown

Solution:

Remove the Lockfile /var/lib/mongodb/mongod.lock:
# sudo rm /var/lib/mongodb/mongod.lock

Repair mongodb database
# sudo -u mongodb mongod -f /etc/mongodb.conf --repair

Then start server
# sudo service mongodb start

Workarounds to Repair mongoDB Database


Run repairDatabase Command

MongoDB provides an interactive JavaScript interface named mongo shell which is mainly used to carry out administrative operations. Moreover, mongo shell is used to run repairDatabase command that checks and repair data storage errors, inconsistencies, and data integrity.

  • The repairDatabase command compacts all collections
  • It decreases the total size of the data files and recreates indexes
  • RepairDatabase command can be invoked by executing the following commands:
    { repairDatabase: 1 }
  • mongod – – repair
  • Use db.repairDatabase() in the mongo shell which will provide wrapper around the command repairDatabase.
    If there is improper system shutdown/restart/system crash then running repairDatabase is a must. However, when it comes to repair mongoDB database, in the following cases repairDatabase need not to be run :
  • Mongod instance is running with journaling enabled (journaling will automatically restore data after system crash)
  • Replica set with complete data set is available.

Mongodb Unclean Shutdown Repair

If unclean shutdown occurs and journaling is disabled in mongodb then data may become inconsistent. Moreover, if a non-empty mongod.lock file exists then the following error message will appear:

Detected unclean shutdown – mongod.lock is not empty

Repair Process

  • Create a backup copy of the data files in the – –dbpath
  • Initialize mongod with – – repair
    While repairing mongoDB database, by default _tmp directory is used in the dbpath. If MMAPv1 Storage Engine is used then you can specify –repairpath for an alternate temporary directory.
  • Execute the below command:
    mongod - -dbpath /data/db – –repair

Conclusion


Although MongoDB uses document oriented data model to handle a tremendous amount of data in Windows, Unix, Linux, and Mac, yet it encounters plenty of errors. Hence, mongoDB provides mongo Shell to perform administrative tasks by executing several commands that repair mongoDB database and resolve various issues.