mongodump from remote node - unable to authenticate using mechanism "SCRAM-SHA-256" - mongodb

Tried taking dump from a remote node and got the following 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.
Tried two methods to take dump from the remote node. But got the same error in both the methods.
# Method 1
mongodump -h remoteip#port -u xxx -p xxx --db xxx --authenticationDatabase xxx
# Method 2
mongodump --uri "mongodb://username:password#remoteip:port/db?authSource=xxx"
How to resolve this?

For me (trying to use mongodump on a single node DB on the same host), using --authenticationDatabase admin did the trick:
mongodump -u root --password 'secret' --authenticationDatabase admin -d mongo-dev -o /tmp/dump-2020-11-27.bson
(courtesy of mongodump from remote node - unable to authenticate using mechanism "SCRAM-SHA-256")

1.If you are using an URI for mongodump command,--authenticationDatabase admin option is equivalent to ?authSource=admin
mongodump --uri "mongodb://[username:password#]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]"
sample url:
mongodump --uri "mongodb+srv://username1:password1#host1/db1?authSource=admin"

I had the same issue. In my case, the password has special characters. It works with single quote for password:
-p 'my_password'

I had the same problem and solved it using single quotes in the password like this:
--password 'secret'

Was in the same spot that you are, solved it this way:
mongodump --uri "mongodb+srv://username:password#yourmongodbclustersourceurl" --archive \
mongorestore --uri "mongodb+srv://username:password#yourmongodbclusterdestinationurl" --archive \
--nsExclude "admin.system.*"
Needless to mention, you just need to change your username, password and the url in this formula and voila. Good luck.

Related

authenticationDatabase error when running mongoimport

