Strapi: MongoDB cluster connection

Created on 9 Jan 2018  ·  48Comments  ·  Source: strapi/strapi

can i use https://cloud.mongodb.com as DB ?

medium confirmed bug

Most helpful comment

@lauriejim is correct, the problem has to do with mongoose and replica sets. Found something that worked for me:

  • start a mongo instance with docker: docker run -p 27017:27017 mongo
  • invoke strapi new [project] with default options
  • after strapi new finishes, but before you run strapi start, edit your config at /config/environments/development/database.json

    • Remove all properties (except client)

    • Add a uri property using the format:

mongodb://<USERNAME>:<PASSWORD>@your-db-shard-00-00-a3bbg.mongodb.net:27017,your-db-shard-00-01-a3bbg.mongodb.net:27017,your-db-shard-00-02-a3bbg.mongodb.net:27017/[db name you used in setup]?ssl=true&replicaSet=your-db-shard-0&authSource=admin
  • invoke strapi start from your project dir
  • navigate to: http://127.0.0.1:1337/admin and create your admin account

cc: @asharamseervi, @blondie63

All 48 comments

@blondie63 Yes you can. Set your database connection informations in /config/environments/development/database.json or via the settings manager plugin in your admin dashboard

Do you have an example of setting for mongodb cloud atlas?

Inviato da iPhone Mauro

Il giorno 10 gen 2018, alle ore 09:47, Jim LAURIE notifications@github.com ha scritto:

@blondie63 Yes you can. Set you database connection informations in /config/environments/development/database.json or via the settings manager plugin in your admin dashboard


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.

@blondie63
Here is link to Strapi Documentation
https://strapi.io/documentation/configurations/configurations.html#database

Remember to allow access to the database from a given IP address that uses your application or set global access.

./config/environments/**/database.json

{
  "defaultConnection": "default",
  "connections": {
    "default": {
      "connector": "strapi-mongoose",
      "settings": {
        "client": "mongo",
        "host": "<YOUR_CLUSTER>.mongodb.net",
        "port": 27017,
        "database": "<YOUR_DATABASE>",
        "username": "<YOUR_USER>",
        "password": "<YOUR_PASS>"
      },
      "options": {}
    }
  }
}

My scenario is:
Desktop for dev: iMac High Sierra
Local VM on vmware esx server with MongoDB 3.6
Account Mongo Atlas with a cluster just ready

On my desktop i’ve installed MongoDB Compass community edition and i can manage both MongoDB without problems, so there aren’t connection problems

On my Strapi test prj i’ve tried these two connections:
FIRST
{
"defaultConnection": "default",
"connections": {
"default": {
"connector": "strapi-mongoose",
"settings": {
"client": "mongo",
"host": "cluster0-shard-00-00-ccfvh.mongodb.net",
"port": 27017,
"database": "Test1",
"username": "MGadmin",
"password": "mypsw"
},
"options": {}
}
}
}

SECOND
{
"defaultConnection": "default",
"connections": {
"default": {
"connector": "strapi-mongoose",
"settings": {
"client": "mongo",
"host": "192.168.1.217",
"port": 27017,
"database": "mm",
"username": "root",
"password": "mypsw"
},
"options": {}
}
}
}

In both cases this is the log:
iMac-di-Mauro:test mauro$ strapi start
DEBUG (34754 on iMac-di-Mauro.local): Server wasn't able to start properly.
ERROR (34754 on iMac-di-Mauro.local): (hook:mongoose) takes too long to load

What's wrong ?
Thanks

Are you able to connect to these mongodb cloud database with a tiers software as robomongo (https://robomongo.org)?

yes i can connect also with this client without problems, tested now both connections

yeah !! i've started my first prj with mysql and alpha 8.3 :)

This issue come from Strapi not handle cluster connection I think.

If you fixed it to use your mongodb cloud database, you can submit a PR to improve the project 👍

We published an article explaining how to use Strapi with mLab. We hope it will help.

Hi, me too. Me too facing same issue as @blondie63 !! And, similarly I'm able to connect with compass & 3t, but not able to configure strapi with Mongo Atlas Clusters. I'm successfully able to get connected with mLab as mentioned, but mlab sandbox is the way slower to respond.

Hence, looking forward to getting this issue fixed as soon as possible, & happy to get connected with MongoDB Atlas very soon. Me too on hold.

@lauriejim is correct, the problem has to do with mongoose and replica sets. Found something that worked for me:

  • start a mongo instance with docker: docker run -p 27017:27017 mongo
  • invoke strapi new [project] with default options
  • after strapi new finishes, but before you run strapi start, edit your config at /config/environments/development/database.json

    • Remove all properties (except client)

    • Add a uri property using the format:

mongodb://<USERNAME>:<PASSWORD>@your-db-shard-00-00-a3bbg.mongodb.net:27017,your-db-shard-00-01-a3bbg.mongodb.net:27017,your-db-shard-00-02-a3bbg.mongodb.net:27017/[db name you used in setup]?ssl=true&replicaSet=your-db-shard-0&authSource=admin
  • invoke strapi start from your project dir
  • navigate to: http://127.0.0.1:1337/admin and create your admin account

cc: @asharamseervi, @blondie63

Hi! I have the same problem connecting to MongoDB Cluster. I have followed the @adamflater guide, but I get this error when I run strapi start:

[2018-04-26T13:28:27.192Z] debug Server wasn't able to start properly.
(hook:mongoose) takes too long to load

Connection to mLab is working by the way

@seleckis can you try to update in /config/hook.json the timeout time.

The main problem here is that it says that the uri has a slash.

I tried the same as https://github.com/strapi/strapi/issues/432#issuecomment-372837676
but it gave me the following error:

(node:7904) UnhandledPromiseRejectionWarning: Error: Slash in host identifier at parseConnectionString (C:\Users\X220\Projects\strapi\MIN\node_modules\mongodb\lib\url_parser.js:219:15) at parseHandler (C:\Users\X220\Projects\strapi\MIN\node_modules\mongodb\lib\url_parser.js:129:14) at module.exports (C:\Users\X220\Projects\strapi\MIN\node_modules\mongodb\lib\url_parser.js:25:12) at connect (C:\Users\X220\Projects\strapi\MIN\node_modules\mongodb\lib\mongo_client.js:878:3) at connectOp (C:\Users\X220\Projects\strapi\MIN\node_modules\mongodb\lib\mongo_client.js:268:3) at executeOperation (C:\Users\X220\Projects\strapi\MIN\node_modules\mongodb\lib\utils.js:420:24) at MongoClient.connect (C:\Users\X220\Projects\strapi\MIN\node_modules\mongodb\lib\mongo_client.js:259:10) at Promise (C:\Users\X220\Projects\strapi\MIN\node_modules\mongoose\lib\connection.js:427:12) at new Promise (<anonymous>) at NativeConnection.Connection.openUri (C:\Users\X220\Projects\strapi\MIN\node_modules\mongoose\lib\connection.js:424:19) at Mongoose.connect (C:\Users\X220\Projects\strapi\MIN\node_modules\mongoose\lib\index.js:207:15) at _.forEach (C:\Users\X220\Projects\strapi\MIN\node_modules\strapi-mongoose\lib\index.js:60:18) at C:\Users\X220\Projects\strapi\MIN\node_modules\lodash\lodash.js:4925:15 at baseForOwn (C:\Users\X220\Projects\strapi\MIN\node_modules\lodash\lodash.js:3010:24) at C:\Users\X220\Projects\strapi\MIN\node_modules\lodash\lodash.js:4894:18 at Function.forEach (C:\Users\X220\Projects\strapi\MIN\node_modules\lodash\lodash.js:9342:14) at Function.initialize (C:\Users\X220\Projects\strapi\MIN\node_modules\strapi-mongoose\lib\index.js:43:9) at C:\Users\X220\AppData\Roaming\npm\node_modules\strapi\lib\hooks\index.js:21:31 at Promise (C:\Users\X220\AppData\Roaming\npm\node_modules\strapi\lib\hooks\index.js:123:37) at new Promise (<anonymous>) at Promise.all.Object.keys.map.hook (C:\Users\X220\AppData\Roaming\npm\node_modules\strapi\lib\hooks\index.js:64:9) at Array.map (<anonymous>) (node:7904) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2) (node:7904) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. [2018-05-03T17:04:08.782Z] debug Server wasn't able to start properly. (hook:mongoose) takes too long to load

Annoyed by this problem, I took a few minutes to debug it. The solution is actually surprisingly simple - Strapi is passing a default ssl: false when connecting to the URI, and it's required to be true. The following works:

    "default": {
      "connector": "strapi-mongoose",
      "settings": {
        "client": "mongo",
        "uri": "mongodb+srv://USERNAME:[email protected]/DBNAME"
      },
      "options": {
        "ssl": true
      }
    }

Use the driver 3.6+ style URI (mongodb+srv) as shown to you in the Atlas config modal.

That does not work for me.
It still says:

debug Server wasn't able to start properly.
Make sure your MongoDB database is running

@commentatorboy hmm, really! And you're able to connect to the same mongo+srv://... URL using the mongo cli tools, yes?

Yes. I just use the default shell connection
mongo "mongodb+srv://CLUSTERNAME.mongodb.net/DBNAME" --username USERNAME

Btw. should there not be a password in the configuration? Like there should be a username and password I assume to be able to connect to the database?

@commentatorboy yes, there must be both username and password in the URI. they go at the front, before the hostname, and are separated by a colon mongo+srv://USERNAME:PASSWORD@HOSTNAME/DBNAME

Ok I tried it this way:
mongodb+srv://USERNAME:[email protected]/DBNAME
But still says the same thing.

I tried to run the above configuration in the cli and it still worked.

in my case CLUSTERNAME is for example randomname-cluster-58f23

Ah Ok I just found something out...

I created a new database called local where it has different documents inside of it.

This returned the following error:

events.js:183
throw er; // Unhandled 'error' event
^

Error: write EPIPE
at _errnoException (util.js:1022:11)
at ChildProcess.target._send (internal/child_process.js:702:20)
at ChildProcess.target.send (internal/child_process.js:586:19)
at sendHelper (internal/cluster/utils.js:25:15)
at send (internal/cluster/master.js:352:10)
at Worker.disconnect (internal/cluster/master.js:358:3)
at Worker.destroy (internal/cluster/master.js:371:10)
at Worker.kill (internal/cluster/worker.js:50:16)
at _.forEach.worker (C:\Users\X220\AppData\Roaming\npm\node_modules\strapi\bin\strapi-start.js:99:59)
at C:\Users\X220\AppData\Roaming\npm\node_modules\strapi\node_modules\lodash\lodash.js:4925:15

Is this issue fixed?

@blondie63 any update on this issue? Just wanted to see if this is something we could close out.

@derrickmehaffy I think it can be closed.

@pierreburgy Alrighty :smiley:

@lauriejim you want to close this?

I have not been able to fix this issue yet. But I have not tried to do anything since last time I posted.
If it is alright with you I want to take the time during the summer to see if it works?

This issue still exits.
MongoDB client is earlier than 3.4 version. So it need replica set servers to connect to the database. And Strapi not accept replica set.

On our Strapi setup mongo client is already installing with support for 3.6. Do you have an older version pegged in package.json?

You will have to use uri params in database.json file (check production env file)

It works using uri property in development configuration:

https://github.com/strapi/strapi/issues/636#issuecomment-422484168

I just solve the same problem.
I choose MongoDB to connect for application and get connection for srv for MongoDB 3.6+
My string is :
mongodb+srv://username:[email protected]/test?retryWrites=true

in installation prompt put for database: test
host: interview-zgg4h.mongodb.net
srv: true
username: username
password: password
authentication: admin
ssl: true

and works properly!! :+1:

@lauriejim is correct, the problem has to do with mongoose and replica sets. Found something that worked for me:

  • start a mongo instance with docker: docker run -p 27017:27017 mongo
  • invoke strapi new [project] with default options
  • after strapi new finishes, but before you run strapi start, edit your config at /config/environments/development/database.json

    • Remove all properties (except client)
    • Add a uri property using the format:
mongodb://<USERNAME>:<PASSWORD>@your-db-shard-00-00-a3bbg.mongodb.net:27017,your-db-shard-00-01-a3bbg.mongodb.net:27017,your-db-shard-00-02-a3bbg.mongodb.net:27017/[db name you used in setup]?ssl=true&replicaSet=your-db-shard-0&authSource=admin
  • invoke strapi start from your project dir
  • navigate to: http://127.0.0.1:1337/admin and create your admin account

cc: @asharamseervi, @blondie63

@adamflater For Production Environment, should I use the same configurations?

Annoyed by this problem, I took a few minutes to debug it. The solution is actually surprisingly simple - Strapi is passing a default ssl: false when connecting to the URI, and it's required to be true. The following works:

    "default": {
      "connector": "strapi-mongoose",
      "settings": {
        "client": "mongo",
        "uri": "mongodb+srv://USERNAME:[email protected]/DBNAME"
      },
      "options": {
        "ssl": true
      }
    }

Use the driver 3.6+ style URI (mongodb+srv) as shown to you in the Atlas config modal.

Tks it works

