Ideally, we should format the name node only during the installation of Hadoop, because it creates all the necessary files and directories for HDFS and assigns a unique id to namenode. When we start services at that time the same id is assigned to all the datanodes.
But if we format NameNode again, it deletes all the data stored in the HDFS. Now it assigns a new id to name node, while datanodesare having their old id. This causes version-id mismatch error in Hadoop.
To resolve this issue, simply delete hdfs base directory (specified in core-site.xml as dfs.tmp.dir) and again format the namenode and start the services.