所以我目前正在创建一个Web应用程序,我需要数据库,所以我决定使用mongodb和mongoose . 到目前为止,我测试了localhost上的所有内容并且它有效,但我想将数据移动到服务器上 . 我听说过Atlas并注册了自己并“上传”了数据 .
现在我想通过node.js应用程序连接到集群 .
mongoose.connect('mongodb+srv://engllucas:p%40ssw0rd@insight-quhku.mongodb.net/test');
我得到了mongodb Atlas网站的字符串 Connect Your Application
然后我换了密码 .
mongoose.connect('mongodb://engllucas:p%40ssw0rd@insight-quhku.mongodb.net/test');
这也不起作用 .
多数民众赞成的错误信息:
{ MongoError: failed to connect to server [insight-shard-00-02-quhku.mongodb.net:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 18.194.163.64:27017]
at Pool.<anonymous> (U:\WEBT\ProjectInsight\Quiz\node_modules\mongodb-core\lib\topologies\server.js:503:11)
at emitOne (events.js:115:13)
at Pool.emit (events.js:210:7)
at Connection.<anonymous> (U:\WEBT\ProjectInsight\Quiz\node_modules\mongodb-core\lib\connection\pool.js:326:12)
at Object.onceWrapper (events.js:318:30)
at emitTwo (events.js:125:13)
at Connection.emit (events.js:213:7)
at TLSSocket.<anonymous> (U:\WEBT\ProjectInsight\Quiz\node_modules\mongodb-core\lib\connection\connection.js:245:50)
at Object.onceWrapper (events.js:316:30)
at emitOne (events.js:115:13)
at TLSSocket.emit (events.js:210:7)
at emitErrorNT (internal/streams/destroy.js:64:8)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
name: 'MongoNetworkError',
消息:'首次连接时无法连接到服务器[insight-shard-00-02-quhku.mongodb.net:27017] [MongoNetworkError:connect ECONNREFUSED 18.194.163.64:27017]'}(node:4920)UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝ID:1):MongoNetworkError:首次连接时无法连接到服务器[insight-shard-00-02-quhku.mongodb.net:27017] [MongoNetworkError:connect ECONNREFUSED 18.194.163.64:27017](node:4920) )[DEP0018]弃用警告:不推荐使用未处理的拒绝承诺 . 将来,未处理的承诺拒绝将使用非零退出代码终止Node.js进程 .
1 回答
如果您使用5.0.15之前的mongoose:
the
mongodb+srv://
uri is not valid to the underlying native driver .只是删除srv也不会工作,因为uri需要包含副本主机和replicaSet / authSource参数 .
你也可以
1. Upgrade to mongoose 5.0.15 and use the shorter +srv format
或
2. Use your current version with the full uri from your Atlas cluster dashboard, selecting the older driver (见下文 Note ) version uri.
从群集仪表板中,按连接按钮,然后(假设您已将节点服务器的IP列入白名单)选择"connect your application",然后按"I am using 3.4 driver or older" . 使用生成的uri字符串替换您的密码 .
Note: 在用于在Atlas集群仪表板上的连接助手中选择URI字符串的对话框中,它显示为"I am using driver 3.* or (newer|older)" . 这是一个糟糕的选择,因为3.6和3.4不是指驱动程序版本,而是服务器版本 . 此外,使用服务器的一个版本或另一个版本与用于连接的驱动程序无关,这是确定使用哪个版本的URI的实际依赖性所在 .