1

我正在使用 nodeJs + MongoDB 开发一个小项目(REST API)。我已经能够在本地安装 MongoDB 并使用 mongoose 连接到它。但是由于某种原因,当我尝试使用 MongoDB Atlas 进行连接时,它失败了。看起来它已连接,但 2 秒后,我收到一条错误消息,提示套接字已关闭(请参阅下面的错误)。我不知道发生了什么。我已将我的 IP 列入白名单,检查了我的登录信息以确保我使用了正确的密码,并且确实我正在使用,因为我能够使用 MongoDB 指南针进行连接。任何帮助是极大的赞赏。

 My current local ENV package versions are:
    nodeJs:V9.7.1
    mongoose:V6.1

=== MongoDb Atlas ===
    mongodb:3.6

下面是我用来连接数据库的代码:

var express = require('express'),
port = process.env.PORT || 3000,
mongoose = require('mongoose'),
user = require('./api/models/userModel'), 
config = require('./api/config');
bodyParser = require('body-parser');

var authRoutes = require('./api/routes/authRoutes'),
userRoutes = require('./api/routes/userRoutes'),
reviewRoutes = require('./api/routes/reviewRoutes');

const swaggerUi = require('swagger-ui-express');
const swaggerDocument = require('./swagger.json');

mongoose.Promise = global.Promise;
mongoose.connect('mongodb://user:myPass@cluster0-shard-00-00 zd6jq.mongodb.net/myDb');

app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());

//Swagger Info
var options = {
    explorer : true
};

app.use('/swagger', swaggerUi.serve, swaggerUi.setup(swaggerDocument, options));
//END Swagger Info

//REGISTER ROUTES
userRoutes(app);
authRoutes(app);
reviewRoutes(app);

app.listen(port);

console.log('iReview RESTful API server listenning on port: ' + port);


module.exports = app;

====错误信息===

    /Users/mdiez/node_test/node_modules/mongodb/lib/server.js:228
        process.nextTick(function() { throw err; })
                                      ^
MongoError: server cluster0-shard-00-00-zd6jq.mongodb.net:27017 sockets closed
    at Pool.<anonymous> (/Users/mdiez/node_test/node_modules/mongodb-core/lib/topologies/server.js:325:47)
    at Object.onceWrapper (events.js:219:13)
    at Pool.emit (events.js:127:13)
    at Connection.<anonymous> (/Users/mdiez/node_test/node_modules/mongodb-core/lib/connection/pool.js:101:12)
    at Object.onceWrapper (events.js:219:13)
    at Connection.emit (events.js:127:13)
    at Socket.<anonymous> (/Users/mdiez/node_test/node_modules/mongodb-core/lib/connection/connection.js:142:12)
    at Object.onceWrapper (events.js:219:13)
    at Socket.emit (events.js:127:13)
    at TCP._handle.close [as _onclose] (net.js:558:12)
4

2 回答 2

3

var uri = 'mongodb://<usernamr>:<password>@<clustername>/<dbname>?ssl=true&replicaSet=<replica setname>&authSource=admin';

var db = mongoose.connect(uri).catch((error) => { console.log(error); });

指定副本集名称、ssl true 和身份验证数据库。这是基于 Atlas 文档的参考。

于 2018-06-04T02:44:13.260 回答
1

您可以使用此代码连接到 Compass 和应用程序。您只需要执行以下操作:

  1. 从 Cluster -> Overview 中替换所有三个主要和次要分片
  2. 将 natours-app 替换为您的集群名称。

指南针: mongodb://babar_bahadur:PASSWORD@natours-app-shard-00-00-ybksz.mongodb.net:27017,natours-app-shard-00-01-ybksz.mongodb.net:27017,natours-app- shard-00-02-ybksz.mongodb.net:27017/test?authSource=admin&replicaSet=natours-app1-shard-0&readPreference=primary&appname=MongoDB%20Compass&retryWrites=true&ssl=true

应用: mongodb://babar_bahadur:PASSWORD@natours-app-shard-00-00-ybksz.mongodb.net:27017,natours-app-shard-00-01-ybksz.mongodb.net:27017,natours-app- shard-00-02-ybksz.mongodb.net:27017/test?ssl=true&replicaSet=natours-app-shard-0&authSource=admin&retryWrites=true

于 2019-12-22T10:27:36.423 回答