How to connect to external MongoDB instance in Meteor?
Asked Answered
C

4

9

I would like to find out how to connect to an external MongoDB instance in Meteor.

I have added this environment

Meteor.startup(function () { 
process.env.MONGO_URL = 'mongodb://[UN]:PW]@[host]:[port]/meteorTest'
});

but still the data is coming from the local database.

I want to move all the collections from my local db to this external db. I read all the tutorials, its all telling me to setup this evn variable but nothing really working. How do I test whether its connected or not?

Catholicon answered 2/8, 2016 at 6:16 Comment(5)
link- Refer to the given link, hope you can find your answerDistrust
i have already read this..but couldn't able to make out anything where i need to add the line of code ins the terminal or in the meteor startup? if i add this on startup it throw erros if i add this in terminal it shows export is not a commandCatholicon
try it in terminalDistrust
i have set the variable in terminal, but when i am running meteor with that it throwing error 'MONGO_URL' is not recognized as an internal or external command, operable program or batch file.Catholicon
I've removed the urgent begging from this question, as it is likely to attract downvotes.Homoio
C
20

In my own experience; I have needed to set the environment variable before starting the meteorjs server app. To do this you will need to pass the environment variable on the command-line as you invoke meteor or preset the environment for the profile that is running the meteor app on your system.

So you would start your app with this kind of a command:

MONGO_URL='mongodb://user:[email protected]:12345/' meteor

You should also make sure that the mongodb is reachable and that your user credentials are correct! I am assuming you are trying to run meteor on your local machine using a remote mongodb instance.

On Windows

You will have to create a batch file in your meteor application folder to invoke the environment variable. There is an example of this here: https://mcmap.net/q/1170547/-unable-to-set-mongodb-path-in-meteor-on-window-system

Candi answered 2/8, 2016 at 7:25 Comment(5)
it throwing this error in the terminal 'MONGO_URL' is not recognized as an internal or external command, operable program or batch file.Catholicon
So you are on a windows machine? If so, please see this answer: https://mcmap.net/q/1170547/-unable-to-set-mongodb-path-in-meteor-on-window-systemCandi
i created a .bat file with a name 'startup.bat' but still when i am running MONGO_URL='mongodb://user:[email protected]:12345/' meteor it throwing the same errorCatholicon
You need to run the .bat file... not a command on the commandline and you need to make sure the mongodb url is configured correctly.Candi
I did this but the collections looks empty. I tried disabling autopublish, enabiling it again and checking the pub/subs, but anything has worked so far.Fatidic
T
7

I don't like to use big repeating command and I was searching for a solution where I will be setting a variable embedded with something so every time I start my meteor app; the MONGO_URL will set to environment automatically. So this what I did:

In the package.json file I replaced the start parameter as below:

"scripts": {
    "start": "MONGO_URL=mongodb://username:password@host_url:portnumber/dbname meteor run"
  },

Now every time I want to run my app; I run npm start instead of meteor or meteor run

Note: there is a disadvantage with that. Your db credentials will be exposed if you put your db credentials to package.json file and add this file to version control.

Tother answered 12/12, 2017 at 1:59 Comment(0)
A
0

run it in command prompt:

 "MONGO_URL=mongodb://<USER>:<PASSWORD>@<SERVER>:<PORT>/<DB> meteor"

or save this url in run.sh file in project folder and run meteor

Aldous answered 2/8, 2016 at 11:55 Comment(0)
I
0

On windows, I set MONGO_URL in my system's environment variable and it worked fine for me.

I have created a new environment variable and it's value as MONGO_URL=mongodb://username:password@host_url:portnumber/dbname

And in path variable, I have added %MONGO_URL%

Then in meteor app root folder, I have run $meteor

Ind answered 23/4, 2021 at 10:44 Comment(1)
Hi, welcome to StackOverflow ! You should take the Tour on StackOverflow, explaining how to properly ask a question. The community is here to help you, but you definitely need to provide more details to help us answer your question :) (such as code samples, giving more context of what you are trying to achieve, etc) The key here is to provide as much context as possible :)Phenformin

© 2022 - 2024 — McMap. All rights reserved.