I am running a mongoDB instance from within a docker container. I am trying to switch to issue commands using docker exec and the --eval options. What I am trying to accomplish is this:
Switch to my database
Authenticate into the database
Drop a collection
The command I am running to do this is as follows:
docker exec -it mongo_instance mongo ir --eval "db.adminCommand('use db1'); db.auth("dbuser","dbpassword"); db.storage.drop()"
When I run this command, it always fails saying:
ReferenceError: dbuser is not defined :
Which I know it is. That user is located in the db1 database and I just need to switch to that database in order for the authentication to work.
If I run the following from inside the mongo shell, it works:
use ir
db.auth("dbuser","dbpassword")
db.storage.drop()
Any ideas how I can get around this or get this done?
Thanks in advance!
There are differences between interactive and scripted mongo shell interaction. In particular, syntax like use db only works in an interactive session. You can also use mongo command-line options to simplify the JavaScript to eval and avoid having passwords in your shell history.
Assuming your authentication database name is ir and the collection to drop is db1.storage , a suggested command line to use would be:
docker exec -it mongo_instance mongo -u dbuser -p --authenticationDatabase ir --eval "db.getSiblingDB('db1').storage.drop()"
A few comments on this:
The db.getSiblingDB() helper is the scripting equivalent for the interactive use <db> syntax. You could also specify the dbname to use as part of the mongo command line (eg: mongo -u dbuser -p db1 ...), but db.getSiblingDB('db1') may still be preferable as an extra sanity check that you are dropping a collection from the expected database.
If the collection you are dropping is also in the same database as your admin credentials, a shorter option for the mongo command line would be: mongo -u dbuser -p ir --eval "db.storage.drop()". The shell uses the dbname you are connecting to as the default auth database.
There is also a db.getCollection() helper which is useful if you want to be more intentional about the collection name (or if the collection name clashes with built-in methods or isn't valid in JavaScript). An equivalent to the example above would be db.getSiblingDB('db1').getCollection('storage').drop().
Using -p (aka --password) without providing a password argument will prompt you for the password so it won't be accidentally saved in your shell history. Alternatively you could use -p dbpassword if you aren't fussed about the password being saved.
Related
I have 4 local dbs in my /data/db directory
$ mongo
MongoDB shell version: 3.2.9
connecting to: db-A
$ show dbs
db-A 0.00gb
db-B 0.02gb
db-C 0.08gb
db-D 0.00gb
When I try to run:
$ mongorestore -h dsxxxxx.mlab.com:xxxxx -d <dbname> -u <user> -p <password> /data/db`
It imports the db-A database to my mlab db, how do I specify that I want to import the db-C databse?
This is an old post, but I figured i would post my findings.
If you use mongodump -d dbname -o /path/to/folder for all your data bases, you can then use mongorestore /path/to/folder and it will read all of the databases inside the folder.
If you are moving data from one mongodb instance to other, you need first dump data out from first database and then you can import it to other. You cannot use mongorestore to read directly source database and "restore" it to new one.
You can use copyDatabase() command at destination mongo to copy whole database from other machine.
There is mongodb-connector what you can use too. Actually it's very handy tool to "replicate" one way between two instances.
Is there an easy way to clear a mongodb database running in docker?
Or possibly delete it all and create it again?
There are a number of ways:
The same way you would normally clear a mongodb database. You're running it in Docker, but it is a real mongodb after all. See this, and this.
If you've mounted your database's data as a volume on the host system using -v on the docker run command, you can just clear this folder out.
If you haven't, the data lives in the container. You can remove the container and recreate it (docker rm container_name). Or run a shell in the container and remove the data from the container's filesystem (docker exec -it container_name bash).
Regarding the last option, you shouldn't be in this scenario because your data should live on the host system and your container should be disposable.
docker exec -it mongodb_container bash -c "mongo db-name --eval 'db.dropDatabase()'"
I had a case with slightly different requirements:
docker-compose
authentication needed
bash variable expansion (the container was running with environemt variables MONGO_USER & MONGO_PASSWORD already set)
So, for anyone who might need a handy one-liner to authenticate and drop a mongo db with docker, here it is đŸ‘‡:
docker-compose exec mongo_container_name /bin/bash -c 'mongo database_name -u $MONGO_USER -p $MONGO_PASSWORD --authenticationDatabase admin --eval "db.dropDatabase();"'
And if you add a Makefile to the "mix", it's exactly the same but it needs $$ to the variables:
db-reset:
docker-compose exec mongo_container_name /bin/bash -c 'mongo database_name -u $$MONGO_USER -p $$MONGO_PASSWORD --authenticationDatabase admin --eval "db.dropDatabase();"'
I am a newbie to mongodb. I am trying to take a backup of my database using mongodump. But whenever I use this command I get the below error
Referenceerror: mongodump is not defined
I tried creating a new user with all the roles but still I get the same error. Should I add a specific role to take a backup? Or am I doing something wrong?
'mongodump' is a command/tool which is included in the 'mongodb-tools' package. If you don't have this package installed on your machine, it makes sense that it is not defined. The mongodb-tools also provide several other tools used for importing and exporting DBs (like mongorestore).
That being said, 'mongodump' is not a mongo-shell command, you shouldn't be using it in mongo-shell. It's a whole different command that you would be executing just like you execute 'mongod' or 'mongo' etc.
One simple way is to right-click mongodump.exe and "Run as administrator". It will create dump folder in bin of your mongodb home containing all database backups.
If you want to go with commands, open cmd as administrator and go to bin of your mongodb where you'll be able to fire commands such as mongorestore, mongodump etc. with intended parameters e.g for specific db or interact with remote mongodb.
tested on (v4.2.3)
Note: many folks try to execute these commands in mongod where you can execute queries which is wrong, these tools needs to be used separately.
Here ar e 2 simple exemples for a full backup using authentication and without
mongodump -h hostname -v -u sys_account -p ys_password --authenticationDatabase admin --out folder_location_for_backup
if no authentication
mongodump -h hostname -v --out folder_location_for_backup
here are the mongorestore commands as well
mongorestore -h hostname -v -u admin_user -p admin_password --authenticationDatabase admin --dir folder_location_where_backup_is_located
if no authentication
mongorestore -h hostname -v --dir folder_location_where_backup_is_located
for windows, you need to start executable file by running following command
"C:\Program Files\MongoDB\Server\3.4\bin\mongodump.exe" --db your_database_name
Above command will export your database to dump folder. This folder will be located where you have kept your "data" folder. If you use default "data/db" folder then it will be there but if you use different location then it will be kept over there.
This command must be run in normal command prompt, not in mongo shell. It is executable not mongo shell command. Official docs link is here.
Download MongoDB Command Line Database Tools .The mongodump tool is part of the MongoDB Database Tools package.
There are great chances that mongodump is being performed under Mongo Shell
mongodump should be run directly on command line or terminal (NOT inside mongo shell)
For creating dump of database
mongo --db database_name
For creating dump of any collection of database
mongo --db databas_name --collection collection_name
I am starting my first meteor app, using MongoDB for my database.
I have copied my data in from a JSON file into mongodb collection and when hosting locally, the data appears as expected; the collection is in the meteor mongo database and I can interpret at will
When I deploy the app to xx.meteor.com, the meteor collection I need (named 'assets') does not get posted to the mongodb on the server. I can check this by using meteor mongo onemore.meteor.com. Other collections are posted though.
How do I deploy my mongoDB colletion ('assets') along with the app using meteor deploy?
Is this issue due to the way the mongodatabase was orginally imported? I used this method: https://github.com/awatson1978/meteor-cookbook/blob/master/cookbook/database-management.md
I copied the collection ('assets) from a staging database using db.copyDatabase('staging3','meteor','localhost')
Using mongodump and mongorestore also works:
Dump data from existing mongodb (mongodb url: mongodb://USER:PASSWORD#DBHOST/DBNAME)
mongodump -h DBHOST -d DBNAME -u USER -p PASSWORD
This will create a "dump" directory, with all the data going to dump/DBNAME.
Get the mongodb url for the deployed meteor app (i.e. www.mymeteorapp.com)
meteor mongo --url METEOR_APP_URL
This will return the following:
mongodb://#USERNAME#:#PASSWORD###HOSTNAMEANDPORT#/#YOURAPPLICATION#
Note: the PASSWORD expires every min.
Upload the db dump data to the meteor app (using an example meteor db url)
mongorestore -u #USERNAME# -p #PASSWORD# -h #HOSTNAMEANDPORT# -d www_mymeteorapp_com dump/DBNAME/
All the data should get transferred!
This answer is basically a modified version of Davidd8's answer here, but because that was never accepted I reposted it here.
None of the above worked for me. Here is what did the trick for me:
1) How do I get my localhost dump of my meteor db?
tip 1: make sure mongo is running (type mongod to run it)
Which port was my meteor running on?
$ mongodump --host localhost:3002 (nope)
$ mongodump --host localhost:3000 (nope)
$ mongodump --host localhost:3001 (yes!) this worked
Now where the heck did it put my dump?
$ ls (shows me the dump is in my current directory and meteor is inside the dump directory)
I need to somehow get that meteor data onto my live site on meteor.com
Now the next problem is that meteor.com will give me the information I need but I only have 1 minute before their information expires.
This information is what I need to connect to my remote meteor site and import my local meteor data.
I type this in the terminal:
$ meteor mongo --url iamcool.meteor.com
Then it spits back something like this:
mongodb://client-11f3014w:f1c52f68-8506-b682-c880-b5db03a9510e#production-db-a2.meteor.io:27017/iamcool_meteor_com
I used info from above that told me what data to extract from the above code and where to put it in the mongorestore code
mongorestore -u #USERNAME# -p #PASSWORD# -h #HOSTNAMEANDPORT# -db www_mymeteorapp_com dump/DBNAME/
This is where you have to be a super fast typer. I recommend opening a blank file in your IDE (Sublime Text or Atom or whatever you use) and create this template:
mongorestore -u XXX -p XXX -h production-db-a2.meteor.io:27017 -db iamcool_meteor_com dump/meteor
So I took the data meteor.com gave me and I plugged it in below:
mongorestore -u client-4bf9be36 -p d1cdef86-6c71-3b11-ef4c-221fbdcf07d0 -h production-db-a2.meteor.io:27017 -db iamcool_meteor_com dump/meteor
And then the errors came. And boy did they come.
Sometimes I got this error:
too many positional arguments
Sometimes I got this error:
Failed: error connecting to db server: auth fails
But this error tip gave me the info that saved the day:
try 'mongorestore --help' for more information
I did and I found out that the flag to pass for database wasn't -db but rather -d
I made the following change and typed it fast using my trusty template (see above)
mongorestore -u client-4bf9be36 -p d1cdef86-6c71-3b11-ef4c-221fbdcf07d0 -h production-db-a2.meteor.io:27017 -d iamcool_meteor_com dump/meteor
And then it worked.
It took me two hours to figure out. Hope it saves you the time I lost.
This post is about backing up your meteor database
I am trying to back up my meteor database and I understand what this post is telling me to do, but I must not be in the right directory when I run the mongodump command, b/c I keep getting 'Command not found'. Or do I need to export a path?
[EDIT]
OK, now I have the binaries installed but when I run 'mongodump', I get:
couldn't connect to [127.0.0.1] couldn't connect to server 127.0.0.1:27017
... and when I run 'mongodump --host localhost:3002', I get:
couldn't connect to [localhost:3002] couldn't connect to server localhost:3002
Now what?
OK, thanks to #David Weldon, I can provide a fairly complete answer to this issue:
Backing up and restoring your local MongoDB for Meteor users (OSX)
Backup:
1) Your app must be running, so start up your Meteor server.
2) In a terminal window (NOT in the meteor mongo shell), enter: mongodump -h 127.0.0.1 --port 3001 -d meteor
This will create a 'dump' directory inside your home folder (your name under Users).
3) If you get a 'command not found' message, you probably just installed Mongo as a part of Meteor, meaning you don't have the mongo command line tools. Use a package like Homebrew to reinstall Mongo and you will have the command line tools. This will also add the correct PATH information to your system, so that it can find the tools.
Restoring:
1) From MiniMongo shell (run ‘meteor mongo’ inside your Meteor project dir), enter:
db.[collectionName].drop(); //repeat for all collections you wish to restore
2) Then, from a terminal window, enter:
mongorestore -h 127.0.0.1 --port 3001 -d meteor dump/meteor
Caveats:
The individual documents will not necessarily be in the same order after they are restored. So you need some way to sort documents that need to be presented in a certain order.
Caveats:
The individual documents will not necessarily be in the same order after they are restored. So you need some way to sort documents that need to be presented in a certain order.
There is a flag for this
mongorestore --maintainInsertionOrder
If you are restoring to a mup'ed meteor app, the command would be:
mongorestore -h 127.0.0.1 --port 3001 -d dump/meteor
If you forgot the name of your app, you can see it by going into the mongo shell and listing all databases.