【问题标题】:MEAN.js Could not connect to MongoDBMEAN.js 无法连接到 MongoDB
【发布时间】:2018-04-30 14:03:24
【问题描述】:

在使用 MEAN.js 创建我的应用程序时,无法以任何方式在端口 27017 上进行 mongodb 连接。 我在目录中手动安装了 mongodb

~$ ls -la /etc/init.d/mongod 

输出

lrwxrwxrwx 1 root root 56 Nov 4 20:45 /etc/init.d/mongod -> /home/myuser/projects/packages/linux-mongodb/bin/mongod 
I try to run the file or `` server.js` grunt` the NASC repository command and got the following error:

运行node server.js 我收到这个输出:

Application loaded using the "development" environment configuration 
Failed to load c ++ bson extension, using pure JS version 
Insecurely using http protocol 
MEAN.JS application started on port 27017 
Could not connect to MongoDB! 
Error: connection closed 

存储库网址: https://github.com/meanjs/mean

暗示什么可能是什么? 我期待个人联系。 提前致谢!

参考链接:

https://stackoverflow.com/a/22557417/3332734

运行cat /etc/mongod.conf

# mongod.conf

# Where to store the data.

# Note: if you run mongodb as a non-root user (recommended) you may
# need to create and set permissions for this directory manually,
# e.g., if the parent directory isn't mutable by the mongodb user.
dbpath=/var/lib/mongodb
###dbpath=/home/myuser/projects/packages/mongodb-linux/databases

#where to log
logpath=/var/log/mongodb/mongod.log
###logpath=/home/myuser/projects/packages/mongodb-linux/mongod.log

logappend=true

port = 27017

# Listen to local interface only. Comment out to listen on all interfaces. 
bind_ip = 127.0.0.1

# Disables write-ahead journaling
# nojournal = true

# Enables periodic logging of CPU utilization and I/O wait
#cpu = true

# Turn on/off security.  Off is currently the default
#noauth = true
#auth = true

# Verbose logging output.
#verbose = true

# Inspect all client data for validity on receipt (useful for
# developing drivers)
#objcheck = true

# Enable db quota management
#quota = true

# Set oplogging level where n is
#   0=off (default)
#   1=W
#   2=R
#   3=both
#   7=W+some reads
#diaglog = 0

# Ignore query hints
#nohints = true

# Enable the HTTP interface (Defaults to port 28017).
#httpinterface = true
httpinterface = true

# Turns off server-side scripting.  This will result in greatly limited
# functionality
#noscripting = true

# Turns off table scans.  Any query that would do a table scan fails.
#notablescan = true

# Disable data file preallocation.
#noprealloc = true

# Specify .ns file size for new databases.
# nssize = <size>

# Replication Options

# in replicated mongo databases, specify the replica set name here
#replSet=setname
# maximum size in megabytes for replication operation log
#oplogSize=1024
# path to a key file storing authentication info for connections
# between replica set members
#keyFile=/path/to/keyfile

在控制台中运行mongo 并检索输出:

$ mongo
MongoDB shell version: 2.6.5
connecting to: test
2014-11-05T19:23:18.957-0200 warning: Failed to connect to  127.0.0.1:27017, reason: errno:111 Connection refused
2014-11-05T19:23:18.958-0200 Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed at src/mongo/shell/mongo.js:146

异常:连接失败


在控制台中运行mongod 并检索输出:

$ mongod

mongod --help for help and startup options
2014-11-05T19:26:53.215-0200 [initandlisten] MongoDB starting : pid=3487 port=27017 dbpath=/data/db 64-bit host=francis-tosystems
2014-11-05T19:26:53.215-0200 [initandlisten] db version v2.6.5
2014-11-05T19:26:53.215-0200 [initandlisten] git version: e99d4fcb4279c0279796f237aa92fe3b64560bf6
2014-11-05T19:26:53.215-0200 [initandlisten] build info: Linux build8.nj1.10gen.cc 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
2014-11-05T19:26:53.215-0200 [initandlisten] allocator: tcmalloc
2014-11-05T19:26:53.215-0200 [initandlisten] options: {}
2014-11-05T19:26:53.215-0200 [initandlisten] exception in initAndListen: 10296 
*********************************************************************
 ERROR: dbpath (/data/db) does not exist.
 Create this directory or give existing directory in --dbpath.
 See http://dochub.mongodb.org/core/startingandstoppingmongo
