Skip to content

Instantly share code, notes, and snippets.

@derrickmehaffy
Last active May 29, 2020 09:11
Show Gist options
  • Select an option

  • Save derrickmehaffy/aea3c635e0b9704b14fd1693fc56fea2 to your computer and use it in GitHub Desktop.

Select an option

Save derrickmehaffy/aea3c635e0b9704b14fd1693fc56fea2 to your computer and use it in GitHub Desktop.
Strapi MongoDB URI example
{
"defaultConnection": "default",
"connections": {
"default": {
"connector": "mongoose",
"settings": {
"uri": "mongodb://strapi:[email protected]:27017,strapi-test-shard-00-01-rqvys.gcp.mongodb.net:27017,strapi-test-shard-00-02-rqvys.gcp.mongodb.net:27017/test?ssl=true&replicaSet=strapi-test-shard-0&authSource=admin&retryWrites=true&w=majority"
},
"options": {
"authenticationDatabase": "admin",
"ssl": true
}
}
}
}
@joeczar
Copy link

joeczar commented May 26, 2020

I just build a new project and set it up with a local mongoDB database.

`{
"defaultConnection": "default",
"connections": {
"default": {
"connector": "mongoose",
"settings": {
"database": "goal_tracker",
"host": "127.0.0.1",
"srv": false,
"port": 27017,
"username": "",
"password": ""
},
"options": {
"authenticationDatabase": ""
}
}
}
}

`
It fires up fine. Then I replace the "config/enviroments/development/database.json" with the Atlas setup:

`{
"defaultConnection": "default",
"connections": {
"default": {
"connector": "mongoose",
"settings": {
"uri": "$mongodb://user:[email protected]:27017,goal-tracker-shard-00-01-f72do.mongodb.net:27017,goal-tracker-shard-00-02-f72do.mongodb.net:27017/test?ssl=true&replicaSet=Goal-Tracker-shard-0&authSource=admin&retryWrites=true&w=majority"

  },
  "options": {
    "authenticationDatabase": "admin" 
    "ssl": true
  }
}

}
}
`
And i get the following error:

[2020-05-26T12:21:10.181Z] debug ⛔️ Server wasn't able to start properly. [2020-05-26T12:21:10.183Z] error Error connecting to the Mongo database. Server selection timed out after 30000 ms error Command failed with exit code 1. info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

@joeczar
Copy link

joeczar commented May 26, 2020

Sorry about the formatting, still getting the hang of this.

@derrickmehaffy
Copy link
Author

your URI looks wrong that leading $

@joeczar
Copy link

joeczar commented May 26, 2020

Thanks, I removed it and am still getting the same error. I have double-checked my password and am using the uri link direct from Atlas.

@derrickmehaffy
Copy link
Author

Outside of that I really don't know. I quite literally just spun up an atlas cluster about an hour ago and an EC2 Instance and had no issues there. 🤔

@joeczar
Copy link

joeczar commented May 26, 2020

Well, thanks. I'll try a new cluster, maybe I missed something.

@derrickmehaffy
Copy link
Author

something to consider that I just thought of, if you are using a remote Atlas database and trying to run it locally. Your local connection may be too slow to support this. (Databases, even Mongo Atlas clusters have a limit to what they can do in extremely high latency environments.)

While I personally don't agree with how Atlas works by sending what should be low latency connections over the public internet (encrypted or not), it's purpose is that of clouds, not local development.

@joeczar
Copy link

joeczar commented May 26, 2020

The end goal is just to get a prototype up and running on Heroku. I was hoping Strapi would be a quick and easy solution. Deploying it on Heroku has really been no fun though.

I can connect now with atlas through MongoDBCompas, so It's working, but Strapi A) doesn't give helpful errors for missed connections and B) just won't connect.

I'm currently looking at AWS options and hoping that will work.

@ellis-sutehall
Copy link

I'm having the same problem. Absolute nightmare trying to make it work on Heroku.
I think the problem is the recent release of the Stable Version without updating the docs.
Have you had any luck in making it work?

@joeczar
Copy link

joeczar commented May 28, 2020

I'm having the same problem. Absolute nightmare trying to make it work on Heroku.
I think the problem is the recent release of the Stable Version without updating the docs.
Have you had any luck in making it work?

I was able to get it working, but I had to have Atlas Open (0.0.0.0) and I wasn't soo keen on that. I got it running on my personal server with NOIP and a lets Encrypt certificate atm because I just wanted to get going.

@ellis-sutehall
Copy link

I have already set me network access to be open everywhere, set user permissions for the database user, checked my password, encoded special characters and still nothing works.

I can connect to it locally and through MongoDB Compass just Heroku always fails with "invalid connection string".

@joeczar
Copy link

joeczar commented May 28, 2020

Sorry, I forgot to mention that instead of the uri string from "Connect to your App" I tried the string from Connect to MongoDB Compass. It works on both local and Heroku instances, which is ideal because I can use the Dev env on my local machine and it updates the Production API as well.
MongoDB Atlas

@ellis-sutehall
Copy link

Thanks I'll give that a try, is there any reason (security) why using that might be a bad idea?

@joeczar
Copy link

joeczar commented May 28, 2020

@ellis-sutehall
Copy link

I actually finally managed to make this work using the URI string. Hopefully Strapi will update their official documentation now the stable release is out and things have changed quite a lot for deploying to Heroku.

Looks like the handling of environments has totally changed as has the reading the of environment variables.

In case anyone else stumbles on this thread here's my complete database file. This works for both production and local. I created a .env.production file to save all my environment variables in to test the connection locally. Then you'll need to copy them into Heroku as well. This can be done with the CLI or in the GUI for the webapp. I recommend using the GUI as the CLI was confused by some of the query params on the end of the string (at least for me).

module.exports = ({ env }) => ({ defaultConnection: "default", connections: { default: { connector: "mongoose", settings: { client: "mongo", uri: env("DATABASE_HOST"), host: env("DATABASE_HOST", "127.0.0.1"), srv: env.bool("DATABASE_SRV", false), port: env.int("DATABASE_PORT", 27017), database: env("DATABASE_NAME", "strapi_portfolio"), username: env("DATABASE_USERNAME", ""), password: env("DATABASE_PASSWORD", ""), }, options: { authenticationDatabase: env("AUTHENTICATION_DATABASE", null), ssl: env.bool("DATABASE_SSL", false), }, }, }, });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment