balderdashy / sails

Realtime MVC Framework for Node.js
https://sailsjs.com
MIT License
22.83k stars 1.95k forks source link

MongoError: failed to connect to server #6797

Open AliHammami opened 5 years ago

AliHammami commented 5 years ago

Node version: v10.10.0 Sails version (sails): "^1.2.3" ORM hook version (sails-hook-orm): "^2.1.1" Sockets hook version (sails-hook-sockets): "^2.0.0" Organics hook version (sails-hook-organics): Grunt hook version (sails-hook-grunt): "^4.0.1" Uploads hook version (sails-hook-uploads): DB adapter & version (e.g. sails-mysql@5.55.5): "sails-mongo": "^1.0.1" Skipper adapter & version (e.g. skipper-s3@5.55.5):


Hi everybody,

I need some help. I can't connect to Atlas mongoDB and I don't understand why.

this is the error that I get:

error: A hook (orm) failed to load! error: Could not tear down the ORM hook. Error details: Error: Consistency violation: Attempting to tear down a datastore (mongodb) which is not currently registered with this adapter. This is usually due to a race condition in userland code (e.g. attempting to tear down the same ORM instance more than once), or it could be due to a bug in this adapter. (If you get stumped, reach out at http://sailsjs.com/support.)

error: Failed to lift app: Error: Consistency violation: Unexpected error creating db connection

manager: MongoError: failed to connect to server [cluster0-kqqjs.mongodb.net:27017] on first connect [MongoError: getaddrinfo ENOTFOUND cluster0-kqqjs.mongodb.net cluster0-kqqjs.mongodb.net:27017]

Here's my datastore.js file:

/**
 * Datastores
 * (sails.config.datastores)
 *
 * A set of datastore configurations which tell Sails where to fetch or save
 * data when you execute built-in model methods like `.find()` and `.create()`.
 *
 *  > This file is mainly useful for configuring your development database,
 *  > as well as any additional one-off databases used by individual models.
 *  > Ready to go live?  Head towards `config/env/production.js`.
 *
 * For more information on configuring datastores, check out:
 * https://sailsjs.com/config/datastores
 */

module.exports.datastores = {

  /***************************************************************************
  *                                                                          *
  * Your app's default datastore.                                            *
  *                                                                          *
  * Sails apps read and write to local disk by default, using a built-in     *
  * database adapter called `sails-disk`.  This feature is purely for        *
  * convenience during development; since `sails-disk` is not designed for   *
  * use in a production environment.                                         *
  *                                                                          *
  * To use a different db _in development_, follow the directions below.     *
  * Otherwise, just leave the default datastore as-is, with no `adapter`.    *
  *                                                                          *
  * (For production configuration, see `config/env/production.js`.)          *
  *                                                                          *
  ***************************************************************************/

  mongodb: {

    /***************************************************************************
    *                                                                          *
    * Want to use a different database during development?                     *
    *                                                                          *
    * 1. Choose an adapter:                                                    *
    *    https://sailsjs.com/plugins/databases                                 *
    *                                                                          *
    * 2. Install it as a dependency of your Sails app.                         *
    *    (For example:  npm install sails-mysql --save)                        *
    *                                                                          *
    * 3. Then pass it in, along with a connection URL.                         *
    *    (See https://sailsjs.com/config/datastores for help.)                 *
    *                                                                          *
    ***************************************************************************/
    adapter: 'sails-mongo',
    url:'mongodb://ali:<password>@cluster0-kqqjs.mongodb.net:27017/test?w=majority',
    ssl: true,
  },

My model.js file

/**
 * Default model settings
 * (sails.config.models)
 *
 * Your default, project-wide model settings. Can also be overridden on a
 * per-model basis by setting a top-level properties in the model definition.
 *
 * For details about all available model settings, see:
 * https://sailsjs.com/config/models
 *
 * For more general background on Sails model settings, and how to configure
 * them on a project-wide or per-model basis, see:
 * https://sailsjs.com/docs/concepts/models-and-orm/model-settings
 */

module.exports.models = {

  /***************************************************************************
  *                                                                          *
  * Whether model methods like `.create()` and `.update()` should ignore     *
  * (and refuse to persist) unrecognized data-- i.e. properties other than   *
  * those explicitly defined by attributes in the model definition.          *
  *                                                                          *
  * To ease future maintenance of your code base, it is usually a good idea  *
  * to set this to `true`.                                                   *
  *                                                                          *
  * > Note that `schema: false` is not supported by every database.          *
  * > For example, if you are using a SQL database, then relevant models     *
  * > are always effectively `schema: true`.  And if no `schema` setting is  *
  * > provided whatsoever, the behavior is left up to the database adapter.  *
  * >                                                                        *
  * > For more info, see:                                                    *
  * > https://sailsjs.com/docs/concepts/orm/model-settings#?schema           *
  *                                                                          *
  ***************************************************************************/
  datastore: 'mongodb',
  // schema: true,

  /***************************************************************************
  *                                                                          *
  * How and whether Sails will attempt to automatically rebuild the          *
  * tables/collections/etc. in your schema.                                  *
  *                                                                          *
  * > Note that, when running in a production environment, this will be      *
  * > automatically set to `migrate: 'safe'`, no matter what you configure   *
  * > here.  This is a failsafe to prevent Sails from accidentally running   *
  * > auto-migrations on your production database.                           *
  * >                                                                        *
  * > For more info, see:                                                    *
  * > https://sailsjs.com/docs/concepts/orm/model-settings#?migrate          *
  *                                                                          *
  ***************************************************************************/

  migrate: 'alter',

  /***************************************************************************
  *                                                                          *
  * Base attributes that are included in all of your models by default.      *
  * By convention, this is your primary key attribute (`id`), as well as two *
  * other timestamp attributes for tracking when records were last created   *
  * or updated.                                                              *
  *                                                                          *
  * > For more info, see:                                                    *
  * > https://sailsjs.com/docs/concepts/orm/model-settings#?attributes       *
  *                                                                          *
  ***************************************************************************/

  attributes: {
    createdAt: { type: 'number', autoCreatedAt: true, },
    updatedAt: { type: 'number', autoUpdatedAt: true, },
    id: { type: 'number', autoIncrement: true, },
    //--------------------------------------------------------------------------
    //  /\   Using MongoDB?
    //  ||   Replace `id` above with this instead:
    //
    // ```
    // id: { type: 'string', columnName: '_id' },
    // ```
    //
    // Plus, don't forget to configure MongoDB as your default datastore:
    // https://sailsjs.com/docs/tutorials/using-mongo-db
    //--------------------------------------------------------------------------
  },

  /******************************************************************************
  *                                                                             *
  * The set of DEKs (data encryption keys) for at-rest encryption.              *
  * i.e. when encrypting/decrypting data for attributes with `encrypt: true`.   *
  *                                                                             *
  * > The `default` DEK is used for all new encryptions, but multiple DEKs      *
  * > can be configured to allow for key rotation.  In production, be sure to   *
  * > manage these keys like you would any other sensitive credential.          *
  *                                                                             *
  * > For more info, see:                                                       *
  * > https://sailsjs.com/docs/concepts/orm/model-settings#?dataEncryptionKeys  *
  *                                                                             *
  ******************************************************************************/

  dataEncryptionKeys: {
    default: 'KGQjanQiuFfn7VzbIpgIw9GvJn7FFYs+jf30HzvkzGk='
  },

  /***************************************************************************
  *                                                                          *
  * Whether or not implicit records for associations should be cleaned up    *
  * automatically using the built-in polyfill.  This is especially useful    *
  * during development with sails-disk.                                      *
  *                                                                          *
  * Depending on which databases you're using, you may want to disable this  *
  * polyfill in your production environment.                                 *
  *                                                                          *
  * (For production configuration, see `config/env/production.js`.)          *
  *                                                                          *
  ***************************************************************************/

  cascadeOnDestroy: true

};

My Tasks.js file on the model folder


module.exports = {

  attributes: {

    //  ╔═╗╦═╗╦╔╦╗╦╔╦╗╦╦  ╦╔═╗╔═╗
    //  ╠═╝╠╦╝║║║║║ ║ ║╚╗╔╝║╣ ╚═╗
    //  ╩  ╩╚═╩╩ ╩╩ ╩ ╩ ╚╝ ╚═╝╚═╝

    //  ╔═╗╔╦╗╔╗ ╔═╗╔╦╗╔═╗
    //  ║╣ ║║║╠╩╗║╣  ║║╚═╗
    //  ╚═╝╩ ╩╚═╝╚═╝═╩╝╚═╝

    //  ╔═╗╔═╗╔═╗╔═╗╔═╗╦╔═╗╔╦╗╦╔═╗╔╗╔╔═╗
    //  ╠═╣╚═╗╚═╗║ ║║  ║╠═╣ ║ ║║ ║║║║╚═╗
    //  ╩ ╩╚═╝╚═╝╚═╝╚═╝╩╩ ╩ ╩ ╩╚═╝╝╚╝╚═╝

  },
  datastore: 'mongodb',
};

Thank you

sailsbot commented 5 years ago

@AliHammami Thanks for posting! We'll take a look as soon as possible.

In the mean time, there are a few ways you can help speed things along:

Please remember: never post in a public forum if you believe you've found a genuine security vulnerability. Instead, disclose it responsibly.

For help with questions about Sails, click here.

johnabrams7 commented 5 years ago

@AliHammami Thanks for the detailed example code. Have you tried setting id: { type: 'string', columnName: '_id' }, under attributes?

AliHammami commented 5 years ago

@johnabrams7 i've just tried it, I have the same issues.

johnabrams7 commented 5 years ago

@AliHammami It looks like it's having trouble reaching cluster0-kqqjs.mongodb.net - I'm curious if the test?w=majority query string part of the URL is possibly confusing the adapter. Is that required and what happens without it?

AliHammami commented 5 years ago

I don't know if it's required. I deleted it and I have this issue now:

Failed to lift app: Error: Invalid configuration for datastore mongodb: No database could be determined from configuration (tried to use database: undefined).

But if i only delete ?w=majority, I have the same issue:

Failed to lift app: Error: Consistency violation: Unexpected error creating db connection manager:


MongoError: failed to connect to server [cluster0-kqqjs.mongodb.net:27017] on first connect [MongoError: getaddrinfo ENOTFOUND cluster0-kqqjs.mongodb.net cluster0-kqqjs.mongodb.net:27017]
AliHammami commented 5 years ago

This is the uri link from atlas mongodb, maybe it can help you to resolve my problem: mongodb+srv://ali:@cluster0-kqqjs.mongodb.net/test?retryWrites=true&w=majority

I saw in an another post that I have to delete the +srv and retryWhites=true

glemiere commented 5 years ago

@AliHammami see this: https://github.com/balderdashy/sails/issues/4590#issuecomment-494669327

AliHammami commented 5 years ago

@glemiere I've tried it:

adapter: 'sails-mongo',
host: 'localhost',
port: 27017,
database: 'todolist',
user:'ali',
password: 'password',

And I've get this error:

Failed to lift app: Error: Consistency violation: Unexpected error creating db connection manager:


MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]
johnabrams7 commented 5 years ago

@AliHammami I found this potential fix on StackOverflow for the same issue with sails-mongo and Atlas mongoDB:

AliHammami commented 5 years ago

Thank you guys for your help! I still have the issue :(.

glemiere commented 5 years ago

@AliHammami the error you link is showing that you tried to connect to localhost but your server is at cluster0-kqqjs.mongodb.net.

Try:

adapter: 'sails-mongo',
host: 'cluster0-kqqjs.mongodb.net',
port: 27017,
database: 'YOURDBNAMEHERE',
user:'YOURDBUSERHERE',
password: 'YOURPASSWORDHERE',

Just make sure you have the right information at the right place. Do you know your db name? Are you sure your user has read/write access to it?

AliHammami commented 5 years ago

@glemiere I still have the issue. I know the db name, I have read/write access to it. I tried to connect it with an express.js project and it works fine but I don't understand why it don't want to connect with sails.

johnabrams7 commented 5 years ago

@AliHammami - Good to know the credentials are confirmed working. Do you have an example of the working db configuration code from express.js to compare?

sheikh-mavin commented 4 years ago

DID U FIX THIS?

Lozcep commented 4 years ago

I have the same problem. I need help

YassineMd commented 4 years ago

Hey guys I have the same issue and I found the solution on StackOverflow.

Basically you need to update the connection driver version in mongo atlas dashboard from Node.js 3.0 or later driver version to Node.js 2.2.12 or later version.

eashaw commented 4 years ago

Thank you @YassineMd for finding that.

Hope this will help @AliHammami @Lozcep @w3villa-mavin

deepakvalagam commented 3 years ago

Hey guys I have the same issue and I found the solution on StackOverflow.

Basically you need to update the connection driver version in mongo atlas dashboard from Node.js 3.0 or later driver version to Node.js 2.2.12 or later version.

Thank you @YassineMd! This finally worked!

alambertt commented 3 years ago

Hey guys I have the same issue and I found the solution on StackOverflow. Basically you need to update the connection driver version in mongo atlas dashboard from Node.js 3.0 or later driver version to Node.js 2.2.12 or later version.

Thank you @YassineMd! This finally worked!

I worked for me to. You can close this issue