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

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

Couldn't connect to server 127.0.0.1:27017

mongodb

提问by alexchenco

I'm getting the following error:

我收到以下错误:

alex@alex-K43U:/$ mongo
MongoDB shell version: 2.2.0
connecting to: test
Thu Oct 11 11:46:53 Error: couldn't connect to server 127.0.0.1:27017 src/mongo/shell/mongo.js:91
exception: connect failed
alex@alex-K43U:/$ 

This is what happens when I try to start mongodb:

这是我尝试启动 mongodb 时发生的情况:

* Starting database mongodb                                             [fail]

I already tried mongo --repair

我已经试过了 mongo --repair

I made chown and chmod to var, lib, and data/db and logmongodb.

我将 chown 和 chmod 转换为var、lib 和 data/db 并记录mongodb。

Not sure what else to do. Any suggestions?

不知道还能做什么。有什么建议?

mongodb.log:

mongodb.log:

***** SERVER RESTARTED *****


Thu Oct 11 08:29:40 
Thu Oct 11 08:29:40 warning: 32-bit servers don't have journaling enabled by default. Please use --journal if you want durability.
Thu Oct 11 08:29:40 
Thu Oct 11 08:29:41 [initandlisten] MongoDB starting : pid=1052 port=27017 dbpath=/var/lib/mongodb 32-bit host=alex-K43U
Thu Oct 11 08:29:41 [initandlisten] 
Thu Oct 11 08:29:41 [initandlisten] ** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of data
Thu Oct 11 08:29:41 [initandlisten] **       see http://blog.mongodb.org/post/137788967/32-bit-limitations
Thu Oct 11 08:29:41 [initandlisten] **       with --journal, the limit is lower
Thu Oct 11 08:29:41 [initandlisten] 
Thu Oct 11 08:29:41 [initandlisten] db version v2.2.0, pdfile version 4.5
Thu Oct 11 08:29:41 [initandlisten] git version: f5e83eae9cfbec7fb7a071321928f00d1b0c5207
Thu Oct 11 08:29:41 [initandlisten] build info: Linux domU-12-31-39-01-70-B4 2.6.21.7-2.fc8xen #1 SMP Fri Feb 15 12:39:36 EST 2008 i686 BOOST_LIB_VERSION=1_49
Thu Oct 11 08:29:41 [initandlisten] options: { config: "/etc/mongodb.conf", dbpath: "/var/lib/mongodb", logappend: "true", logpath: "/var/log/mongodb/mongodb.log" }
Thu Oct 11 08:29:41 [initandlisten] Unable to check for journal files due to: boost::filesystem::basic_directory_iterator constructor: No such file or directory: "/var/lib/mongodb/journal"
************** 
Unclean shutdown detected.
Please visit http://dochub.mongodb.org/core/repair for recovery instructions.
*************
Thu Oct 11 08:29:41 [initandlisten] exception in initAndListen: 12596 old lock file, terminating
Thu Oct 11 08:29:41 dbexit: 
Thu Oct 11 08:29:41 [initandlisten] shutdown: going to close listening sockets...
Thu Oct 11 08:29:41 [initandlisten] shutdown: going to flush diaglog...
Thu Oct 11 08:29:41 [initandlisten] shutdown: going to close sockets...
Thu Oct 11 08:29:41 [initandlisten] shutdown: waiting for fs preallocator...
Thu Oct 11 08:29:41 [initandlisten] shutdown: closing all files...
Thu Oct 11 08:29:41 [initandlisten] closeAllFiles() finished
Thu Oct 11 08:29:41 dbexit: really exiting now

EDIT:

编辑:

I removed the lock then did mongod repair and got this error:

我移除了锁,然后进行了 mongod 修复并收到此错误:

Thu Oct 11 12:05:37 [initandlisten] exception in initAndListen: 10309 Unable to create/open lock file: /data/db/mongod.lock errno:13 Permission denied Is a mongod instance already running?, terminating

so I did it with sudo:

所以我用sudo做到了:

alex@alex-K43U:~$ sudo mongod --repair
Thu Oct 11 12:05:42 
Thu Oct 11 12:05:42 warning: 32-bit servers don't have journaling enabled by default. Please use --journal if you want durability.
Thu Oct 11 12:05:42 
Thu Oct 11 12:05:42 [initandlisten] MongoDB starting : pid=5129 port=27017 dbpath=/data/db/ 32-bit host=alex-K43U
Thu Oct 11 12:05:42 [initandlisten] 
Thu Oct 11 12:05:42 [initandlisten] ** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of data
Thu Oct 11 12:05:42 [initandlisten] **       see http://blog.mongodb.org/post/137788967/32-bit-limitations
Thu Oct 11 12:05:42 [initandlisten] **       with --journal, the limit is lower
Thu Oct 11 12:05:42 [initandlisten] 
Thu Oct 11 12:05:42 [initandlisten] db version v2.2.0, pdfile version 4.5
Thu Oct 11 12:05:42 [initandlisten] git version: f5e83eae9cfbec7fb7a071321928f00d1b0c5207
Thu Oct 11 12:05:42 [initandlisten] build info: Linux domU-12-31-39-01-70-B4 2.6.21.7-2.fc8xen #1 SMP Fri Feb 15 12:39:36 EST 2008 i686 BOOST_LIB_VERSION=1_49
Thu Oct 11 12:05:42 [initandlisten] options: { repair: true }
Thu Oct 11 12:05:42 [initandlisten] Unable to check for journal files due to: boost::filesystem::basic_directory_iterator constructor: No such file or directory: "/data/db/journal"
Thu Oct 11 12:05:42 [initandlisten] finished checking dbs
Thu Oct 11 12:05:42 dbexit: 
Thu Oct 11 12:05:42 [initandlisten] shutdown: going to close listening sockets...
Thu Oct 11 12:05:42 [initandlisten] shutdown: going to flush diaglog...
Thu Oct 11 12:05:42 [initandlisten] shutdown: going to close sockets...
Thu Oct 11 12:05:42 [initandlisten] shutdown: waiting for fs preallocator...
Thu Oct 11 12:05:42 [initandlisten] shutdown: closing all files...
Thu Oct 11 12:05:42 [initandlisten] closeAllFiles() finished
Thu Oct 11 12:05:42 [initandlisten] shutdown: removing fs lock...
Thu Oct 11 12:05:42 dbexit: really exiting now

