最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - MongoError: failed to connect to server [localhost:27017] - Stack Overflow

programmeradmin1浏览0评论

When I type node server.js it returns me this error

MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]
    at Pool.<anonymous> (/home/islam/workspace/project/node_modules/mongoose/node_modules/mongodb-core/lib/topologies/server.js:327:35)
    at emitOne (events.js:96:13)
    at Pool.emit (events.js:191:7)
    at Connection.<anonymous> (/home/islam/workspace/project/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:274:12)
    at Object.onceWrapper (events.js:293:19)
    at emitTwo (events.js:106:13)
    at Connection.emit (events.js:194:7)
    at Socket.<anonymous> (/home/islam/workspace/project/node_modules/mongoose/node_modules/mongodb-core/lib/connection/connection.js:177:49)
    at Object.onceWrapper (events.js:293:19)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:191:7)
    at emitErrorNT (net.js:1283:8)
    at _binedTickCallback (internal/process/next_tick.js:80:11)
    at process._tickCallback (internal/process/next_tick.js:104:9)

**

When I type in mand line mongod it returns me this

    2017-05-05T23:33:06.816+0600 I CONTROL  [initandlisten] MongoDB starting : pid=24805 port=27017 dbpath=/data/db 64-bit host=user
2017-05-05T23:33:06.816+0600 I CONTROL  [initandlisten] db version v3.4.4
2017-05-05T23:33:06.816+0600 I CONTROL  [initandlisten] git version: 888390515874a9debd1b6c5d36559ca86b44babd
2017-05-05T23:33:06.816+0600 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2g  1 Mar 2016
2017-05-05T23:33:06.816+0600 I CONTROL  [initandlisten] allocator: tcmalloc
2017-05-05T23:33:06.816+0600 I CONTROL  [initandlisten] modules: none
2017-05-05T23:33:06.816+0600 I CONTROL  [initandlisten] build environment:
2017-05-05T23:33:06.816+0600 I CONTROL  [initandlisten]     distmod: ubuntu1604
2017-05-05T23:33:06.816+0600 I CONTROL  [initandlisten]     distarch: x86_64
2017-05-05T23:33:06.816+0600 I CONTROL  [initandlisten]     target_arch: x86_64
2017-05-05T23:33:06.816+0600 I CONTROL  [initandlisten] options: {}
2017-05-05T23:33:06.816+0600 I STORAGE  [initandlisten] exception in initAndListen: 29 Data directory /data/db not found., terminating
2017-05-05T23:33:06.816+0600 I NETWORK  [initandlisten] shutdown: going to close listening sockets...
2017-05-05T23:33:06.816+0600 I NETWORK  [initandlisten] shutdown: going to flush diaglog...
2017-05-05T23:33:06.816+0600 I CONTROL  [initandlisten] now exiting
2017-05-05T23:33:06.816+0600 I CONTROL  [initandlisten] shutting down with code:100

**

However the mongo mand gives me this error

 MongoDB shell version v3.4.4
connecting to: mongodb://127.0.0.1:27017
2017-05-05T23:34:21.724+0600 W NETWORK  [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: Connection refused
2017-05-05T23:34:21.724+0600 E QUERY    [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
connect@src/mongo/shell/mongo.js:237:13
@(connect):1:6
exception: connect failed

I need to access to my database collections,

Any help will be appreciated!

When I type node server.js it returns me this error

MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]
    at Pool.<anonymous> (/home/islam/workspace/project/node_modules/mongoose/node_modules/mongodb-core/lib/topologies/server.js:327:35)
    at emitOne (events.js:96:13)
    at Pool.emit (events.js:191:7)
    at Connection.<anonymous> (/home/islam/workspace/project/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:274:12)
    at Object.onceWrapper (events.js:293:19)
    at emitTwo (events.js:106:13)
    at Connection.emit (events.js:194:7)
    at Socket.<anonymous> (/home/islam/workspace/project/node_modules/mongoose/node_modules/mongodb-core/lib/connection/connection.js:177:49)
    at Object.onceWrapper (events.js:293:19)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:191:7)
    at emitErrorNT (net.js:1283:8)
    at _binedTickCallback (internal/process/next_tick.js:80:11)
    at process._tickCallback (internal/process/next_tick.js:104:9)

