Mongoose 无法连接到 localhost:MongoError: failed to connect to server [127.0.0.1:27017]

Posted

技术标签:

【中文标题】Mongoose 无法连接到 localhost:MongoError: failed to connect to server [127.0.0.1:27017]【英文标题】:Mongoose fails to connect to localhost: MongoError: failed to connect to server [127.0.0.1:27017] 【发布时间】:2017-04-17 00:57:09 【问题描述】:

我正在处理从 Github https://github.com/MessagingBots/MessengerBot 下载的 repo,我知道 js 代码很好。

我有一个配置文件将 db URL 指向“mongodb://127.0.0.1:27017/messenger”。 (我还没有创建数据库)

我在 Heroku 上有节点应用程序,但 Heroku Logs 每次在 mongodb 连接时都失败。

2016-12-02T06:23:31.636339+00:00 app[web.1]: mongodb://127.0.0.1:27017/messenger
2016-12-02T06:23:31.672354+00:00 app[web.1]: CALLBACK URL IS
2016-12-02T06:23:31.672434+00:00 app[web.1]: https://cool-scrubland-89690.herokuapp.com/messages/receive/
2016-12-02T06:23:31.683394+00:00 app[web.1]: students
2016-12-02T06:23:31.701863+00:00 app[web.1]: listening on port 3124
2016-12-02T06:23:31.702052+00:00 app[web.1]: Application running at port 48594
2016-12-02T06:23:31.717752+00:00 app[web.1]: 
2016-12-02T06:23:31.717764+00:00 app[web.1]: /app/node_modules/mongoose/node_modules/mongodb/lib/server.js:261
2016-12-02T06:23:31.717766+00:00 app[web.1]:         process.nextTick(function()  throw err; )
2016-12-02T06:23:31.717767+00:00 app[web.1]:                                       ^
2016-12-02T06:23:31.718744+00:00 app[web.1]: MongoError: failed to connect to server [127.0.0.1:27017] on first connect
2016-12-02T06:23:31.718746+00:00 app[web.1]:     at Pool.<anonymous> (/app/node_modules/mongoose/node_modules/mongodb-core/lib/topologies/server.js:313:35)
2016-12-02T06:23:31.718747+00:00 app[web.1]:     at emitOne (events.js:96:13)
2016-12-02T06:23:31.718747+00:00 app[web.1]:     at Pool.emit (events.js:188:7)
2016-12-02T06:23:31.718748+00:00 app[web.1]:     at Connection.<anonymous> (/app/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:271:12)
2016-12-02T06:23:31.718749+00:00 app[web.1]:     at Connection.g (events.js:291:16)
2016-12-02T06:23:31.718750+00:00 app[web.1]:     at emitTwo (events.js:106:13)
2016-12-02T06:23:31.718750+00:00 app[web.1]:     at Connection.emit (events.js:191:7)
2016-12-02T06:23:31.718751+00:00 app[web.1]:     at Socket.<anonymous> (/app/node_modules/mongoose/node_modules/mongodb-core/lib/connection/connection.js:165:49)
2016-12-02T06:23:31.718752+00:00 app[web.1]:     at Socket.g (events.js:291:16)
2016-12-02T06:23:31.718752+00:00 app[web.1]:     at emitOne (events.js:96:13)
2016-12-02T06:23:31.718753+00:00 app[web.1]:     at Socket.emit (events.js:188:7)
2016-12-02T06:23:31.718754+00:00 app[web.1]:     at emitErrorNT (net.js:1276:8)
2016-12-02T06:23:31.718754+00:00 app[web.1]:     at _combinedTickCallback (internal/process/next_tick.js:74:11)
2016-12-02T06:23:31.718755+00:00 app[web.1]:     at process._tickCallback (internal/process/next_tick.js:98:9)
2016-12-02T06:24:24.832476+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2016-12-02T06:24:24.832543+00:00 heroku[web.1]: Stopping process with SIGKILL
2016-12-02T06:24:24.958931+00:00 heroku[web.1]: State changed from starting to crashed
2016-12-02T06:24:24.949820+00:00 heroku[web.1]: Process exited with status 137

我有一个 mongod 实例正在运行。 mongod 正确连接到 localhost 并且很稳定

2016-12-02T14:02:26.733+0700 I CONTROL  [initandlisten] MongoDB starting : pid=2706 port=27017 dbpath=/data/db 64-bit host=ME.local
2016-12-02T14:02:26.735+0700 I CONTROL  [initandlisten] db version v3.2.11
2016-12-02T14:02:26.735+0700 I CONTROL  [initandlisten] git version: 009580ad490190ba33d1c6253ebd8d91808923e4
2016-12-02T14:02:26.735+0700 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2j  26 Sep 2016
2016-12-02T14:02:26.735+0700 I CONTROL  [initandlisten] allocator: system
2016-12-02T14:02:26.735+0700 I CONTROL  [initandlisten] modules: none
2016-12-02T14:02:26.735+0700 I CONTROL  [initandlisten] build environment:
2016-12-02T14:02:26.735+0700 I CONTROL  [initandlisten]     distarch: x86_64
2016-12-02T14:02:26.735+0700 I CONTROL  [initandlisten]     target_arch: x86_64
2016-12-02T14:02:26.735+0700 I CONTROL  [initandlisten] options: 
2016-12-02T14:02:26.738+0700 I -        [initandlisten] Detected data files in /data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
2016-12-02T14:02:26.739+0700 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=4G,session_max=20000,eviction=(threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
2016-12-02T14:02:27.758+0700 I CONTROL  [initandlisten] 
2016-12-02T14:02:27.759+0700 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
2016-12-02T14:02:27.763+0700 I NETWORK  [HostnameCanonicalizationWorker] Starting hostname canonicalization worker
2016-12-02T14:02:27.763+0700 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
2016-12-02T14:02:27.767+0700 I NETWORK  [initandlisten] waiting for connections on port 27017

问题可能与 Mongoose 有关。

谁能帮帮我?

【问题讨论】:

【参考方案1】:

你没有在你的 mongoose connect 调用中精确的 mongodb:// :

mongoose.connect("localhost:27017/mydbname");

【讨论】:

我试过没有mongodb://,但没有结果。谢谢你!还在苦恼中 显示您的代码,并打印以下命令的输出:sudo netstat -atunlp | grep“27017” 终端说命令sudo netstat -atunlp | grep "27017" 不存在(我也尝试过tunlpa tunlp,但没有任何结果。我的代码在repo 中github.com/MessagingBots/MessengerBot 我有一个配置文件,指向 db URL ` "dbURL": "localhost:27017/messenger" ` 尝试 netstat,如果找不到命令安装 netstat,(对于 debian 版本 sudo apt-get install netstat) netstat 提供“活跃的互联网连接”和数百个地址。没有引用端口 27017【参考方案2】:

查看是否存在旧的锁定文件(如果存在),然后将其删除 sudo rm /var/lib/mongodb/mongod.lock 看看这是否适合你 sudo mongod --repair and (sudo start mongodb or sudo service mongodb start)

【讨论】:

以上是关于Mongoose 无法连接到 localhost:MongoError: failed to connect to server [127.0.0.1:27017]的主要内容,如果未能解决你的问题,请参考以下文章

如何连接到最新的 Mongoose 版本

Mongoose 无法连接到 mLab 环境

Mongoose 无法连接到 mLab 环境

无法将 Mongoose 连接到 Atlas

无法使用 mongoose 连接到 mongodb 数据库

无法使用 mongoose 连接到 Mongodb Atlas