You can use MONGO_URLenv variable to tell meteor to connect to your instance of mongodb. Great.
For instance:
MONGO_URL=mongodb://localhost:27017/my_project meteor works. I see the new documents in my robomongo on localhost in a database named my_project.
But if I do:
MONGO_URL=mongodb://localhost:27017/my_project meteor reset, the database my_project stays unchanged. meteor reseting works when I use the meteor-provided mongodb (when I don't supply MONGO_URL).
Things I tried:
EXPORTing MONGO_URL instead of putting it in front of the command. Doesn't help.
Also to what mongo instance do meteor connects to if I just run meteor (the default one) ? I can't find any meteor database on my localhost
If you don't specify a MONGO_URL, then mongo will create a database in your .meteor directory tree and run it on 127.0.0.1:3001. The mongo reset command seems to just remove the local files for those. To reset your external mongo db for your meteor app you can just remove the my_project db: mongo my_project -> db.dropDatabase().
From the source code for the 'reset' command
if (options.args.length !== 0) {
process.stderr.write(
"meteor reset only affects the locally stored database.\n" +
"\n" +
"To reset a deployed application use\n" +
" meteor deploy --delete appname\n" +
"followed by\n" +
" meteor deploy appname\n");
return 1;
}
Related
I cannot find anywhere to set the default connection URL for the mongo CLI. I want to be able to specify a particular username/password/hostname/database, which can all be specified as a URL. However, I want to just be able to type mongo instead of mongo "mongodb://…" in the shell.
I also don’t want the password to show up in /proc/«PID»/cmdline, but having it in a dotfile in my home directory is perfectly fine with me.
It doesn’t appear that ~/.mongorc.js allows specifying the default connection string. But I would expect such an option to be available because the mysql CLI supports ~/.my.cnf which allows you to specify username/password/hostname/database. So, where is this for mongodb?
EDIT: The solution has to work even if the mongodb at localhost is fully secured (no insecure local test database should be required—I thought this went without saying).
You can run mongo shell commands in ~/.mongorc.js. So you can just run the connect method in your ~/.mongorc.js:
db = connect("mongodb://username:password#host:27017/myDatabase")
When you run mongo it will first connect to localhost, then connect to the database passed to the connect method.
A MongoDB server must be running on localhost, otherwise the mongo shell will error and exit before trying to connect to the other DB.
Run mongo --nodb to get around needing to connect on localhost before connecting to whatever is defined in ~/.mongorc.js.
To directly launch mongo against a particular hostname/database/username/password without prompting for a password, without passing the password on the CLI, and without requiring an insecure database at mongodb://localhost/test, you can use this technique. Write a script file which does the connection for you and then launch mongo with the appropriate arguments. For example:
~/.mongo-do-auth.js
// https://docs.mongodb.com/manual/reference/program/mongo/#cmdoption-nodb
// https://docs.mongodb.com/manual/tutorial/write-scripts-for-the-mongo-shell/#mongo-shell-new-connections
//
// Run this with:
//
// mongo --nodb --shell ~/.mongo-do-auth.js
db = connect('localhost/admin', 'root', 'asdf123');
And then, simply launch it from the CLI to get an interactive session:
sam ~ # mongo --nodb --shell ~/.mongo-do-auth.js
MongoDB shell version: 2.6.12
type "help" for help
connecting to: localhost/admin
> quit()
Or, to run a script noninteractively, simply remove --shell and append your script. Consider a script:
s2.js:
print('connected to database ' + db);
To run:
sam ~ # mongo --nodb ~/.mongo-do-auth.js ~/s2.js
MongoDB shell version: 2.6.12
loading file: /root/.mongo-do-auth.js
connecting to: localhost/admin
loading file: /root/s2.js
connected to database admin
However, this requires a lot of work and invoking mongo with arguments. It appears that this is a limitation of mongo because no one has suggested any alternative which works as smoothly as the mysql CLI client’s ~/.my.cnf. This means that all shell scripts which directly invoke mongo will need to allow the the user to pass through --nodb and a path to a connection script instead of being able to rely on implicit per-user configuration.
I am running a Meteor app. locally using a Virtual Machine (VM). I want to use a new Mongo DB that I have set-up on the VM called 'my_db'. I have set the MONGO_URL on the command line like this:
export MONGO_URL='mongodb://127.0.0.1:3001/my-db'
I have checked that this environment variable is set using echo $MONGO_URL and I get back:
mongodb://127.0.0.1:3001/my-db
However, when I start up my Meteor app. it still connects to the default 'meteor' database. Why is this and what can I do to get it connecting to my alternative 'my_db' database?
Like #dr.dimitr say env vars are only used on the production process (for example connecting your modulus app with the modulus db they provide or another different), on the developing process you can use the mongo drive like this.
if(Meteor.isServer){
Meteor.startup(function () {
var myDatabase = new MongoInternals.RemoteCollectionDriver("<mongo url>");
MyCollection = new Mongo.Collection("collection_name", { _driver: myDatabase });
});
}
Now you need to know name of the url <mongo url> which could be something like mongodb://127.0.0.1:27017/local or meteor
Some Related Posts - (1) and (2)
When I meteor deploy my app, it seems to create an entirely new mongodb instance. I'd like to be able to deploy with the current mongodb have locally.
Same goes the other way -- I'd like to be able to download the mongodb back to my localhost after it has been deployed.
For clarification, I'd really like to know the follow:
1) how to deploy with a fresh mongodb
2) how to deploy to an existing deployed app without overwriting the old mongodb
3) how to download/sync mongodb locally with the existing deployed app
4) how to make local backups of mongodb
You can perform a mongo dump using meteor mongo to export your local database and deploy your app using Meteor Up which should also allow you to automate the database import and deployment process.
"Meteor Up (mup for short) is a command line tool that allows you to deploy any meteor app into your own server."
You can stop the mongodb service and start a mongod instance in a separate terminal, by just typing mongod. This will let you monitor what's happening on the mongodb instance that you just started.
Open another terminal and do export MONGO_URL=mongodb://localhost:27017/nameOfDBgoesHere
This will create a new DB named "nameOfDBgoesHere" and it won't overwrite what you currently have, unless you name it with the same name.
After that just start meteor by typing meteor in your program's folder. In the mongod terminal that you opened you should see some connections opening.
By default mongodb creates it's DB files in /data/db. If you have another meteor app and follow the same steps in another terminal, while keeping the name of the DB you specified in the MONGO_URL you will just connect to it from that app - without overwriting anything.
As for the syncing with a deployed app and the local backups of mongo - it seems like something that the mongodb website covers, but maybe someone can chime in here. Not sure if there is a meteor specific, easy way of doing this.
Is there a simple way to reset the data from a meteor deployed app?
So, for example, if I had deployed an app named test.meteor.com — how could I easily reset the data that has been collected by that app?
Locally I run meteor reset, but I am unsure of what to do in production.
If you have your app with you you could do this in your project directory
meteor deploy test.meteor.com --delete
meteor deploy test.meteor.com
The first deletes the app so its all blank. The second deploys a fresh instance of it back.
one way is to login to the mongo instance yourself and delete the relevant data
so something like per collection:
$ meteor mongo APP.meteor.com
> db.users.drop()
> db.xxx.drop()
you could just drop the whole DB, but that would confuse their env and you have to --delete the app and re-deploy anyway.
> db.dropDatabase()
I know this is a bit old, but I just changed my collection name. so in your /lib/collections.js file,
someCollection = new Mongo.Collection("originalcollection");
becomes
someCollection = new Mongo.Collection("newcollectionname");
this is assuming of course that your app generates the data for the database.
Simply you can access your meteor DB as
production-db-d2.meteor.io:27017/XYZ_meteor_com
where XYZ = your subdomain
for authentication use meteor auth (username & password)
You can access it from rockmongo, robomogo, mongoui, etc tools.
To access from command line
First authenticate by typing username, password of meteor
$ meteor login
Then
$ meteor mongo XYZ.meteor.com
I have my MONGO_URL set to mongodb://localhost:27017/meteor and have the MongoDB run as a service.
When running my project it seems OK to store data to the separate MongoDB until I tried to run meteor reset.
My assumption is it tried to remove its default database. The error complained that myproject.meteor\local is not empty and pointed to fs.js:456 which goes to files.js:256 (rm_recursive) and so on.
any idea what and how I can fix this?
$ meteor reset only resets the bundled MongoDB. It won't reset an external Mongo database.
(That's something we should explain better in the documentation.)
In your case, try connecting to the Mongo database directly (with the mongo command line shell) and running > db.dropDatabase()