Search code examples
node.jsmongodbmongoosemlabstrapi

Connect to mLab mongoDB database using nodeJS Strapi


I just got started using strapi framework and I would like to use mLab as my mongoDB database, so I go to configure strapi and fill the following details:

{
  "defaultConnection": "default",
  "connections": {
    "default": {
      "connector": "strapi-mongoose",
      "settings": {
        "client": "mongo",
        "host": "localhost",
        "port": 27017,
        "database": "development",
        "username": "",
        "password": ""
      },
      "options": {}
    }
  }
}

The details I get from mLab are:

mongodb://myUsername:myPassword@ds047891.mlab.com:41365/myDatabase

Here is my final config:

{
  "defaultConnection": "default",
  "connections": {
    "default": {
      "connector": "strapi-mongoose",
      "settings": {
        "client": "mongo",
        "host": "ds047891.mlab.com",
        "port": 41365,
        "database": "myDatabase",
        "username": "myUsername",
        "password": "myPassword"
      },
      "options": {}
    }
  }
}

When I try to start strapi, I get the following error:

DEBUG (2748 on DESKTOP-HAL1ATE): Server wasn't able to start properly.
ERROR (2748 on DESKTOP-HAL1ATE): (hook:mongoose) takes too long to load

I think that I did not setup my configuration right, but I can't pinpoint where the problem is. I hope someone could, thanks.


Solution

  • I am Pierre, on of the creators of Strapi. I tried with the following configuration and it worked well:

    { "defaultConnection": "default", "connections": { "default": { "connector": "strapi-mongoose", "settings": { "client": "mongo", "host": "ds135777.mlab.com", "port": "35777", "database": "myDatabase", "username": "myUsername", "password": "myPassword" }, "options": {} } } }

    Our configurations files look quiet similar.

    What file did you updated (/config/environment/development/database.json or /config/environment/production/database.json)?

    Are you sure you entered the correct username and password? Did you try to login to your MongoDB instance through the command line mongo ds135777.mlab.com:35777/myDatabase -u <dbuser> -p <dbpassword>?

    UPDATE

    In version >= 3 for mlab don't forget to specify authenticationDatabase

    "options": {
        "authenticationDatabase": "your_mlad_database_name",
        "ssl": false
      }