@nhtuan2608 do you know what is difference between
"strapi-mongoose" and `"strapi-hook-mongoose"

{
  "defaultConnection": "default",
  "connections": {
    "default": {
      "connector": "strapi-hook-mongoose",
      "settings": {
        "uri": "mongodb+srv://USER:PASSWORD@CLUSTER/DB",
        "client": "mongo"
      },
      "options": {
        "authenticationDatabase": "test",
        "ssl" : "true"
      }
    }
  }
}

@tigrankhachikyan strapi-mongoose was the name of the old package which is no longer maintained. strapi-hook-mongoose and likewise with the hooks bookshelf and knex are the new ones.

I tried all the above solutions but I get an entirely different error :(

This is my database.json:

"ootje-db": {
      "connector": "strapi-hook-mongoose",
      "settings": {
        "client": "mongo",
        "uri": "mongodb+srv://user:[email protected]/ootje"
      },
      "options": {
        "ssl": true
      }
    }

I am able to connect to the cluster via mongodb compass.

When I run strapi start I get the following error in the console:

_stream_readable.js:242
          Object.getPrototypeOf(chunk) !== Buffer.prototype) {
                 ^

TypeError: Object.getPrototypeOf is not a function
    at readableAddChunk (_stream_readable.js:242:18)
    at TLSSocket.Readable.push (_stream_readable.js:219:10)
    at TLSWrap.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
✨  Done in 3.56s.

Am I missing something here? I'm new to strapi so might be doing something wrong :)

@Temmermans this could be a few things. I know that there are some issues with certain symbols in passwords: https://github.com/strapi/strapi/issues/2441

Also (can't remember off the top of my head if this was fixed) but I believe there was an issue with the SRV connection setting. You may want to search through some of the old issues about it, alternative being manually break that srv connection down and set each setting on their own.

@derrickmehaffy thanks for the reply! I'm aware of the pw restrictions so that's not the issue. I don't know why, but it is suddenly working. The only thing I did was adding the mongoose hook to the hooks array in config/hook.json:

{
  "timeout": 100000,
  "load": {
    "before": [],
    "order": ["strapi-hook-mongoose"],
    "after": []
  }
}

Could that have been the issue?

@Temmermans that's odd, can you feed me your package.json? My guess is there is multiple hooks in there and maybe you tried in the past to use the quickstart option which is SQLite

I'm not at my pc atm. I didnt change package.json of strapi but I did start the project with the quickstart flag yesterday! How are those related? Do I need to clear cache after using that flag?

In case it helps, I was able to connect locally to a Mongo Atlas instance (development config) configured like this:

"connector": "strapi-hook-mongoose",
  "settings": {
    "client": "mongo",
    "uri": "mongodb+srv://USERNAME:[email protected]/DATABASE_NAME?retryWrites=true"
  },
"options": {
  "ssl": true
}

As @nhtuan2608 mentioned, ssl is set to false by default.

I just solve the same problem.
I choose MongoDB to connect for application and get connection for srv for MongoDB 3.6+
My string is :
mongodb+srv://username:[email protected]/test?retryWrites=true

in installation prompt put for database: test
host: interview-zgg4h.mongodb.net
srv: true
username: username
password: password
authentication: admin
ssl: true

and works properly!!

Tried several options and this worked for me.

Thank you

I just solve the same problem.
I choose MongoDB to connect for application and get connection for srv for MongoDB 3.6+
My string is :
mongodb+srv://username:[email protected]/test?retryWrites=true
in installation prompt put for database: test
host: interview-zgg4h.mongodb.net
srv: true
username: username
password: password
authentication: admin
ssl: true
and works properly!!

Tried several options and this worked for me.

Thank you

This worked for me except when I tried to start the project with "npm run develop", i got an error connecting to Mongo. It turns out the generated the database.json file puts "yes" instead of "true" for the srv portion: Here is the diff:

--- a/config/environments/development/database.json
+++ b/config/environments/development/database.json
@@ -6,7 +6,7 @@
       "settings": {
         "database": "mydb",
         "host": "mydb-cluster0-xxxxcm.mongodb.net",
-        "srv": "yes",
+        "srv": "true",
         "port": 27017,
         "username": "root",
         "password": "welcome1"

I'm not at my pc atm. I didnt change package.json of strapi but I did start the project with the quickstart flag yesterday! How are those related? Do I need to clear cache after using that flag?

@Temmermans - When you use Quickstart it install SQLite. And you CAN go from SQLite to PostgreSQL, MariaDB or MYSql. But you CANNOT go to MongoDB. There --quickstart is not possible if you want to use MongoDB in production.

You will need to prototype on your dev with a mongodb running. That is the relationship between these two points.

BTW - did everything resolve otherwise?

The string connection in cluster mongodb is pretty weird. You can see one tutorial that I have double checked and works fine with mongodb atlas and mlab providers as well.
https://link.medium.com/gaDCSaX8OY

@xargr MongoDB connection strings are a little bit strange, there are some example of SRV-record based connection strings as is the case for MongoDB Atlas. For replicasets, you have another type of string (effectively an array of servers) and for single installations like a local docker setup, you have another format. In addition to that, you also have different requirements depending on your setup, as to whether you have to specify things like an authentication database as a URL argument.

I would probably suggest that the documentation is updated to reflect the above. Replicasets are a given in a production MongoDB deployment. It's quite a good setup to use something like KubeDB as well on Kubernetes. There seems to be a lot of demand for strapi on Docker and Kubernetes.

String connection in MongoDB sometimes behaves strangely. However, I followed this article and it works fine for me.

https://www.mstweaks.com/strapi-with-mongodb-and-mongodb-atlas/

I encountered this issue as well, the only thing I have to set is the ssl to true. I wish this could have been caught on explicit logging.

Was this page helpful?
0 / 5 - 0 ratings