*********************************************************************
, terminating
2014-11-05T19:26:53.215-0200 [initandlisten] dbexit: 
2014-11-05T19:26:53.215-0200 [initandlisten] shutdown: going to close listening sockets...
2014-11-05T19:26:53.215-0200 [initandlisten] shutdown: going to flush diaglog...
2014-11-05T19:26:53.215-0200 [initandlisten] shutdown: going to close sockets...
2014-11-05T19:26:53.215-0200 [initandlisten] shutdown: waiting for fs preallocator...
2014-11-05T19:26:53.215-0200 [initandlisten] shutdown: lock for final commit...
2014-11-05T19:26:53.215-0200 [initandlisten] shutdown: final commit...
2014-11-05T19:26:53.215-0200 [initandlisten] shutdown: closing all files...
2014-11-05T19:26:53.216-0200 [initandlisten] closeAllFiles() finished
2014-11-05T19:26:53.216-0200 [initandlisten] dbexit: really exiting now

或者运行mongod

$ mongod --config /etc/mongod.conf &
[1] 3568 2014-11-05T19:28:16.867-0200 SEVERE: Failed global initialization: FileNotOpen Failed to open "/var/log/mongodb/mongod.log"

编辑


我不再使用 MEAN.js,但我继续学习 MongoDB 和 node.js

问题是我不知道如何启动mongodb连接数据库

这一年我学到了很多关于 mongodb 和 node.js 的知识,我明白了我需要做什么。

在您的帮助下,我完成了三个步骤:

  1. 使用mongod --dbpath /project/path-do-database/ 打开控制台
  2. 我将 API 连接到 mongodb,并通过了正确的端口
  3. 通过使用nodenodemon 启动node.js API 并传递项目的主javascript 文件来打开第二个控制台。

【问题讨论】:

  • 你能粘贴你的完整错误吗?
  • mongod 还在运行吗?尝试使用 CLI 客户端 mongo 连接并检查 netstat 绑定到哪个 IP 和端口。
  • 谢谢@mnemosyn!你的帮助对我很有帮助。

标签: node.js mongodb


【解决方案1】:

基本上,您需要在同一个文件夹中运行 2 个终端。 一个用于 mongoDB,一个用于您的应用程序。

在第一个终端上,

1>> npm install -g meanjs
1>> yo meanjs

然后填写你的应用名称、作者姓名等。

1>> npm install

打开第二个终端来启动 mongodb 服务器

2>> mongod 

回到第一个终端并运行它

1>> npm start

【讨论】:

  • 感谢您的帮助,@Chee Loong Soon! :)
【解决方案2】:

您正在与您的 mongodb 服务器 (27017) 相同的端口上启动您的 meanjs 应用程序。如果没有为meanjs指定端口,在开发环境中会从3000开始。

【讨论】:

  • 无论如何。唯一的解决方案是通过 apt-get 安装 mongodb,这是单独启动的唯一方法。但是我只想安装纯javascript,只有这个不是单独启动的。不喜欢使用 apt-get 安装任何 NoSQL,但这是我找到的唯一解决方案。
  • 是的,您需要使用系统包管理器安装 mongodb。您还需要(手动)启动它。您只能使用 npm 安装 mongodb 驱动程序(这已经在 meanjs 项目中完成)。
  • 不太明白他在说什么。所以对于 Mean.js,我必须通过 apt-get 和 npm 中的驱动器安装 mongodb?这个?
  • 但是因为不仅可以通过 npm 工作吗?我将如何在通过 npm 安装的 Linux mongo 上工作?请帮帮我。
  • mongodb 是系统守护进程,而 npm 是节点内容的包管理器。 mongodb 不是节点模块或类似的东西。为了解决您最初的问题,您只需要在另一个端口上运行您的 meanjs 应用程序(您已尝试在与您的 mongodb 应该运行的端口相同的端口上运行它)。
【解决方案3】:

我知道可能已经晚了,但错误日志说明了一切:

 ERROR: dbpath (/data/db) does not exist.

我相信您需要创建 /data/db 文件夹,或者它是在其他地方创建的。 这里已经回答了: mongodb Mongod complains that there is no /data/db folder

【讨论】:

  • 感谢@olzhas-shaikenov,但我需要在我的 ubuntu 服务器中创建 mongodb 的 person 文件夹,并且 mongo.conf 运行不正确。
  • @FrancisRodrigues 从您提供的所有信息中,尚不清楚 mongodb 配置的问题还是 Ubuntu,有时它有点时髦。所以我会这样做。 1. 转到 mongolab 并创建小型沙箱数据库 2. 连接到您新创建的数据库,如果可行,则意味着您需要在本地调查 mongod ~$ systemctl status mongod 3. 如果您无法连接到 Mongolab,那么您需要查看 nodejs 配置。通过这种方式,您可以分离问题并找出真正导致问题的原因。希望这会有所帮助。
  • 谢谢@oshaiken!!现在我明白我需要做什么了。
猜你喜欢
  • 2017-02-02
  • 2017-06-17
  • 2012-02-01
  • 2016-07-31
  • 2014-11-04
  • 2013-07-27
  • 2017-06-11
相关资源
最近更新 更多