Authentification fail with mongodump - mongodb

I try to create a backup of my MongoDB replicaset but I have an authentication problem :D .
So when I try to use mongodump
root#e03c2a89ac31:/# mongodump -u myBackupAccount -p 123456 --host mongo1 --out /home/backup
2017-01-17T11:00:41.069+0000 Failed: error connecting to db server: server returned error on SASL authentication step: Authentication failed.
root#e03c2a89ac31:/# mongodump -u myBackupAccount -p 123456 --host mongo1 --authenticationDatabase admin --out /home/backup
2017-01-17T12:37:45.819+0000 Failed: error connecting to db server: server returned error on SASL authentication step: Authentication failed.
But if I try with a mongo shell, I can connect to my instance
root#e03c2a89ac31:/# mongo -u myBackupAccount -p 123456 --host mongo1
MongoDB shell version v3.4.1
connecting to: mongodb://mongo1:27017/
MongoDB server version: 3.4.1
REPLICASET_NAME:PRIMARY> exit
My backup user was created with that command
root#e03c2a89ac31:/# mongo -u myUserAdmin --authenticationDatabase "admin" -p 123456
MongoDB shell version v3.4.1
connecting to: mongodb://mongo1:27017/
MongoDB server version: 3.4.1
REPLICASET_NAME:PRIMARY> db.createUser({user: "myBackupAccount", pwd: "123456", roles: [ { role: "backup", db: "admin" } ]})
So do you know why it isn't working ?
I did somethings bad ? :p
Thanks in advance for your help ;)

Related

Error when trying to create dump of mongodb database

i tried to create a dump of a mongodb database on remote server which is running centos,nginx,mongodb v4.0.18 using the following command..
mongodump --host localhost:27017 --authenticationDatabase admin --username <uname> --password <password> --db <db> --out <path to dump>
but im getting the following error
Failed: error connecting to db server: server returned error on SASL authentication step: Authentication failed.
I just replaced --host localhost:27017 for --port 27017. My db is in localhost too. I omitted --db <db> --out <path to dump> too. I point to my db with the --authenticationDatabase flag.
I hope this helps.

Trying to run mongorestore and cannot connect?

Using MongoAtlas and trying to run mongorestore and I am getting the following error.
Failed: error connecting to db server: no reachable servers
I am using the command Atlas proposes.
mongorestore --host BStaging-shard-0/bstaging-shard-00-00-lq11i.mongodb.net:27017,bstaging-shard-00-01-lq11i.mongodb.net:27017,bstaging-shard-00-02-lq11i.mongodb.net:27017 --ssl --username heroku --password "VN" --authenticationDatabase admin
2019-04-14T11:02:31.336-0500
Failed: error connecting to db server: no reachable servers
Fixed:
Updated MongoDB community Shell
Changed bash_profile to point to new shell
Changed bash_profile to point to new mongo db community4.0
(/usr/local/opt/mongodb-community#4.0/bin:/usr/local/opt/mongodb-community-shell/bin:$PATH)
source bash_profile
reconnect to mongo
mongo "mongodb://staging-shard-00-00-dgdr.mongodb.net:27017,staging-shard-00-01-dgdr.mongodb.net:27017,staging-shard-00-02-dgdr.mongodb.net:27017/test?replicaSet=Staging-shard-0" --ssl --authenticationDatabase admin --username me --password pwd
restore
mongorestore --host Staging-shard-0/staging-shard-00-00-dgdr.mongodb.net:27017,staging-shard-00-01-dgdr.mongodb.net:27017,staging-shard-00-02-dgdr.mongodb.net:27017 --ssl --username me --password pwd --authenticationDatabase admin

Mongodump from secure Mongo database

In order for me to connect to this [secure] Mongo instance I have to run the following command:
mongo --ssl --host sampleHostname --sslPEMKeyFile /path/to/user.pem --sslCAFile /path/to/mongoca.cer --authenticationDatabase '$external' --authenticationMechanism=MONGODB-X509
I am trying to do a mongodump command to get the data but I keep running into the following errors:
Attempt 1
mongodump -d mydb
Failed: error connecting to db server: no reachable servers
Attempt 2
Can't create backup mongodump with --db. Authentication failed
mongodump -d mydb --authenticationDatabse '$external'
Failed: error connecting to db server: no reachable servers
Attempt 3 Using the same command as how I connect.
mongodump -d mydb --ssl --host sampleHostname --sslPEMKeyFile /path/to/user.pem --sslCAFile /path/to/mongoca.cer --authenticationDatabase '$external' --authenticationMechanism=MONGODB-X509
Failed: error getting collections for database 'mydb': error running 'listCollections'. Database: 'mydb' Err: not authorized on 'mydb' to execute command {listCollections: 1, cursor: {} }
I have tried the same command with sudo but it still returns the same error.
Attempt 4 Minimum permission for using mongodump (to dump a specific db)
mongodump -d mydb --ssl --host sampleHostname --sslPEMKeyFile /path/to/user.pem --sslCAFile /path/to/mongoca.cer --authenticationDatabase '$external' --authenticationMechanism=MONGODB-X509 --excludeCollection=system.indexes
Failed: error getting collections for database 'mydb': error running 'listCollections'. Database: 'mydb' Err: not authorized on 'mydb' to execute command {listCollections: 1, cursor: {} }
I am stuck and I am eventually going to run mongorestore but I do not want to run this without making sure I am able to backup first. I imagine the solution for mongodump will resolve any possible issues I may have with mongorestore (if any).
I found the solution thanks to this blog post , looks you have to set the -u value with the CN when using 509 and $external.
mongodump --ssl --sslPEMKeyFile user.pem --sslCAFile cap.pem --sslAllowInvalidHostnames --authenticationMechanism=MONGODB-X509 --authenticationDatabase '$external' --host "rsTmpCloudManager/10.100.15.118:27017,10.100.16.237:27017,10.100.17.107:27017" -d testJoce -u "CN=???,OU=???,O=???,L=???,ST=???,C=??"

mongodb database connection to my database

I connect to mongodb database like this
bin]$ ./mongo --host XXXXX-2.XXXXXXX-XXXX.1234.test.com
Connecting to test
mongo>use my_database
How do I connect to my_database directly instead of the default ?
Omit --host and append /<dbname> to the host name to provide a "db address":
bin]$ ./mongo XXXXX-2.XXXXXXX-XXXX.1234.test.com/my_database

Access as admin with mongo client ok, fails with mongorestore

Using the mongo client, I can authenticate successfully with my admin account:
$ mongo -u my_admin_username -p my_admin_pass --authenticationDatabase admin
MongoDB shell version: 2.6.3
connecting to: test
>
but when I try to execute mongorestore with the same credentials, it fails:
$ mongorestore -u my_admin_username -p my_admin_pass /backup/20140821/db/myproject/
connected to: 127.0.0.1
assertion: 13 not authorized on admin to execute command { getParameter: 1, authSchemaVersion: 1 }
Why is that? What am I missing? I'd like to execute successfully a mongorestore.
$ mongorestore -u my_admin_username -p my_admin_pass /backup/20140821/db/myproject/ -db myproject
This worked.