从数字海洋Ubuntu Droplet连接到Mongo Atlas

时间:2018-05-10 20:19:23

标签: node.js mongodb express mongoose digital-ocean

我有一个节点应用程序连接到Atlas上的MongoDB分片。当我在本地开发时,一切正常,但当我部署到Digital Ocean droplet时,我收到连接错误。

我已通过ufw打开了所需的端口:

sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
80                         ALLOW       Anywhere
443                        ALLOW       Anywhere
27017                      ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)
80 (v6)                    ALLOW       Anywhere (v6)
443 (v6)                   ALLOW       Anywhere (v6)
27017 (v6)                 ALLOW       Anywhere (v6)

启动连接的代码是:

mongoose.connect(process.env.DATABASE).then(
  ()=>{
    console.log("connected to mongoDB")},
 (err)=>{
     console.log("err",err);
})

但我收到错误消息:

    err { MongoNetworkError: connection 3 to myapp-shard-00-00- 
      jooao.mongodb.net:27017 closed
      at TLSSocket.<anonymous
 (/home/stuart/apps/myapp.co.uk/node_modules/mongoose/node_modules/mongodb-core/lib/connection/connection.js:275:9)
    at Object.onceWrapper (events.js:272:13)
    at TLSSocket.emit (events.js:185:15)
    at TLSSocket.emit (domain.js:422:20)
    at _handle.close (net.js:541:12)
    at TCP.done [as _onclose] (_tls_wrap.js:379:7)
  name: 'MongoNetworkError',

  message: 'connection 3 to myapp-shard-00-00-jooao.mongodb.net:27017 closed' }

有人可以告诉我做错了吗?

0 个答案:

没有答案