mongodb mongo - 无法连接到服务器 127.0.0.1:27017

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13312358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 12:55:29  来源:igfitidea点击:

mongo - couldn't connect to server 127.0.0.1:27017

mongodbpymongo

提问by Tampa

I am coming from riak and redis where I never had an issue with this services starting, or to interact.

我来自 riak 和 redis,在那里我从未遇到过此服务启动或交互的问题。

This is a pervasive problem with mongo and am rather clueless. Restarting does not help.I am new to mongo.

这是 mongo 的一个普遍问题,而且我相当无能为力。重新启动没有帮助。我是 mongo 的新手。

mongo
MongoDB shell version: 2.2.1
connecting to: test
Fri Nov  9 16:44:06 Error: couldn't connect to server 127.0.0.1:27017 src/mongo/shell/mongo.js:91
exception: connect failed

This is what I see in the logs.

这是我在日志中看到的。

 now open)
Fri Nov  9 16:44:34 [conn47] end connection 10.29.16.208:5306 (1 connection now open)
Fri Nov  9 16:45:04 [initandlisten] connection accepted from 10.29.16.208:5307 #48 (2 connections now open)
Fri Nov  9 16:45:04 [conn48] end connection 10.29.16.208:5307 (1 connection now open)
Fri Nov  9 16:45:04 [initandlisten] connection accepted from 10.29.16.208:5308 #49 (2 connections now open)
Fri Nov  9 16:45:04 [conn49] end connection 10.29.16.208:5308 (1 connection now open)
Fri Nov  9 16:45:34 [initandlisten] connection accepted from 10.29.16.208:5316 #50 (2 connections now open)
Fri Nov  9 16:45:34 [conn50] end connection 10.29.16.208:5316 (1 connection now open)
Fri Nov  9 16:45:34 [initandlisten] connection accepted from 10.29.16.208:5317 #51 (2 connections now open)
Fri Nov  9 16:45:34 [conn51] end connection 10.29.16.208:5317 (1 connection now open)
Fri Nov  9 16:46:04 [initandlisten] connection accepted from 10.29.16.208:5320 #52 (2 connections now open)
Fri Nov  9 16:46:04 [conn52] end connection 10.29.16.208:5320 (1 connection now open)
Fri Nov  9 16:46:04 [initandlisten] connection accepted from 10.29.16.208:5321 #53 (2 connections now open)
Fri Nov  9 16:46:04 [conn53] end connection 10.29.16.208:5321 (1 conn

采纳答案by Jenna

This error is what you would see if the mongo shell was not able to talk to the mongod server.

如果 mongo shell 无法与 mongod 服务器通信,您将看到此错误。

This could be because the address was wrong (host or IP) or that it was not running. One thing to note is the log trace provided does not cover the "Fri Nov 9 16:44:06" of your mongo timestamp.

这可能是因为地址错误(主机或 IP)或者它没有运行。需要注意的一件事是提供的日志跟踪不包括您的mongo timestamp.

Can you:

你是否可以:

  1. Provide the command line arguments (if any) used to start your mongod process
  2. Provide the log file activity from the mongod startup as well as logs during the mongo shell startup attempt?
  3. Confirm that your mongod process is being started on the same machine as the mongo shell?
  1. 提供用于启动 mongod 进程的命令行参数(如果有)
  2. 提供 mongod 启动的日志文件活动以及 mongo shell 启动尝试期间的日志?
  3. 确认您的 mongod 进程与 mongo shell 在同一台机器上启动?

回答by Ravi Hamsa

Normally this caused because you didn't start mongod process before you try starting mongo shell.

通常这是因为您在尝试启动 mongo shell 之前没有启动 mongod 进程。

Start mongod server

启动mongod服务器

mongod

Open another terminal window

打开另一个终端窗口

Start mongo shell

启动 mongo shell

mongo

回答by shakthydoss

Resolved.

解决。

This problem could be solved by the below mentioned 4 steps

这个问题可以通过下面提到的4个步骤来解决

1) Remove .lock file

1) 删除 .lock 文件

sudo rm /var/lib/mongodb/mongod.lock 

2) repair the mongodb

2)修复mongodb

mongod -–repair

3) start the mongod server

3)启动mongod服务器

sudo service mongod start 

4) start the mongo client

4)启动mongo客户端

mongo

For more details take a look at http://shakthydoss.com/error-couldnt-connect-to-server-127-0-0-127017-srcmongoshellmongo-js-exception-connect-failed/

有关更多详细信息,请查看http://shakthydoss.com/error-couldnt-connect-to-server-127-0-0-127017-srcmongoshellmongo-js-exception-connect-failed/

http://shakthydoss.com/technical/error-couldnt-connect-to-server-127-0-0-127017-srcmongoshellmongo-js-exception-connect-failed/

http://shakthydoss.com/technical/error-couldnt-connect-to-server-127-0-0-127017-srcmongoshellmongo-js-exception-connect-failed/

回答by poorva

This method only works if you want to repair your data files without preserving the original files

此方法仅适用于要修复数据文件而不保留原始文件的情况

To find where you dbpath resides- vim /etc/mongodb.conf

要找到 dbpath 所在的位置 - vim /etc/mongodb.conf

check for option dbpath=

检查选项dbpath=

(I have dbpath=/var/lib/mongodb)

(我有 dbpath=/var/lib/mongodb)

Default:/data/db/

默认值:/data/db/

Typical locations include: /srv/mongodb, /var/lib/mongodb or /opt/mongodb .

典型的位置包括:/srv/mongodb、/var/lib/mongodb 或 /opt/mongodb。