But still having the same problem.

但仍然有同样的问题。

采纳答案by Trott

The log indicates that mongodb is terminating because there is an old lock file.

日志表明mongodb正在终止,因为有一个旧的锁文件。

If you are not and were not running with journaling, remove the lock file, run repair, and start mongodb again.

如果您没有并且没有使用日记功能运行,请删除锁定文件,运行修复,然后再次启动 mongodb。

If you are or were running with journaling turned on, see the relevant Mongo DB docs. Note that they say "If you are running with Journaling you should not do a repair to recover to a consistent state." So if you were journaling, the repair may have made things worse.

如果您正在运行或正在运行日志,请参阅相关的 Mongo DB 文档。请注意,他们说“如果您使用日记功能运行,则不应进行修复以恢复到一致状态。” 所以如果你在写日记,维修可能会让事情变得更糟。

回答by Nanhe Kumar

Step 1: Remove lock file.
sudo rm /var/lib/mongodb/mongod.lock

Step 2: Repair mongodb. 
sudo mongod --repair 

Step 3: start mongodb.
sudo start mongodb 
or
sudo service mongodb start

Step 4: Check status of mongodb.
sudo status mongodb 
or   
sudo service mongodb status

Step 5: Start mongo console.
mongo 

回答by eloone

Did you run mongodbefore running mongo?

你跑mongod前跑了mongo吗?

I followed installation instructions for mongodb from http://docs.mongodb.org/manual/tutorial/install-mongodb-on-os-x/and I had the same error as you only when I ran mongobefore actually running the mongo process with mongod. I thought installing mongodb would also launch it but you need to launch it manually with mongodbefore you do anything else that needs mongodb.

我按照http://docs.mongodb.org/manual/tutorial/install-mongodb-on-os-x/中的 mongodb 安装说明进行操作,只有当我mongo在实际运行 mongo 进程之前运行时才遇到与您相同的错误mongod. 我认为安装 mongodb 也会启动它,但是mongod在执行任何需要 mongodb 的其他操作之前,您需要手动启动它。

回答by Javier Gomez

This it's because the mongod process it's down, you must to run the commands bellow in order to get up the mongod process:

这是因为 mongod 进程已关闭,您必须运行以下命令才能启动 mongod 进程:

~$ sudo service mongodb stop

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

~$ sudo mongod --repair --dbpath /var/lib/mongodb

~$ sudo mongod --fork --logpath /var/lib/mongodb/mongodb.log --dbpath /var/lib/mongodb

~$ sudo service mongodb start

Hope this helps you.

希望这对你有帮助。

回答by u2549359

Try

尝试

sudo service mongodb start

This solved my issue.

这解决了我的问题。

回答by msnfreaky

Check your filesystem free space and increase it if its less. This also could cause the mongo not to start. Check the /var/log/mongodb/mongodb.log file.

检查您的文件系统可用空间,如果较少则增加它。这也可能导致 mongo 无法启动。检查 /var/log/mongodb/mongodb.log 文件。

ERROR: Insufficient free space for journal files
Please make at least 3379MB available in /var/lib/mongodb/journal or use --smallfiles

回答by Kiran P.

Try running mongodbefore mongo.

尝试运行mongod之前mongo

sudo /usr/sbin/mongodon my opensuse

sudo /usr/sbin/mongod在我的 openuse 上

This solved my problem,

这解决了我的问题,

回答by user3470929

so first you have to remove the mongod.lock file by below command

所以首先你必须通过下面的命令删除 mongod.lock 文件

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

and then restart the mongo service by issuing below command

然后通过发出以下命令重新启动 mongo 服务

sudo service mongod restart 

回答by Luke Le

In windows run cmd as Admin:

在 Windows 中以管理员身份运行 cmd:

  1. Create directory:

    mkdir c:\mongo\data\db

  2. Install service:

    mongod.exe --install --logpath c:\mongo\logs --logappend --bind_ip 127.0.0.1 --dbpath c:\mongo\data\db --directoryperdb

  3. Start MongoDB:

    net start MongoDB

  1. 创建目录:

    mkdir c:\mongo\data\db

  2. 安装服务:

    mongod.exe --install --logpath c:\mongo\logs --logappend --bind_ip 127.0.0.1 --dbpath c:\mongo\data\db --directoryperdb

  3. 启动MongoDB:

    网络启动MongoDB

4.Start Mongo Shell:

4.启动Mongo Shell:

c:\mongo\bin\mongo.exe

This solution work fine for me

这个解决方案对我来说很好用

回答by Efren

You can check with netstat -anp | grep 27017to see if the port is in use by another process.

您可以查看netstat -anp | grep 27017该端口是否正在被另一个进程使用。