我最近使用Homebrew安装了mongodb-2.6.0。
成功安装后,我尝试使用mongo
命令进行连接。我收到以下错误,不允许我连接:
Failed to connect to 127.0.0.1:27017, reason: errno:61 Connection refused
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
exception: connect failed
答案 0 :(得分:266)
当mongodb服务没有在mac上运行时,可能会发生这种情况。为了开始,我尝试了
brew services start mongodb
并且有效。
编辑:根据有关本地PR的讨论,自制:https://github.com/Homebrew/homebrew/issues/30628
brew services
已被弃用,我查看了SO并发现这些答案现在回答了问题:What is the correct way to start a mongod service on linux / OS X?
答案 1 :(得分:32)
我遇到了完全相同的问题,这里有一个明确的一步一步的过程来避免这个错误。
第1步 - 安装(如果您已经安装了MongoDB,请不要执行此步骤):
brew update
brew install mongodb
第2步 - 运行Mongo守护程序:
mkdir -p /data/db
sudo mongod
第3步 - 运行Mongo Shell界面:
mongo
在这个序列中,我能够运行mongo
命令而没有任何错误。我还详细介绍了错误跟踪及其解决方案on this blog。
答案 2 :(得分:22)
要解决您的问题,您需要按照使用" brew install mongodb"后通过brew给您的说明进行操作。
要在登录时启动启动mongodb:
ln -sfv /usr/local/opt/mongodb/*.plist ~/Library/LaunchAgents
然后立即加载mongodb:
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist
或者,如果您不想/需要launchctl,您可以运行:
mongod --config /usr/local/etc/mongod.conf
你可能只能运行最后一个命令,但它对我没有用,我需要运行第二个命令。为了拯救我将来。我也跑了第一个命令。希望有所帮助!
编辑 Hrishi使用brew services mongodb start
的方法对我有用。我认为他们应该在mongo docs中包含这个。
答案 3 :(得分:16)
在另一个标签中,您可以使用
启动mongo shell的mongod
然后返回上一个标签,然后重试。如果您在设置mongoshell时遇到问题,请查看mongo shell上的此链接:http://docs.mongodb.org/manual/tutorial/manage-mongodb-processes/ 或者这个关于安装mongodb的链接: http://docs.mongodb.org/manual/tutorial/install-mongodb-on-os-x/
答案 4 :(得分:7)
从命令行调用mongod
时遇到了同样的问题。
我通过调用sudo mongod
来解决这个问题。
答案 5 :(得分:4)
我有同样的错误,但根本原因不同。以为我会在这里发布解决方案以防其他人遇到问题。在我运行mongorestore -d foo dump/foo/
时,我的Mac不正确关闭后出现此错误。
tl; dr :我通过从我的数据文件夹中移除损坏的foo.ns
文件以及foo.0
,foo.1
,...来解决了问题{1}}。然后我用/usr/local/var/mongodb/
重新启动了mongo服务器,我恢复了正常。
详细信息:即使在尝试通过brew或launchctl启动或重新启动mongodb服务后,我仍然遇到错误。最后我跑了
brew services restart mongodb
并看到该服务实际上没有启动,并且启动序列包括以下错误:
mongod --dbpath /usr/local/var/mongodb
我摆脱了错误的
[initandlisten] bad .ns file: /usr/local/var/mongodb/foo.ns
[initandlisten] User Assertion: 10079:bad .ns file length, cannot open database
文件和其他数据文件,下次我启动服务时,我很高兴。
答案 6 :(得分:3)
对我说osx,我不得不杀死旧的运行实例,然后重新启动工作。
$>ps -aef | grep mongo
502 8047 1 0 11:52AM ?? 0:00.23 /usr/local/opt/mongodb/bin/mongod --config /usr/local/etc/mongod.conf
$>sudo kill 8047
$>sudo mongod
答案 7 :(得分:2)
对于在Mac更新到OS Catalina之后在这里寻找答案的任何人
运行
brew services start mongodb-community
答案 8 :(得分:1)
我用自制软件升级到mongo 3.6后出现此错误。
日志/usr/local/var/log/mongodb/mongo.log
包含消息shutting down with code:62
。此错误代码表示现有数据库太旧而无法使用当前版本的mongo运行。
我在另一个SO question找到了2个解决方案:
/usr/local/var/mongodb
)我选择升级。在我的情况下,这意味着我必须降级到3.4,在mongo控制台中运行命令,然后再次升级。 Mongo要求您一次升级一个主要版本,因此根据您的回溯程度,可能还有其他步骤。文档将指导您。
brew switch
*和brew services restart
命令在相对无痛的版本之间进行交换。
答案 9 :(得分:0)
如果您已经安装了MongoDB,那么首先尝试以sudo用户身份运行mongod,因为没有以超级用户身份运行mongod,我遇到了这个问题。
我已经在最底部粘贴了两个命令( mongod 和 sudo mongod )的o / p,你也可以检查,但是
首先尝试这个
sudo mongod
不是
mongod
通过依次运行以下命令,我在我的MAC OS X Sierra 10.12.6上安装了MongoDB。
brew update
brew install mongodb --devel
brew services start mongodb
然后创建了一个mongod进程将写入数据的目录,这是可选的,因为mongod进程默认采用它,请参阅https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/上的这个有用指南
sudo mkdir -p /data/db
最后开始 mongod 流程如下
sudo mongod
在我的终端上mongod (失败)和 sudo mongod (成功)命令的输入。
MacBook-Pro-2:appscheck admin$ mongod
2017-12-10T08:12:06.166+0530 I CONTROL [initandlisten] MongoDB starting : pid=2698 port=27017 dbpath=/data/db 64-bit host=MacBook-Pro-2.local
2017-12-10T08:12:06.166+0530 I CONTROL [initandlisten] db version v3.4.10
2017-12-10T08:12:06.166+0530 I CONTROL [initandlisten] git version: 078f28920cb24de0dd479b5ea6c66c644f6326e9
2017-12-10T08:12:06.166+0530 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2n 7 Dec 2017
2017-12-10T08:12:06.166+0530 I CONTROL [initandlisten] allocator: system
2017-12-10T08:12:06.166+0530 I CONTROL [initandlisten] modules: none
2017-12-10T08:12:06.166+0530 I CONTROL [initandlisten] build environment:
2017-12-10T08:12:06.166+0530 I CONTROL [initandlisten] distarch: x86_64
2017-12-10T08:12:06.166+0530 I CONTROL [initandlisten] target_arch: x86_64
2017-12-10T08:12:06.166+0530 I CONTROL [initandlisten] options: {}
2017-12-10T08:12:06.166+0530 I STORAGE [initandlisten] exception in initAndListen: 20 Attempted to create a lock file on a read-only directory: /data/db, terminating
2017-12-10T08:12:06.166+0530 I NETWORK [initandlisten] shutdown: going to close listening sockets...
2017-12-10T08:12:06.166+0530 I NETWORK [initandlisten] shutdown: going to flush diaglog...
2017-12-10T08:12:06.167+0530 I CONTROL [initandlisten] now exiting
2017-12-10T08:12:06.167+0530 I CONTROL [initandlisten] shutting down with code:100
MacBook-Pro-2:appscheck admin$ sudo mongod
Password:
2017-12-10T08:12:14.084+0530 I CONTROL [initandlisten] MongoDB starting : pid=2700 port=27017 dbpath=/data/db 64-bit host=MacBook-Pro-2.local
2017-12-10T08:12:14.084+0530 I CONTROL [initandlisten] db version v3.4.10
2017-12-10T08:12:14.084+0530 I CONTROL [initandlisten] git version: 078f28920cb24de0dd479b5ea6c66c644f6326e9
2017-12-10T08:12:14.084+0530 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2n 7 Dec 2017
2017-12-10T08:12:14.084+0530 I CONTROL [initandlisten] allocator: system
2017-12-10T08:12:14.084+0530 I CONTROL [initandlisten] modules: none
2017-12-10T08:12:14.084+0530 I CONTROL [initandlisten] build environment:
2017-12-10T08:12:14.084+0530 I CONTROL [initandlisten] distarch: x86_64
2017-12-10T08:12:14.084+0530 I CONTROL [initandlisten] target_arch: x86_64
2017-12-10T08:12:14.084+0530 I CONTROL [initandlisten] options: {}
2017-12-10T08:12:14.084+0530 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=7680M,session_max=20000,eviction=(threads_min=4,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),
2017-12-10T08:12:14.472+0530 I CONTROL [initandlisten]
2017-12-10T08:12:14.472+0530 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-12-10T08:12:14.472+0530 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2017-12-10T08:12:14.472+0530 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2017-12-10T08:12:14.472+0530 I CONTROL [initandlisten]
2017-12-10T08:12:14.472+0530 I CONTROL [initandlisten]
2017-12-10T08:12:14.472+0530 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
2017-12-10T08:12:14.560+0530 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
2017-12-10T08:12:14.707+0530 I INDEX [initandlisten] build index on: admin.system.version properties: { v: 2, key: { version: 1 }, name: "incompatible_with_version_32", ns: "admin.system.version" }
2017-12-10T08:12:14.707+0530 I INDEX [initandlisten] building index using bulk method; build may temporarily use up to 500 megabytes of RAM
2017-12-10T08:12:14.719+0530 I INDEX [initandlisten] build index done. scanned 0 total records. 0 secs
2017-12-10T08:12:14.720+0530 I COMMAND [initandlisten] setting featureCompatibilityVersion to 3.4
2017-12-10T08:12:14.720+0530 I NETWORK [thread1] waiting for connections on port 27017
然后我打开新终端开始查询 MongoDB ,它有效。
MacBook-Pro-2:appscheck admin$ mongo
MongoDB shell version v3.4.10
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.10
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
Server has startup warnings:
2017-12-10T08:12:14.472+0530 I CONTROL [initandlisten]
2017-12-10T08:12:14.472+0530 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-12-10T08:12:14.472+0530 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2017-12-10T08:12:14.472+0530 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2017-12-10T08:12:14.472+0530 I CONTROL [initandlisten]
2017-12-10T08:12:14.472+0530 I CONTROL [initandlisten]
2017-12-10T08:12:14.472+0530 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
> show dbs;
admin 0.000GB
local 0.000GB
> use practice
switched to db practice
>
那就是它。
答案 10 :(得分:0)
我遇到了同样的问题,在查看日志文件时,我看到了以下内容:
{2019-03-09T11:57:32.136-0500 I STORAGE [initandlisten]
exception in initAndListen: NonExistentPath:
Data directory /usr/local/var/mongodb not found., terminating}
所以我创建了目录/usr/local/var/mongodb
发出了brew服务重启mongodb命令,我可以打开mongo控制台。
答案 11 :(得分:-1)
我的猜测是,在尝试通过" mongo"进行测试时,您没有正在运行的数据库。命令。
首先在终端中运行此命令:
mongod
然后打开另一个终端窗口并运行:
蒙戈
现在一切都应该正常工作。
答案 12 :(得分:-1)
通过在尝试运行mongod时删除“sudo rm /data/db/mongod.lock”文件来解决我的类似错误。 现在你可以运行mongod然后运行mongo。