Replace the /var/lib/mongodbwith your dbpath

/var/lib/mongodb替换为您的dbpath

sudo rm /var/lib/mongodb/mongod.lock
sudo mongod --dbpath /var/lib/mongodb/ --repair
sudo mongod --dbpath /var/lib/mongodb/ --journal

(Make sure that you leave you terminal running in which you have run above lines, dont press 'Ctrl+c' or quit it.) Type the command to start mongo now in another window.

(确保你的终端在上面运行的地方运行,不要按 'Ctrl+c' 或退出它。)现在在另一个窗口中输入命令来启动 mongo。

Hope this works for you ! for those who want to repair your data files while preserving the original filesmongo recover

希望这对你有用!对于那些想要在保留原始文件的同时修复数据文件的人mongo recovery

回答by William Hu

MAC OS

苹果系统

Check status

检查状态

brew services list

Something like:

就像是:

Name    Status  User Plist
mongodb stopped  

start it

启动它

brew services start mongodb

Try it again

再试一次

mongoor mongo --port 27017

mongo或者 mongo --port 27017

回答by Finn

In my case:

就我而言:

First I open config file

首先我打开配置文件

sudo vi /etc/mongodb.conf

Comment IP and Port like this

像这样注释 IP 和端口

#bind_ip = 127.0.0.1
#port = 27017

Then restart mongodb

然后重启mongodb

sudo service mongodb restart
sudo service mongod restart

Finally, it's work :D

最后,它的工作:D

finn@Finn ~ $ mongo
MongoDB shell version: 2.4.9
connecting to: test
> exit

回答by Finn

A little to late with the answer but I've encountered the same issue

答案有点晚了,但我遇到了同样的问题

Following are the steps which helped me.

以下是帮助我的步骤。

  1. Go to C:\
  2. Create "data" folder
  3. In "data" create "db" folder
  4. open terminal (CMD) -> go to EX:"c:\MongoDB\Server\3.4\bin"
  5. type in mongod => this will start mongo server (leave it opened)
  1. 转到 C:\
  2. 创建“数据”文件夹
  3. 在“数据”中创建“db”文件夹
  4. 打开终端 (CMD) -> 转到 EX:" c:\MongoDB\Server\3.4\bin"
  5. 输入 mongod => 这将启动 mongo 服务器(保持打开状态)

Run using a GUI, EX "robomongo"

使用 GUI 运行,EX“robomongo”

Or

或者

open new terminal (CMD) -> go to EX:"c:\MongoDB\Server\3.4\bin" type in "mongo" command

打开新终端 (CMD) -> 转到 EX:" c:\MongoDB\Server\3.4\bin" 输入 "mongo" 命令

回答by Bipul Chandra Dev Nath

Make sure that your mongo is running. I fixed this problem by trying to repair the mongodb, there I found that the directory required for the db to run was not created. It shows this error

确保您的 mongo 正在运行。我通过尝试修复 mongodb 解决了这个问题,在那里我发现没有创建运行 db 所需的目录。它显示这个错误

Type: mongod, it will show the error

输入:mongod,它会显示错误

exception in initAndListen: 29 Data directory /data/db not found., terminating

exception in initAndListen: 29 Data directory /data/db not found., terminating

Error happen because dbpath /data/db/(default config) does not exist. You need to create data folder and set permission for it.

发生错误是因为 dbpath /data/db/(默认配置)不存在。您需要创建数据文件夹并为其设置权限。

Then I create a folder on my system by the command

然后我通过命令在我的系统上创建一个文件夹

sudo mkdir -p /data/db/and sudo chown id -u/data/db

须藤 mkdir -p /data/db/须藤 chown id -u/data/db

then I run the mongo again and it worked.

然后我再次运行 mongo 并且它起作用了。

回答by Blasanka

I am a windows user and I installed MongoDB in November 2018 and I didn't wanted to setup data/db directories. But after few days, when I open, got an error message:

我是 Windows 用户,我在 2018 年 11 月安装了 MongoDB,但我不想设置 data/db 目录。但是几天后,当我打开时,收到一条错误消息:

MongoDB shell version v4.0.4
connecting to: mongodb://127.0.0.1:27017
2018-12-05T20:42:40.108+0530 E QUERY    [js] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: No connection could be made because the target machine actively refused it. :
connect@src/mongo/shell/mongo.js:257:13
@(connect):1:6
exception: connect failed

Then I tried to fix using all of above answers and didn't worked. When I tried to run mongod it said

然后我尝试使用上述所有答案进行修复,但没有奏效。当我试图运行 mongod 它说

MongoDB starting : pid=12220 port=27017 dbpath=C:\data\db\ 64-bit host=bla

I tried to change the db path(to program files) through file system and also using cmd but those didnot worked.

我试图通过文件系统并使用 cmd 更改数据库路径(到程序文件),但那些没有用。

Solution that worked for me is:

对我有用的解决方案是:

Opened the Task Manager(ctrl + shift + esc) -> Servicestab and there was a MongoDB rowwith the status stopped. Then I right clicked and clicked startand everything working perfectly :).

打开任务管理器ctrl + shift + esc) - >服务选项卡,并有一个MongoDB的行状态停止。然后我右键单击并单击开始,一切正常:)

回答by Khalid

If all the above solution does not work: go to service (start>search>services) and start mongodb service. Then, in a cmd prompt, after going to bin, type :/>mongo

如果以上所有解决方案都不起作用:转到服务 ( start>search>services) 并启动 mongodb 服务。然后,在 cmd 提示符下,进入 bin 后,键入:/>mongo