当我输入node server.js
时,它会返回此错误
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 _combinedTickCallback (internal/process/next_tick.js:80:11)
at process._tickCallback (internal/process/next_tick.js:104:9)
**
当我输入命令行mongod
时,它会返回此
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
**
但是mongo
命令给了我这个错误
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
我需要访问我的数据库集合,
任何帮助将不胜感激!
答案 0 :(得分:1)
看起来您需要创建/ data / db文件夹
尝试在终端中执行此操作
sudo mkdir /data/db
然后启动mongodb
答案 1 :(得分:1)
Mongo默认将数据写入/ data文件夹,而运行mongo服务的用户没有创建/ data文件夹的权限。
您可以从此日志片段中获取此信息
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...
所以,你需要这样做
sudo mkdir /data/db
sudo chown $USER -R /data/db # give permission to the user who is running mongo service
答案 2 :(得分:0)
首次运行 mongod.exe 如果它提供有关不安全关机的任何警告,指标,诊断会忽略它们并在另一个中运行 mongo.exe CLI(命令行界面)。
即便如此,如果它不起作用,只需备份 ../ data / db 目录并重做数据库。
在使用数据库驱动程序(例如:mongoose,mongojs)访问数据库之前,请确保数据库已启动并正在运行。
$ mongo
MongoDB shell版本v3.4.4
连接到:mongodb://127.0.0.1:27017
MongoDB服务器版本:3.4.4
使用YourAwesomeDatabaseName
切换到db YourAwesomeDatabaseName
你很高兴!
答案 3 :(得分:0)
我在本地终端尝试运行GCP托管的mongo时遇到了问题/mongo.js:237:13。我通过删除GCP中的标签来修复它
答案 4 :(得分:0)
嘿那是因为你的mongodb服务器没有运行这个问题
您可以通过
进行验证sudo service mongodb status
** 您需要通过运行命令
来启动服务器sudo service mongodb start
祝你好运