[英]Cannot connect to mongodb errno:61 Connection refused
I recently installed mongodb-2.6.0 with Homebrew .我最近用Homebrew安装了 mongodb-2.6.0 。 After successfully installed, I tried to connect using the
mongo
command.安装成功后,我尝试使用
mongo
命令进行连接。 I am receiving the following errors which do not allow me to connect:我收到以下不允许我连接的错误:
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
It can happen when the mongodb service is not running on the mac.当 mongodb 服务未在 mac 上运行时,可能会发生这种情况。 To start it, I tried
要开始它,我试过
brew services start mongodb
and it worked.它奏效了。
Edit: According to the discussion on this PR on homebrew: https://github.com/Homebrew/homebrew/issues/30628编辑:根据关于 homebrew 的这个 PR 的讨论: https : //github.com/Homebrew/homebrew/issues/30628
brew services
is deprecated, I looked around on SO and found these answers now answer the question: What is the correct way to start a mongod service on linux / OS X? brew services
已被弃用,我环顾四周,发现这些答案现在回答了这个问题: 在 linux/OS X 上启动 mongod 服务的正确方法是什么?
I encountered the exact same issue and here is a clear step by step process to avoid this error.我遇到了完全相同的问题,这里有一个明确的分步过程,以避免出现此错误。
Step 1 - Installation ( Don't follow this step if you have already installed MongoDB ):第 1 步- 安装(如果您已经安装了 MongoDB,请不要执行此步骤):
brew update
brew install mongodb
Step 2 - Run Mongo Daemon:第 2 步- 运行 Mongo 守护进程:
mkdir -p /data/db
sudo mongod
Step 3 - Run Mongo Shell Interface:第 3 步- 运行 Mongo Shell 界面:
mongo
In this sequence, I was able to run the mongo
command without any error.在这个序列中,我能够运行
mongo
命令而没有任何错误。 I have also detailed the error trace and its solution on this blog .我还在此博客上详细介绍了错误跟踪及其解决方案。
To solve your issue you need to follow the instructions which are then given to you by brew after you use "brew install mongodb".要解决您的问题,您需要按照使用“brew install mongodb”后 brew 给您的说明进行操作。
To have launchd start mongodb at login:要在登录时启动 mongodb:
ln -sfv /usr/local/opt/mongodb/*.plist ~/Library/LaunchAgents
Then to load mongodb now:然后现在加载mongodb:
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist
Or, if you don't want/need launchctl, you can just run:或者,如果您不想要/不需要launchctl,您可以运行:
mongod --config /usr/local/etc/mongod.conf
You may be able to just run that last command, but it didn't work for me and I needed to run the second command.您也许可以只运行最后一个命令,但它对我不起作用,我需要运行第二个命令。 To save me in the future.
将来救我。 I just ran the first command too.
我也刚运行了第一个命令。 Hope that helps!
希望有帮助!
EDIT Hrishi's method of using brew services mongodb start
worked good for me.编辑Hrishi 使用
brew services mongodb start
的方法对我很有用。 I think they should include this in the mongo docs.我认为他们应该将其包含在 mongo 文档中。
In another tab, you can start the mongo shell with在另一个选项卡中,您可以使用以下命令启动 mongo shell
mongod
蒙哥
Then return to the previous tab and try again.然后返回上一个选项卡并重试。 If you're having trouble setting up your mongoshell, check out this link on the mongo shell: http://docs.mongodb.org/manual/tutorial/manage-mongodb-processes/ or this link on installing mongodb: http://docs.mongodb.org/manual/tutorial/install-mongodb-on-os-x/
如果您遇到问题设置您的mongoshell,检查出蒙戈外壳此链接: http://docs.mongodb.org/manual/tutorial/manage-mongodb-processes/或上安装MongoDB的链接: HTTP:/ /docs.mongodb.org/manual/tutorial/install-mongodb-on-os-x/
I was having the same issue when calling mongod
from the command line.从命令行调用
mongod
时我遇到了同样的问题。
I resolved this by calling instead sudo mongod
.我通过调用
sudo mongod
解决了这个问题。
for me on osx, I had to kill old running instance, then restarting worked.对于我在 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
I had the same error but a different root cause.我有同样的错误,但根本原因不同。 Thought I'd post the solution here in case anyone else runs into the problem.
以为我会在这里发布解决方案,以防其他人遇到问题。 I got this error after my Mac improperly shut down while I was running
mongorestore -d foo dump/foo/
.在我运行
mongorestore -d foo dump/foo/
,我的 Mac 不正常关闭后出现此错误。
tl;dr : I fixed the problem by removing the damaged foo.ns
file along with foo.0
, foo.1
,... from my data folder /usr/local/var/mongodb/
. tl;dr :我通过从我的数据文件夹
/usr/local/var/mongodb/
删除损坏的foo.ns
文件以及foo.0
、 foo.1
、... 解决了这个问题。 Then I restarted the mongo server with brew services restart mongodb
and I was back to normal.然后我用
brew services restart mongodb
重新启动了 mongo 服务器,我又恢复了正常。
Details : I kept getting the error even after trying to start or restart the mongodb service via brew or launchctl.详细信息:即使在尝试通过 brew 或 launchctl 启动或重新启动 mongodb 服务后,我仍然收到错误消息。 Eventually I ran
mongod --dbpath /usr/local/var/mongodb
and saw that the service was not actually starting, and the start sequence included the following error: [initandlisten] bad .ns file: /usr/local/var/mongodb/foo.ns [initandlisten] User Assertion: 10079:bad .ns file length, cannot open database
I got rid of the bad .ns
file and the rest of the data files, and the next time I started the service I was good to go.最终我运行
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
我摆脱了糟糕的.ns
文件和数据文件的剩余部分,接下来的时间,我开始我是好服务走。
For anyone who is here looking for the answer for this after your mac has updated to OS Catalina对于在您的 Mac 更新到 OS Catalina 后在这里寻找答案的任何人
run跑
brew services start mongodb-community
find more info here: https://medium.com/@semmons245/arghhhh-thanks-to-the-catalina-os-update-this-now-needs-changing-987f416ebcbe在这里找到更多信息: https : //medium.com/@semmons245/arghhhh-thanks-to-the-catalina-os-update-this-now-needs-changed-987f416ebcbe
I got this error after I upgraded to mongo 3.6 with homebrew.使用自制软件升级到 mongo 3.6 后出现此错误。
The log /usr/local/var/log/mongodb/mongo.log
contained the message shutting down with code:62
.日志
/usr/local/var/log/mongodb/mongo.log
包含shutting down with code:62
的消息。 This error code means that the existing database is too old to run with the current version of mongo.此错误代码表示现有数据库太旧,无法使用当前版本的 mongo 运行。
I found 2 solutions in another SO question :我在另一个SO 问题中找到了 2 个解决方案:
/usr/local/var/mongodb
)/usr/local/var/mongodb
)I chose to upgrade.我选择升级。 In my case, that meant I had to downgrade to 3.4, run a command in the mongo console, then upgrade again.
就我而言,这意味着我必须降级到 3.4,在 mongo 控制台中运行命令,然后再次升级。 Mongo requires you to upgrade one major version at a time, so depending on how far back you were, there could be additional steps.
Mongo 要求您一次升级一个主要版本,因此根据您过去的时间,可能会有额外的步骤。 The docs will guide you.
文档将指导您。
The brew switch
* and brew services restart
commands made swapping between versions relatively painless. brew switch
*和brew services restart
命令使得版本之间的切换相对轻松。
If you have already installed MongoDB then first try to run mongod as a sudo user, I was facing the issue because of not running mongod as a superuser.如果您已经安装了 MongoDB,那么首先尝试以 sudo 用户身份运行 mongod,由于没有以超级用户身份运行 mongod,我遇到了这个问题。
I have pasted the o/p for both the commands( mongod and sudo mongod ) at very bottom, you can check that too but我已经在最底部粘贴了两个命令( mongod和sudo mongod )的 o/p,你也可以检查一下,但是
First try this先试试这个
sudo mongod
not this不是这个
mongod
I had installed MongoDB on my MAC OS X Sierra 10.12.6 by by running the following command in sequence.我通过按顺序运行以下命令在我的 MAC OS X Sierra 10.12.6 上安装了 MongoDB。
brew update
brew install mongodb --devel
brew services start mongodb
then created a directory to which mongod process will write the data, this is optional as mongod process takes it by default, see this useful guide at https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/然后创建了一个目录,mongod 进程将向其中写入数据,这是可选的,因为 mongod 进程默认使用它,请参阅https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os 上的有用指南-X/
sudo mkdir -p /data/db
Finally started mongod process as follows最后启动mongod流程如下
sudo mongod
Oputput of mongod (failed) and sudo mongod (succeeded) commands on my terminal.在我的终端上输出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
Then I opened new terminal to start querying the MongoDB , it worked.然后我打开新终端开始查询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
>
That's it.就是这样。
I had the same issue and in looking in the log file I saw this:我有同样的问题,在查看日志文件时我看到了这个:
{2019-03-09T11:57:32.136-0500 I STORAGE [initandlisten]
exception in initAndListen: NonExistentPath:
Data directory /usr/local/var/mongodb not found., terminating}
so I created the directory /usr/local/var/mongodb
issued the brew services restart mongodb command I was able to open the mongo console.所以我创建了目录
/usr/local/var/mongodb
发出 brew services restart mongodb 命令我能够打开 mongo 控制台。
My guess is that you do not have a running database while trying to access testing through the "mongo" command.
我的猜测是您在尝试通过“mongo”命令访问测试时没有正在运行的数据库。
First run this command in terminal:
首先在终端中运行此命令:
mongod
Then open another terminal window and run:
然后打开另一个终端窗口并运行:
mongo蒙戈
Everything should be working now.
现在一切都应该正常了。
My similar error is resolved by deleting "sudo rm /data/db/mongod.lock" file while trying to run mongod.我的类似错误是通过在尝试运行 mongod 时删除“sudo rm /data/db/mongod.lock”文件来解决的。 Now u can run mongod and then mongo.
现在你可以运行 mongod,然后运行 mongo。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.