无法连接到 mongodb 错误:无法连接到 src/mongo/shell/mongo.js:L112 处的服务器 127.0.0.1:27017

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16245805/
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 13:10:43  来源:igfitidea点击:

Unable to connect to mongodb Error: couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:L112

mongodb

提问by JP.

When ever I try to connect to mongo db I always get this error as below.

当我尝试连接到 mongo db 时,我总是收到如下错误。

MongoDB shell version: 2.4.3 connecting to: test Fri Apr 26 14:31:46.941 JavaScript execution failed: Error: couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:L112 exception: connect failed

MongoDB shell 版本:2.4.3 连接到:test Fri Apr 26 14:31:46.941 JavaScript 执行失败:错误:无法连接到服务器 127.0.0.1:27017 在 src/mongo/shell/mongo.js:L112 异常:连接失败

I tried various solutions listed in stackoverflow and tried following commands but nothing works.

我尝试了 stackoverflow 中列出的各种解决方案并尝试了以下命令,但没有任何效果。

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

1) 须藤 rm /var/lib/mongodb/mongod.lock

// says such file located at this place.

// 表示位于此位置的此类文件。

2) sudo service mongodb start

2) sudo 服务 mongodb 启动

// error: sudo: service: command not found

// 错误:sudo:服务:找不到命令

3) I've made sure nothing else is running at that port.

3) 我已经确定在那个端口没有其他东西在运行。

4) Even tried uninstalling and installing it again.

4)甚至尝试卸载并再次安装它。

5) Also tried kill pid

5) 也试过 kill pid

回答by Juan Serrats

Same error here, this worked for me:

同样的错误,这对我有用:

sudo mongod --repair

followed by

其次是

sudo mongod

(forget about serviceand start)

(忘记服务开始

Then on a different tab/terminal:

然后在不同的选项卡/终端上:

mongo

回答by Esakkiappan .E

Start mongod server first

首先启动mongod服务器

mongod

Open another terminal window

打开另一个终端窗口

Start mongo shell

启动 mongo shell

mongo

回答by yogesh singh

This is due to locking of Mongodb
sudo rm /var/lib/mongodb/mongod.lock
sudo service mongodb restart

这是由于Mongodb
sudo rm /var/lib/mongodb/mongod.lock
sudo service mongodb restart锁定

回答by MADHAIYAN M

Please Start the MongoDB server first and try to connect.

请先启动MongoDB服务器并尝试连接。

Pre-requisite:

先决条件:

Create space for storing DB and tables in any directory Example (windows): D:\mongodbdata\

在任何目录中创建用于存储数据库和表的空间示例 (windows): D:\mongodbdata\

Steps:

脚步:

  1. Start server

    mongod --port 5000 --dbpath D:\mongodbdata\ (please mention above created path)

  2. Connect mongodb server (Run in another terminal/console)

    mongo --port 5000

  1. 启动服务器

    mongod --port 5000 --dbpath D:\mongodbdata\(请在上面提到创建的路径)

  2. 连接 mongodb 服务器(在另一个终端/控制台中运行)

    蒙戈 -- 端口 5000

回答by Debasis Mondal

If any one is facing the problem in windows machine then follow the steps.

如果有人在 Windows 机器中遇到问题,请按照以下步骤操作。

  1. Add the path of mongodb in the environmental variable.
  2. Create a directory C:\Data\db.
  3. Open a terminal(cmd) and write mongod. (it will initiate the server).
  4. Open another terminal and write your own code.
  1. 在环境变量中添加mongodb的路径。
  2. 创建目录 C:\Data\db。
  3. 打开终端(cmd)并编写mongod。(它将启动服务器)。
  4. 打开另一个终端并编写自己的代码。

回答by Sathish

I had the same issue, In my case there was a mismatch of ipaddress in mongo config

我遇到了同样的问题,就我而言,mongo 配置中的 ipaddress 不匹配

~# mongo
MongoDB shell version: 2.6.7
connecting to: test
2015-02-02T17:48:52.302+0530 warning: Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
2015-02-02T17:48:52.303+0530 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

When I checked my configuration file, I have specified the bind address to eth0 ip(10.8.10.111) not loopback address(127.0.0.1), So I have changed the ip address to loopback as like bind_ip = 127.0.0.1in /etc/mongod.conf file and restart it with service mongod restart, finally works

当我检查我的配置文件时,我已将绑定地址指定为 eth0 ip(10.8.10.111) 而不是环回地址 (127.0.0.1),因此我已将 ip 地址更改为环回,就像bind_ip = 127.0.0.1在 /etc/mongod.conf 文件中一样重新启动它service mongod restart,终于可以工作了

# mongo 
MongoDB shell version: 2.6.7
connecting to: test
> 

回答by Vikas Kukreti

mongod --configsvr --smallfiles --nojournal --dbpath cfg/1 --port 26052 --fork --logpath cfg/3.log >/dev/null

Create a single mongos server on the default port

在默认端口上创建单个 mongos 服务器

sleep 2000
mongos --configdb ${HOSTNAME}:26050,${HOSTNAME}:26051,${HOSTNAME}:26052 --fork --logpath mongos.log >/dev/null

Sleep for some time before starting the mongos you will not face the problem

在开始mongos之前睡一段时间你不会遇到问题

回答by sunitj

Faced same issue, my understanding is(it could be wrong)
1. Make sure mongodb is up and running
2. For linux access as sudo and for windows if connecting localhost turning off firewall may help but its not necessary
3. Just type mongo, it will try to connect to localhost by default. You need to specify IP if you are connecting to a remote server. By default testdb will be used.

面临同样的问题,我的理解是(它可能是错误的)
1. 确保mongodb 已启动并正在运行
2. 对于 linux 访问作为 sudo 和 Windows,如果连接 localhost 关闭防火墙可能会有所帮助,但没有必要
3. 只需键入 mongo,默认情况下,它将尝试连接到本地主机。如果要连接到远程服务器,则需要指定 IP。默认情况下将使用测试数据库。

回答by ketankk

First start mongodservice then mongoor mongos

首先启动mongod服务然后mongomongos

回答by chandraprakash

Mongo DB requires space to store it files. So you should create folder structure for Mongo DB before starting the Mongodb server/client.

Mongo DB 需要空间来存储文件。因此,您应该在启动 Mongodb 服务器/客户端之前为 Mongo DB 创建文件夹结构。

for e.g. MongoDb/Dbfiles where Mongo DB is installed.

例如安装了 Mongo DB 的 MongoDb/Dbfiles。

Then in cmd promt exe mongod.exe and mongo.exe for client and done.

然后在 cmd promt exe mongod.exe 和 mongo.exe 为客户端并完成。