**

When I type in mand line mongod it returns me this

    2017-05-05T23:33:06.816+0600 I CONTROL  [initandlisten] MongoDB starting : pid=24805 port=27017 dbpath=/data/db 64-bit host=user
2017-05-05T23:33:06.816+0600 I CONTROL  [initandlisten] db version v3.4.4
2017-05-05T23:33:06.816+0600 I CONTROL  [initandlisten] git version: 888390515874a9debd1b6c5d36559ca86b44babd
2017-05-05T23:33:06.816+0600 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2g  1 Mar 2016
2017-05-05T23:33:06.816+0600 I CONTROL  [initandlisten] allocator: tcmalloc
2017-05-05T23:33:06.816+0600 I CONTROL  [initandlisten] modules: none
2017-05-05T23:33:06.816+0600 I CONTROL  [initandlisten] build environment:
2017-05-05T23:33:06.816+0600 I CONTROL  [initandlisten]     distmod: ubuntu1604
2017-05-05T23:33:06.816+0600 I CONTROL  [initandlisten]     distarch: x86_64
2017-05-05T23:33:06.816+0600 I CONTROL  [initandlisten]     target_arch: x86_64
2017-05-05T23:33:06.816+0600 I CONTROL  [initandlisten] options: {}
2017-05-05T23:33:06.816+0600 I STORAGE  [initandlisten] exception in initAndListen: 29 Data directory /data/db not found., terminating
2017-05-05T23:33:06.816+0600 I NETWORK  [initandlisten] shutdown: going to close listening sockets...
2017-05-05T23:33:06.816+0600 I NETWORK  [initandlisten] shutdown: going to flush diaglog...
2017-05-05T23:33:06.816+0600 I CONTROL  [initandlisten] now exiting
2017-05-05T23:33:06.816+0600 I CONTROL  [initandlisten] shutting down with code:100

**

However the mongo mand gives me this error

 MongoDB shell version v3.4.4
connecting to: mongodb://127.0.0.1:27017
2017-05-05T23:34:21.724+0600 W NETWORK  [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: Connection refused
2017-05-05T23:34:21.724+0600 E QUERY    [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
connect@src/mongo/shell/mongo.js:237:13
@(connect):1:6
exception: connect failed

I need to access to my database collections,

Any help will be appreciated!

Share Improve this question asked May 5, 2017 at 17:37 Islam NurdinIslam Nurdin 151 silver badge5 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 1

Mongo by default writes data to /data folder and the user who is running mongo service does not have permission to create /data folder.

You can get this information from this log snippet

2017-05-05T23:33:06.816+0600 I STORAGE  [initandlisten] exception in initAndListen: 29 Data directory /data/db not found., terminating
2017-05-05T23:33:06.816+0600 I NETWORK  [initandlisten] shutdown: going to close listening sockets...

So, you need to do this

sudo mkdir /data/db
sudo chown $USER -R /data/db # give permission to the user who is running mongo service

Looks like you need to create the /data/db folder

try doing this in the terminal

sudo mkdir /data/db

then start mongodb

first run mongod.exe If it gives any warnings regarding unsafe shutdowns, metrics, diagnostics ignore them and run mongo.exe in another CLI(mand line Interface).

Even then if it does not work, just backup the ../data/db directories and redo the database.

Before access the db using a database driver(eg: mongoose,mongojs) make sure that the database is up and running.

$ mongo
MongoDB shell version v3.4.4
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.4
use YourAwesomeDatabaseName
switched to db YourAwesomeDatabaseName

You are good to go!

I had an issue /mongo.js:237:13 in local terminal trying to run GCP-hosted mongo. I fixed it by removing tags in GCP

hey there This is the issue because of your mongodb server is not running

you can verify this by

sudo service mongodb status

** you need to start the server by running mand

sudo service mongodb start

Good Luck

发布评论

评论列表(0)

  1. 暂无评论