when doing the m103 mongodb course i came across this error when doing the import lab:
user#NHTTPR# mongoimport /dataset/products.json -h localhost:27000 -u m103-application-user -p “m103-application-pass” --authenticationDatabase admin --db applicationData --drop --collection products
2021-04-07T06:19:23.616+0000 error connecting to host: could not connect to server: connection() : auth error: sasl conversation error: unable to authenticate using mechanism "SCRAM-SHA-256": (AuthenticationFailed) Authentication failed.
the solution was to put single quotes (') around the password. instead of double quotes ( or remove the quotes around the password entirely)
mongoimport /dataset/products.json -h localhost:27000 -u m103-application-user -p 'm103-application-pass' --authenticationDatabase admin --db applicationData --drop --collection products
2021-04-07T06:20:25.025+0000 connected to: mongodb://localhost:27000/
2021-04-07T06:20:25.026+0000 dropping: applicationData.products
2021-04-07T06:20:25.423+0000 9966 document(s) imported successfully. 0 document(s) failed to import.
user#NHTTPR#
use single quote or dont use any quotes following works
mongoimport --username m103-application-user --password m103-application-pass --port=27000 --authenticationDatabase admin --db applicationData --drop --collection products /dataset/products.json

Mongo doesn't restore user password

I have following scenario:
Dump Mongo database (cmd mongodump)
Change password from one of admin users
Restore Mongo database (cmd mongorestore)
EDIT:
I use following command for db dump:
mongodump -h $HOST:$PORT --ssl --sslAllowInvalidCertificates --authenticationDatabase $authdb -u $user -p $pass --gzip --archive=$destination
As a result password was not restored. Is there any defect plan that can be followed in mongodb for this specific case?
Are you sure that your admin user is created in the database you dump?
The authentication database is not necessarily the used database in mongodb. If you want to dump/restore users, you have to dump/restore your authentication database(s).
So I found solution, --drop shoud be added in the command:
mongodump -h $HOST:$PORT --ssl --sslAllowInvalidCertificates --authenticationDatabase $authdb -u $user -p $pass --drop --gzip --archive=$destination

Issues with mongoRestore [listCollections requires authentication]

I am trying to restore a MongoDB on an EC2 instance. I am currently running Mongo 4.0. I am restoring a .tgz, which I then unzip, and it contains a directory with all of my files. I previously used this command:
sudo mongorestore --db newDB mongoDump-2018-07-25-0200/viboDB/
Now that I am trying to update our database, I am getting the following error.
building a list of collections to restore from mongoDump-2018-07-25/0200 dir
Failed: viboBI2.Songs: error reading database: command listCollections requires authentication
I have logged into the mongo shell, and used db.auth() to authenticate as an admin. I have tried restarting mongo as well. Any help would be appreciated!
For restoring the Database you need to provide authentication.
mongorestore -u USERNAME -p PASSWORD --authenticationDatabase admin -d dbNAME PATH/TO/DIRECTORY
you can also provide host and port by adding -h and --port

How to get backup with mongodb

When I want to take a back up I confront to this command
mongodump -d databasename -u admin -p -o /home
I encounter an error Errmsg: "auth faild",code:18}
But with this user and pass I can connect to database
But while I would prefer to disconnect authentication
I will Get backup
Thanks
This message indicates that mongodump couldn't authenticate against the mongod. There are a few possible causes. Looking at the docs we can see the following note about the -p option.
Changed in version 3.0.0: If you do not specify an argument for
--password, mongodump returns an error.
Changed in version 3.0.2: If you wish mongodump to prompt the user for
the password, pass the --username option without --password or specify
an empty string as the --password value, as in --password "" .
In the example command you provided, there is just a -p which means it is expecting a password.
Additionally, the --authenticationDatabase option is missing. Looking at the docs for mongodump:
Specifies the database in which the user is created. See
Authentication Database.
If you do not specify an authentication database, mongodump assumes
that the database specified to export holds the user’s credentials.
If you do not specify an authentication database or a database to
export, mongodump assumes the admin database holds the user’s
credentials.
Since the -d parameter is specified, mongodump will try to authenticate against that database. It is more likely that the user exists in the admin database.
Taking all of this into account, please try the following command:
mongodump -d <databasename> -u <user> --authenticationDatabase admin -o /home
This command will prompt you for a password via stdin. If you want to specify the password on the command line, the command changes to
mongodump -d <databasename> -u <user> --authenticationDatabase admin -p <password> -o /home
Please replace <databasename>, <user>, and <password> with the appropriate values for your environment.

Mongodump from remote server

