Windows Batch Script for Automated Mongodb Backup - mongodb

I have setup a superuser in mongodb and I am able to do a backup manually in the command line or connect to the database with the username and password in the console or in Compass, but if I move that command to a batch script and run the script, I always get the error:
Failed: can't create session: could not connect to server: connection() : auth error: sasl conversation error: unable to authenticate using mechanism "SCRAM-SHA-256": (AuthenticationFailed) Authentication failed.
The command is:
mongodump --username myusername --password mypassword --out C:\backups --db mydb --authenticationDatabase admin
I have updated the bindIp to 0.0.0.0 and added security: authorization: "enabled".
Any thoughts on why I can't run this in the batch script. I am using Mongo 4.2
Thanks

Related

Passing MongoDB user password in newline for Mongodump command using bash script

I am trying to backup a MongoDB instance that has authentication enabled but i get this error:
$> mongodump --username=admin --password=******* --authenticationDatabase=admin --db=ixpay --collection=blacklist --out=.
2020-06-02T21:39:39.086+0000 Failed: can't create session: could not connect to server: connection() : auth error: sasl conversation error: unable to authenticate using mechanism "SCRAM-SHA-256": (AuthenticationFailed) Authentication failed.
I am attempting this data dump using a bash script but i noticed that if i run the command directly on my terminal without the --password flag it works but then Mongodump shell will prompt for password like so:
$ mongodump --username=admin --authenticationDatabase=admin --db=ixpay --collection=blacklist --out=.
Enter password:
How do i write the mongodump command in bash, so that i can interactively(but quietly) pass the password to the Mongodump command when i execute the script?

Can't connect to remote mongodb with macOS

I am trying to connect to a mongodb service hosted on IBM Cloud following this instructions.
When I run the following command
mongo -u $USERNAME -p $PASSWORD --ssl --sslCAFile c5f07836-d94c-11e8-a2e9-62ec2ed68f84 --authenticationDatabase admin --host replset/bdb98a3ac10-0.b8a5e798d2d04f2e860d042c915.databases.appdomain.cloud:30484,bd576-96db98a3ac10-1.b8a5e4e5d042c915.databases.appdomain.cloud:30484
I get this error on macOs, while on Windows 10 the connection is correctly estiblished:
SSL peer certificate validation failed: Certificate trust failure:
Invalid Extended Key Usage for policy; connection rejected
If I connect via MongoDB Compass instead of using the terminal the connection works
I had to add --sslAllowInvalidCertificates flag
https://docs.mongodb.com/manual/reference/configuration-options/#net.ssl.allowConnectionsWithoutCertificates

MongoExport SASL authentication step: Authentication failed

I am having trouble running the mongoexport on my server in regards to the error SASL authentication step: Authentication failed.
The command i'm using is:
mongoexport --db database --collection users --out /var/www/test.json -u username -p password --authenticationDatabase 'database'
I am able to get into mongo using this command:
mongo -u username -p password -authenticationDatabase database`
When I go into the user roles, my user has "ReadWrite" access.
I've tried adding role "backup" to the user, but always receive the error
"No role named dbAdminAnyDatabase#database"
"No role named backup#database"
I'm using Mongo version 3.4.6.
Can anybody help me? What am I doing wrong?

Use mongorestore to restore a database to MongoDB (3.4) with --auth enabled, SASL error

Using mongorestore, I am trying to restore a MongoDB database to a new server (both version are 3.4). The new server has -auth enabled, so you are required to login. The database does not exist so I want mongorestore to create it using the --db option. This works when authorization is not enabled but if I enable authorization the restore fails with the following error:
Failed: error connecting to db server: server returned error on SASL authentication step: Authentication failed.
I am using an admin account with the root role when I attempt the restore.
Backing up prod and restoring to dev is a fairly regular activity for us, but we can't just drop the existing database and recreate it because of the error above, not unless we disable authorization which doesn't make much sense. Is there a better way to do this/avoid the SASL errors/not have to disable auth?
I was getting the same error and while I couldn't figure out what was wrong restoring with my admin user (my hunch is a ! in the password which escaping did not help) I was able to restore by creating a new user specifically for the role.
In mongo shell:
>use admin;
>db.createUser({
user: 'restoreuser',
pwd: 'restorepwd',
roles: ['restore']
});
In terminal:
$mongorestore --host databasehost:12345 --username restoreuser --password restorepwd --authenticationDatabase admin --db targetdb ./path/to/dump/
Thanks to Adamo Tonete over at Percona, he helped us solve this problem. If you want to restore a database using your admin user with the root role, you need to specify the authentication database and user in the mongorestore command.
mongorestore --host hostname:27017 -u adminuser -p pass --authenticationDatabase admin -d TargetDatabase /Data/TargetDatabaseRestore
That tells mongo to use the admin database to authenticate the user you are passing in. If that user has the correct rights assigned, it will be able to create the new database.
First Access your db to 4366 port then run this command
mongorestore --port 4366 -u admin -p password --authenticationDatabase admin -d dealmoney /home/yash/Desktop/prodDump/teatingToProductionLastDump/dealmoney .

MongoDB shell command line authentication fails

I set up replica set with authentication. I used this tutorial. I set up keyfile, admin user and other users. It all works fine - anonymous access is disabled and I can login
$ mongo
MongoDB shell version: 2.6.1
connecting to: test
Error while trying to show server startup warnings: not authorized on admin to execute command { getLog: "startupWarnings" }
rs0:PRIMARY> use admin
switched to db admin
rs0:PRIMARY> db.auth("USERNAME","PASSWORD")
1
rs0:PRIMARY>
using credentials of users I created.
But I can't login from command line
$ mongo -u 'USERNAME' -p 'PASSWORD'
MongoDB shell version: 2.6.1
connecting to: test
2014-05-18T14:23:47.324+0200 Error: 18 { ok: 0.0, errmsg: "auth failed", code: 18 } at src/mongo/shell/db.js:1210
exception: login failed
using the same credentials. I haven't find anything helpful in the documentation or here on SO.
The in-shell authentication performed in your example is against the admin database. The command line posted above does not specify a database and is therefore authenticating against the default database which is test. Try this instead to authenticate via command line against the admin db:
mongo admin -u 'USERNAME' -p 'PASSWORD'
if the server is not on the local host then you can use this:
mongo your_host_name:your_port/admin -u 'USERNAME' -p 'PASSWORD'
Perhaps different now because of an update since this question was originally answered, but authentication only succeeds when wrapping username/password in double quotes.
e.g.,
mongo admin -u "myName" -p "myPassword"