dhiraka
dhiraka

Reputation: 1195

Connecting to Mongo Atlas from local Apostrophe cms code

I have enabled connection from anywhere 0.0.0.0/0 on Mongo Atlas. In apostrophe code, in the file data/local.js I added the following code :

module.exports = {
  modules: {
    'apostrophe-db': {
      uri: 'mongodb+srv://clustername.mongodb.net/dbname?retryWrites=true'
    }
  }
};
Tried mongodb+srv://admin:@clustername.mongodb.net/dbname?retryWrites=true as well.

I see the following log :

/home/radhika/code/demo/demo-io/node_modules/apostrophe/index.js:67
    throw err;
    ^
MongoError: not authorized on admin to execute command { listIndexes: "aposCache", cursor: {  } }
        at Function.MongoError.create (/home/radhika/code/demo/demo-io/node_modules/mongodb-core/lib/error.js:31:11)
        at queryCallback (/home/radhika/code/demo/demo-io/node_modules/mongodb-core/lib/cursor.js:212:36)
        at /home/radhika/code/demo/demo-io/node_modules/mongodb-core/lib/connection/pool.js:469:18
        at _combinedTickCallback (internal/process/next_tick.js:131:7)
        at process._tickCallback (internal/process/next_tick.js:180:9)

What am I missing here?

Upvotes: 1

Views: 688

Answers (2)

martinedwards
martinedwards

Reputation: 5835

If you click the Connect button from your cluster and choose Connect your application, and then choose driver version 2. This will give you an example of how to connect.

Upvotes: 0

Tom Boutell
Tom Boutell

Reputation: 7572

I was able to reproduce this with Atlas. You are attempting to use a mongodb+srv URL, which is what Atlas recommends when using the MongoDB driver 3.6 or better. Apostrophe 2.x is currently on the MongoDB 2.x driver, so that doesn't work.

You need to use the older style of URL which you can see on Atlas if you click the "I am using driver 3.4 or earlier" tab instead:

mongodb://user:[email protected]:27017,test-shard-00-01-some-address.mongodb.net:27017/test?ssl=true&replicaSet=your-replica-set-name&authSource=admin&retryWrites=true

When I do it this way I have no trouble connecting.

If you are eager to use a newer version of the MongoDB driver, and the new style of URI, you can use the apostrophe-db-mongo-3-driver module. But, this isn't currently mandatory to be successful with Atlas.

In Apostrophe 3.x we will of course use the newer generation of driver out of the box.

(One last FYI - MongoDB driver versions and MongoDB server versions are not the same thing. You don't need the 3.6 driver to connect to 3.6, for instance.)

Upvotes: 1

Related Questions