15 February 2016

MongoDB Auth Failed

So I logged in to new MongoDB server via currently installed mongo shell. Even though provided credentials were correct, I was receiving:

exception: login failed
$ mongo mongosrver:27017/admin -ssl --sslAllowInvalidCertificates  -u root -p password
MongoDB shell version: 2.6.10
connecting to: mongoserver:27017/admin
2016-05-26T15:53:45.952+0100 Error: 18 { ok: 0.0, errmsg: "auth failed", code: 18 } at src/mongo/shell/db.js:1287
exception: login failed
After much struggle, I came across a forum thread and got the clue that Mongo server and client might be mismatched.

I had 2.6.1 installed: 

$ mongo --version
 version 2.6.10
And it was the latest version in ubuntu repositories:

$ sudo apt show mongodb-clients 
Package: mongodb-clients
Version: 1:2.6.10-0ubuntu1
Priority: optional
Section: universe/database
Source: mongodb
Origin: Ubuntu
Maintainer: Ubuntu Developers 
Therefore, I had to download the package from Mongo official repos.

Adding the mongo repo key:

$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6
Executing: /tmp/tmp.3VAqGKcmyO/gpg.1.sh --keyserver
hkp://keyserver.ubuntu.com:80
--recv
0C49F3730359A14518585931BC711F9BA15703C6
gpg: requesting key A15703C6 from hkp server keyserver.ubuntu.com
gpg: key A15703C6: public key "MongoDB 3.4 Release Signing Key " imported
gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)
Adding the mongo official repo to apt sources:

$ echo "deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list
deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 multiverse
gpg:               imported: 1  (RSA: 1)
Update the apt packages and install the latest mongo shell:

$ sudo apt-get update
$ sudo apt-get install -y mongodb-org-shell
We have the latest version now:

$ mongo --version
MongoDB shell version v3.4.1
git version: 5e103c4f5583e2566a45d740225dc250baacfbd7
OpenSSL version: OpenSSL 1.0.2g  1 Mar 2016
allocator: tcmalloc
modules: none
build environment:
    distmod: ubuntu1604
    distarch: x86_64
    target_arch: x86_64
And eventually we are able to connect:

$ mongo mongoserver:27017/admin -u root -p password
MongoDB shell version v3.4.1
connecting to: mongodb://mongoserver:27017/admin
MongoDB server version: 3.2.11
> 
> show collections
system.users
system.version 

MongoDB Dump and Restore

First we need to have mongo tools installed. If you have already added the Mongo official repo mentioned above, then issue following command:

$ sudo apt install mongodb-org-tools
Now we will back up all of our databases and collections via mongodump to backup folder:

$ mongodump --host mongoserver --port 27017 --username root --password passwd --out ./backup
And restore the mongo dump via mongorestore from backup folder:

$ mongorestore -vvvvv --drop --host localhost --port 27017 ./backup
We set -vvvvv flag for maximum verbose and --drop flag to drop collections if they are already exist on database.