Setting MONGO_URL not applying remote mongo connection to Atlas from local run

I’m trying to connect to a remote mongodb setup on Atlas from a local run.
I have a normal mongo url from compose:

MONGO_URL=mongodb://[username]:[password]@aws-us-east-1-portal.21.dblayer.com:10170/[database]

This works fine for remote connection on local runs and for deployment to Galaxy.

I have made a more complex replica set setup with Atlas and would like to connect to that database set from local development and in production.
The Atlas url looks like this:

MONGO_URL=mongodb://[username]:[password]@cluster0-shard-00-00-xgnuk.mongodb.net:27017,cluster0-shard-00-01-xgnuk.mongodb.net:27017,cluster0-shard-00-02-xgnuk.mongodb.net:27017/[database]?ssl=true&replicaSet=Cluster0-shard-0&readPreference=primaryPreferred&w=majority&authSource=admin

When I deploy to Galaxy, my app connects successfully to the desired Atlas database. However, when I try to set the url locally to the remote database, like I use to with the compose url, my local app instance only connects to my local mongodb with no warnings or errors.

I tried playing around with the option settings in the url, but with no success. Any ideas how I can achieve this?

(No sharding is enabled)

When you say “local run” do you mean in development mode (i.e. using the meteor command)?

Are you doing one of the following?

MONGO_URL=yourUrlString meteor

or

export MONGO_URL=yourUrlString
meteor

It’s not enough to do

MONGO_URL=yourUrlString
meteor
1 Like

Yes. Just running the app as development on my local machine.

So I tried:
MONGO_URL=myUrlString meteor --settings set-dev.json --port 3000 run
or:
export MONGO_URL=myUrlString meteor --settings set-dev.json --port 3000 run
or:
export MONGO_URL=myUrlString meteor --settings set-dev.json --port 3000 run
and all variations of this and makes no difference.

I have also tried this on a blank new meteor app just to make sure its not something within my app or settings, but it yields the same result. Again running the remote with a simple Compose mongo string works fine and if I test the Atlas string in MongoChef it does connect. So I’m suspecting it must be something in the Atlas string.

Hmm. That looks like it should work. I vaguely remember that some people were having problems if the password contained “weird” characters (can’t recollect the definition of weird in this context), but if it works elsewhere it’s probably not that.

1 Like

I also had problems with that before, so I always choose alphanumerics, so that can’t be the case. I’m starting to wonder if the ‘-’ char in the replicaSet name might be causing problems? However I can’t remember selecting this, think it was autogenerated in Atlas setup.

So I’m finding that the problem is definitely with the ‘&’ char in the string.

From Mongodb:

SEMI-COLON SEPARATOR FOR CONNECTION STRING ARGUMENTS
To provide backwards compatibility, drivers currently accept semi-colons (i.e. ;) as option separators.

https://docs.mongodb.com/manual/reference/connection-string/

So when I replace ‘&’ with ‘;’ the var is set and I can do:

echo "$MONGO_URL"

and it gives me the URL up to the first ';'
If I pass the string with ‘&’ the var is not set at all. So how can I set all the options since ‘;’ is only setting the first option? (i.e. /database?ssl=true - and the rest don’t follow) Sorry I’m a noob to bash.

Try quoting your url:

MONGO_URL='mongodb://[username]:[password]@cluster0-shard-00-00-xgnuk.mongodb.net:27017,cluster0-shard-00-01-xgnuk.mongodb.net:27017,cluster0-shard-00-02-xgnuk.mongodb.net:27017/[database]?ssl=true&replicaSet=Cluster0-shard-0&readPreference=primaryPreferred&w=majority&authSource=admin'
3 Likes

Ouch!! :astonished: Thanks for the help! Finally all works.

1 Like

You’re welcome! I should have seen those pesky ampersands from the start!

What file does the export string go into to make meteor connect to real MongoDB ?

It’s an environment variable, so the setting of it depends on how you’re deploying your app.

Woow, thank you :smiley:

Was banging my head against the wall for an hour…

1 Like