I am trying to delete a mongo database inside a shell script. The server requries authentication with a password. When I type the following into the command line:
mongo -u mun -p 'themongopassword'
I am able to connect to the database and then when run the following commands:
use dbname
db.dropDatabase()
The database is successfully deleted. However I would like to do the following:
mongo -u mun -p 'themongopassword' --eval 'db.dropDatabase()' sigma
I get the following output:
MongoDB shell version v4.2.6
connecting to: mongodb://127.0.0.1:27017/sigma?compressors=disabled&gssapiServiceName=mongodb
2020-05-06T13:13:53.419+0300 E QUERY [js] Error: Authentication failed. :
connect#src/mongo/shell/mongo.js:341:17
#(connect):2:6
2020-05-06T13:13:53.420+0300 F - [main] exception: connect failed
2020-05-06T13:13:53.420+0300 E - [main] exiting with code 1
Removing the eval option like so:
mongo -u mun -p 'themongopassword' sigma
also results in an authentication failure with the same output.
Did you tried this:
mongo <dbname> -u mun -p 'themongopassword' --eval "db.dropDatabase()"
If you want to get a readable result try this:
mongo <dbname> -u mun -p 'themongopassword' --eval "printjson(db.dropDatabase())"
Try adding option "--authenticationDatabase admin", i.e.:
mongo <dbname> -u mun -p 'themongopassword' --authenticationDatabase admin --eval "printjson(db.dropDatabase())"
Related
I want to remove document from mongodb collection in script linux(with curl).
like elastic commands ::
curl -X GET "localhost:9200/my-index-000001/_search?pretty"
AS #Wernfried mentioned mongoDB do not provide HTTP service so you cannot use curl , however you can do something like this from your linux shell script:
echo 'db.getCollection("myCollection").remove({_id:"documentIdforRemoval"})'| mongo --quiet --host myDBhost --port myDBport myDB --AuthenticationDatabase=admin -u myuser -p mypass
I have a Mongo database on my server and I can connect to it using docker.
I run the following command:
docker exec -it mongodb mongoexport --port 27017 -u "username" -p "password" --authenticationDatabase "admin" -d databaseName -c user --out /var/www/myTest/output.json
It seems that the command works without any problem and I get the message:
connected to: mongodb://localhost:27017/
exported 16 records
However, when I use FileZilla and look at the directory /var/www/myTest, there is no file there. The folder is empty (I had created the folder myself earlier. If I use a new folder name, the folder is also not generated).
What have I done wrong? Is the file somewhere else?
Thanks in advance,
That directory and the file is inside the mongoexport container.
UPDATE:
You could also bind mount a directory from your host to the containers:
docker run --rm -v /var/www/myTestOnHost:/var/www/myTestOnContainer -it mongodb mongoexport --port 27017 -u "username" -p "password" --authenticationDatabase "admin" -d databaseName -c user --out /var/www/myTestOnContainer/output.json
docs: https://docs.docker.com/storage/bind-mounts/
--rm flag for docker run will remove the container when it finished running, that is executing the mongoexport --port 27017 -u "username" -p "password" --authenticationDatabase "admin" -d databaseName -c user --out /var/www/myTestOnContainer/output.json command
I am running a simple find query from unix shell.
mongo <<DB NAME>> -u <<USERNAME>> -p <<PASSWORD>> <<HOST>>:<<PORT>> --authenticationDatabase <<AUTHENTICATION DB NAME>> --eval db[<<COLLECTION NAME>>].find({"name":"Harsha"})
I am getting below error.
[js] SyntaxError: missing ; before statement #(shell):1:6
I also tried by changing DB name place (ex: host:port/db_name)
mongo -u <<USERNAME>> -p <<PASSWORD>> <<HOST>>:<<PORT>>/<<DB NAME>> --authenticationDatabase <<AUTHENTICATION DB NAME>> --eval db[<<COLLECTION NAME>>].find({"name":"Harsha"})
Still getting same error.
Am I missing something? Can anyone please help?
MongoDB expects the argument of --eval to be passed as string like in this example:
--eval "db[<<COLLECTION NAME>>].find({'name':'Harsha'})"
This is the setup i have
Created mongodb instance with docker
sudo docker run -p 27017:27017 -e MONGODB_DATABASE=DEV -e MONGODB_USER=dev -e MONGODB_PASSWORD=dev123 -e MONGODB _ADMIN_PASSWORD=dev123 -e MONGODB_ROLE=readWriteAnyDatabase --name mymongo -v testdb:/var/lib/mongodb/data -d mongo
Entered container using
sudo docker exec -it container-id /bin/bash
Executed command
mongodump -d DEV -u dev -p dev123 ( works perfectly )
Now the ISSUE happens while restoring to different database
mongorestore --db test ./dump/DEV -- throws below error
Failed: test.duke: error reading database: not authorized on test to execute command { listCollections: 1, cursor: { batchSize: 0 } }
Stuck for 3 days now any help would be appreciated ( beginner to both docker and mongodb)
If your other mongo database has authentication then you should use :
mongorestore -u <username> -p <password> --authenticationDatabase=<database name> --db=test ./dump/DEV
Other advice would be to create dumps like :
mongodump --port 55555 -d testdb --gzip --archive=testdb.tar
and then restore like:
mongorestore --port 55555 --gzip --archive=testdb.tar
I am trying to export mongodb database using this command:
mongoexport -d db_name
But I am getting this error:
SyntaxError: missing ; before statement #(shell):1:15
What will be the command to export db?
You can use mongodump it's faster:
mongodump -d <database_name> -o <directory_backup>
And to restore/import that, i used (from directory_backup/dump/):
mongorestore -d <database_name> <directory_backup>