We recently ported some data over to MongoDB and are now looking into running daily backups, preferably from a cron job, and restore one of the backups to a secondary mongo database.
Our system is set up as follows:
server 1: the development mongo database
server 2: two mongo databases, one for staging data and one for production
server 3: is where we run all of our cron jobs/batch scripts from.
I checked the mongo docs, and logged into our cron job server and tried to run the following command: (username, host, and password changed for security, I'm not actually connecting to localhost)
mongodump --host 127.0.0.1/development --port 27017 --username user --password pass --out /opt/backup/mongodump-2013-10-07-1
I get the following messages:
Mon Oct 7 10:03:42 starting new replica set monitor for replica set 127.0.0.1 with seed of development:27017
Mon Oct 7 10:03:42 successfully connected to seed development:27017 for replica set 127.0.0.1
Mon Oct 7 10:03:42 warning: node: development:27017 isn't a part of set: 127.0.0.1 ismaster: { ismaster: true, maxBsonObjectSize: 16777216, ok: 1.0 }
Mon Oct 7 10:03:44 replica set monitor for replica set 127.0.0.1 started, address is 127.0.0.1/
Mon Oct 7 10:03:44 [ReplicaSetMonitorWatcher] starting couldn't connect to [127.0.0.1/development:27017] connect failed to set 127.0.0.1/development:27017
I confirmed that I can connect to the mongo database using mongo -u -p ip/development
Our ultimate goal will be to dump the data from the production database and store it in the staging database. These two databases are both located on the same box, if that makes a difference, but for testing purposes I am just trying to get a backup of development test data.
mongo client can parse MongoDB connection string URI, so instead of specifying all connection parameters separately you may pass single connection string URI.
In your case you're trying to pass connection URI as a host, but 127.0.0.1/development is not a valid host name. It means you should specify database parameter separately from the host:
mongodump --host 127.0.0.1 -d development --port 27017 --username user --password pass --out /opt/backup/mongodump-2013-10-07-1
You can use with mongodump with --uri
mongodump --uri "mongodb://usersname:password#127.0.0.1:27100/dbname?replicaSet=replica_name&authSource=admin" --out "C:\Umesh"
All your collections will store inside the out folder it will create directory name as your Database name and all the collections are bson and metadata will store as json format.
For restore
mongorestore --uri "mongodb://usersname:password#127.0.0.1:27100/dbname?replicaSet=replica_name&authSource=admin" -d dbname mongodbumppath
Try this it will work.
This worked for me.
Reference: https://docs.mongodb.com/manual/reference/program/mongodump
Syntax 1:
mongodump --host <hostname:port> --db <database> --username <username> --password <password> --out <path>
Syntax 2:
mongodump -h <hostname:port> -d <database> -u <username> -p <password> -o <path>
Example 1:
mongodump --host 127.0.0.1:27017 --db db_app --username root --password secret --out /backup/db/app-17-03-07
Example 2:
mongodump -h 127.0.0.1:27017 -d db_app -u root -p secret -o /backup/db/app-17-03-07
mongodump --host remotehostip:port --db dbname -u username -p password
Here is an example of exporting collection from node server to local machine:
Host : xxx.xxx.xxx.xx
Port :27017
Username:”XXXX”
Password :”YYYY”
AuthDB : “admin”
“DB”: “mydb”
D:\mongodb-backup>mongodump -h xxx.xxx.xxx.xxx –port 27017 -u “XXXX” -p “YYYY” –authenticationDatabase “admin” –db “mydb”
Use this to get dump using URI:
mongodump --uri=mongodb+srv://john:xxxxxxxxxxxxxxx#cluster0-jdtjt.mongodb.net/sales
You can also use gzip for taking backup of one collection and compressing the backup on the fly
mongodump --db somedb --collection somecollection --out - | gzip > collectiondump.gz
Or with a date in the file name:
mongodump --db somedb --collection somecollection --out - | gzip > dump_`date "+%Y-%m-%d"`.gz
This worked to me like a charm for a single collection with a remote Windows Server.
mongodump --host <remote_ip> --port <mongo_port> --db <remote_db_name> --authenticationDatabase <remote_auth_db> --username <remote_mongo_username> --password <remote_db_pwd> --out <local_DB_backup_folder> --collection <remote_collection_name>
On Mac, this is what worked for me (but be sure to use your own real credentials):
brew tap mongodb/brew
brew install mongodb-community#5.0
brew services start mongodb/brew/mongodb-community
mongodump --uri "mongodb://usersname:password#127.0.0.1:27100/dbname" --out "/Users/some_username/code/mongodb_dumps/dump/"
cd /Users/some_username/code/mongodb_dumps/
mongorestore --nsInclude "*.*"
mongodump --host hostip -d dbname --port portnumber --username username --password password --authenticationDatabase admin -o ./path/of/backupfolder
note: "./path/of/backupfolder" path is in your client
This worked for me:
Step1: Export remote/local DB.
mongodump --uri "mongodb+srv://USER:PASSWORD........." --out "/Users/Hardik/Desktop/mongo_bkp"
Step2: Import
mongorestore ./mongo_bkp/
Posting this here in case it helps somebody.
It was impossible for me to connect using mongodump. I ended up installing the VS Code Mongo extension and it generated the string for me. The command looks like this:
mongodump -o dump_destination --uri "mongodb://<USERNAME>:<PASSWORD>#<HOST>:<PORT>/<DATABASENAME>?authSource=admin&readPreference=primary&ssl=true"