Unable to export MongoDB database from a remote server - mongodb

mongoexport --host=hostname --port=27017 --db=dbname --collection=
collection_name --out=path/data.json
Here is the error which I'm getting
error connecting to db server: no reachable servers

you need to fill a remote hostname or a remote ip, username and password if required.
mongoexport --host hostname --port 27017 --username user --password "pass" --collection collectionName --out=path/data.json
try read the official mongodb docs

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 ssl .pem file in connection string

I have a mongodb server v 3.2 configured to use ssl for client connections, with a custom-generated certificate.
I can connect to the server using mongo.exe with the following format:
C:\mongodb\bin>mongo.exe myhost:27017/mydb --sslPEMKeyFile
C:\etc\ssl\mongodb.pem --ssl --username myuser --password mypassword
--sslAllowInvalidCertificates
Is it possible to write an equal mongodb connection string (mongodb://....)?
According to the documentation, there is the only ssl parameter, that seems to be not enough.
Could you try to connect to mongo.exe with the parameter like below:
"mongodb://myuser:mypassword#myhost:27017/mydb?ssl=true&sslAllowInvalidCertificates=true&sslPEMKeyFile=C:/etc/ssl/mongodb.pem"
This works:
mongo --host server --ssl --sslCAFile C:\Path\mongodb-server.pem
--sslPEMKeyFile C:\Path\mongodb-client.pem --sslPEMKeyPassword
password -u user -p --authenticationDatabase admin

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"