简体   繁体   中英

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?

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:password@test-shard-00-00-some-address.mongodb.net: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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM