MongoDB 正在运行但无法使用 shell 进行连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7958228/
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
MongoDB running but can't connect using shell
提问by Garrett
CentOS 5.x Linux with MongoDB 2.0.1 (tried main and legacy-static)
CentOS 5.x Linux 和 MongoDB 2.0.1(尝试了 main 和 legacy-static)
MongoDB is running:
MongoDB 正在运行:
root 31664 1.5 1.4 81848 11148 ? Sl 18:40 0:00 ./mongod -f mongo.conf -vvvvv --fork
Using a simple shell connect to get to the server fails:
使用简单的 shell 连接访问服务器失败:
[root@xxxx bin]# ./mongo
MongoDB shell version: 2.0.1
connecting to: test
Mon Oct 31 18:41:32 Error: couldn't connect to server 127.0.0.1 shell/mongo.js:84
exception: connect failed
The web interface on port 28017 loads fine, as does using the MongoDB shell from a remote Linux host. Can also telnet to localhost:27017, which means no ports are blocked. There is no SELinux running on this machine as well. I have also tried explicitly specifying localhost:2017/db
to no avail.
端口 28017 上的 Web 界面加载良好,从远程 Linux 主机使用 MongoDB shell 也是如此。也可以 telnet 到 localhost:27017,这意味着没有端口被阻塞。这台机器上也没有运行 SELinux。我也尝试过明确指定localhost:2017/db
无济于事。
$ ./mongo remote-ip:27017
MongoDB shell version: 2.0.1
connecting to: remote-ip:27017/test
> show dbs
local 0.03125GB
>
Logs are completely mum on the subject:
日志在这个主题上完全是妈妈的:
.....
Mon Oct 31 18:40:34 [initandlisten] fd limit hard:1024 soft:1024 max conn: 819
Mon Oct 31 18:40:34 [initandlisten] waiting for connections on port 27017
Mon Oct 31 18:40:34 BackgroundJob starting: snapshot
Mon Oct 31 18:40:34 BackgroundJob starting: ClientCursorMonitor
Mon Oct 31 18:40:34 BackgroundJob starting: PeriodicTask::Runner
Mon Oct 31 18:40:34 [websvr] fd limit hard:1024 soft:1024 max conn: 819
Mon Oct 31 18:40:34 [websvr] admin web console waiting for connections on port 28017
Stracing the mongo shell client shows only one problematic call:
跟踪 mongo shell 客户端仅显示一个有问题的调用:
[pid 31708] connect(4, {sa_family=AF_INET, sin_port=htons(27017), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 EBADF (Bad file descriptor)
Filesystem is clean, no ulimit restrictions (running as root for testing). I can see from the strace that the mongo
client is trying to connect via TCP (AF_INET
), but since it is local and MongoDB creates a file socket, is there a way to tell the client to connect through that instead? Or better yet, why would the client be throwing a EBADF?
文件系统干净,没有 ulimit 限制(以 root 身份运行进行测试)。我可以从 strace 中看到mongo
客户端正在尝试通过 TCP ( AF_INET
)进行连接,但是由于它是本地的并且 MongoDB 创建了一个文件套接字,有没有办法告诉客户端通过它进行连接?或者更好的是,为什么客户会抛出 EBADF?
EDIT: My basic Mongo conf:
编辑:我的基本 Mongo conf:
dbpath=/root/mongodb-linux-i686-2.0.1/data
logpath=/root/mongodb-linux-i686-2.0.1/logs/mongo.log
slowms=15
rest=1
回答by JaKi
I think there is some default config what is missing in this version of mongoDb client. Try to run:
我认为这个版本的 mongoDb 客户端缺少一些默认配置。尝试运行:
mongo 127.0.0.1:27017
It's strange, but then I've experienced the issue went away :) (so the simple command 'mongo' w/o any params started to work again for me)
这很奇怪,但后来我遇到了问题消失了:)(所以没有任何参数的简单命令“mongo”开始再次对我来说有效)
[Ubuntu Linux 11.10 x64 / MongoDB 2.0.1]
回答by Andrew M. Slack
You may want to check your config to see if the bind_ip is set
您可能需要检查您的配置以查看是否设置了 bind_ip
bind_ip: 127.0.0.1
bind_ip:127.0.0.1
If it is then this permits only local logins. Comment this out and restart mongo, this may help.
如果是,则只允许本地登录。将此注释掉并重新启动 mongo,这可能会有所帮助。
回答by Vladimir Lazarevski
I had a similar problem, well actually the same (mongo process is running but can't connect to it). What I did was went to my database path and removed mongod.lock, and then gave it another try (restarted mongo). After that it worked.
我有一个类似的问题,实际上是一样的(mongo 进程正在运行但无法连接到它)。我所做的是转到我的数据库路径并删除 mongod.lock,然后再试一次(重新启动 mongo)。在那之后它起作用了。
Hope it works for you too. mongodb repair on ubuntu
希望它也适用于你。 ubuntu 上的 mongodb 修复
回答by mashuai
Delete /var/lib/mongodb/mongod.lock
, then issue sudo service mongodb start
, then mongo
.
删除/var/lib/mongodb/mongod.lock
,然后发出sudo service mongodb start
,然后mongo
。
回答by Ionic? Biz?u
I had same problem. In my case MongoDBserver wasn't running.
我有同样的问题。在我的情况下,MongoDB服务器没有运行。
Try to open this in your web browser:
尝试在您的网络浏览器中打开它:
http://localhost:28017
http://localhost:28017
If you can't, this means that you have to start MongoDB server.
如果你不能,这意味着你必须启动 MongoDB 服务器。
Run mongod
in another terminal tab.
Then in your main tab run mongo
which is is the shell that connects to your MongoDB server.
mongod
在另一个终端选项卡中运行。然后在您的主选项卡中运行mongo
,这是连接到您的 MongoDB 服务器的 shell。
回答by ssv
Open the file /etc/mongod.conf and add the ip of the machine from where you are connecting, to bind_ip
打开文件 /etc/mongod.conf 并将您连接的机器的 ip 添加到bind_ip
bind_ip = 127.0.0.1,your Remote Machine Ip Address Here
bind_ip = 127.0.0.1,你的远程机器IP地址在这里
Ex:-
前任:-
bind_ip = 127.0.0.1,192.168.1.5
Restart mongodb service:
重启mongodb服务:
sudo service mongod restart
Make sure mongodb port is opened in the firewall.
确保 mongodb 端口在防火墙中打开。
You can also comment the line, if you are not worried about security.
如果您不担心安全性,您也可以注释该行。
回答by William Biesty
I had this problem as well. Is your MongoDB journaling? I noticed the following "preallocate" entries in the log file. Once I saw the last line "waiting for connections on port", I could connect. Notice that this "faster" mode took 12 minutes to intialize.
我也有这个问题。你的 MongoDB 是日记吗?我注意到日志文件中有以下“预分配”条目。一旦我看到最后一行“等待端口上的连接”,我就可以连接了。请注意,这种“更快”模式需要 12 分钟才能初始化。
William
威廉
Tue Apr 17 16:48:01 [initandlisten] MongoDB starting : pid=2248 port=27017 dbpath=E:\MongoData 64-bit host=ME
Tue Apr 17 16:48:01 [initandlisten] db version v2.0.0-rc0, pdfile version 4.5
Tue Apr 17 16:48:01 [initandlisten] git version: 8d4bf50111352cee5a4f1abf25b63442d6c45dc4
Tue Apr 17 16:48:01 [initandlisten] build info: windows (6, 1, 7601, 2, 'Service Pack 1') BOOST_LIB_VERSION=1_42
Tue Apr 17 16:48:01 [initandlisten] options: { bind_ip: "ip", dbpath: "E:\MongoData", directoryperdb: true, journal: true, logpath: "E:\MongoData\mongo.log", quiet: true, rest: true, service: true }
Tue Apr 17 16:48:01 [initandlisten] journal dir=E:/MongoData/journal
Tue Apr 17 16:48:01 [initandlisten] recover : no journal files present, no recovery needed
Tue Apr 17 16:48:02 [initandlisten] preallocateIsFaster=true 9.68
Tue Apr 17 16:48:04 [initandlisten] preallocateIsFaster=true 8.44
Tue Apr 17 16:48:06 [initandlisten] preallocateIsFaster=true 9.68
Tue Apr 17 16:48:06 [initandlisten] preallocateIsFaster check took 4.921 secs
Tue Apr 17 16:48:06 [initandlisten] preallocating a journal file E:/MongoData/journal/prealloc.0
Tue Apr 17 16:52:37 [initandlisten] preallocating a journal file E:/MongoData/journal/prealloc.1
Tue Apr 17 16:56:54 [initandlisten] preallocating a journal file E:/MongoData/journal/prealloc.2
Tue Apr 17 17:01:42 [initandlisten] waiting for connections on port 27017
Tue Apr 17 17:01:42 [websvr] admin web console waiting for connections on port 28017
回答by Abhay Dandekar
By default, mongodb is configured to listen only to localhost. Excerpt from mongodb default config file :
默认情况下,mongodb 配置为仅侦听 localhost。摘自 mongodb 默认配置文件:
# Listen to local interface only. Comment out to listen on all interfaces.
bind_ip=127.0.0.1
One needs to comment the bind_ip to listen from external entities.
需要注释 bind_ip 以从外部实体收听。
You wont be able to add shards unless you start listening on non-local interfaces.
除非您开始侦听非本地接口,否则您将无法添加分片。
HTH,
哈,
Abhay Dandekar
阿拜·丹德卡尔
回答by Jmlevick
This is actually not an error... What happens here is that Mongo relies on a daemonin order to run the local database server, so in order to "fire up" the mongo serverin your shell, you have to start the mongo servicefirst.
这实际上不是错误......这里发生的事情是 Mongo 依赖一个守护进程来运行本地数据库服务器,所以为了在你的 shell 中“启动” mongo 服务器,你必须启动mongo 服务第一的。
For Fedora Linux(wich is the Distro I use) You have to run these commands:
对于Fedora Linux(这是我使用的发行版),您必须运行以下命令:
1 sudo service mongod start
2 mongo
And there you have it! the server is going to run. Now, If you want Mongo service to Start when the system boots then you have to run:
你有它!服务器将要运行。现在,如果您希望 Mongo 服务在系统启动时启动,那么您必须运行:
sudo chkconfig --levels 235 mongod on
And that's all! If you do that, now in the shell you just have to type mongoin order to start the server but that's pretty much it, the problem is you have to start the SERVICE first and then the SERVER :)
就这样!如果你这样做了,现在你只需要在 shell 中输入mongo来启动服务器,但差不多就是这样,问题是你必须先启动 SERVICE,然后是 SERVER :)
P.S. The commands I posted might work on other linux distros as well, not just in fedora... In case not maybe you have to tweak some wordsdepending on the distro you're using ;)
PS命令我张贴在其他Linux发行版可能工作为好,不只是在Fedora的......如果不是也许你不得不调整一些的话这取决于你使用的发行版;)
回答by user2487889
If your bind_ip is set to anything other than 127.0.0.1 then you'll need to add the ip explicitly even from the local machine. So simply use the same method that you're using on the remote box on the local box. At least that's what did it for me.
如果您的 bind_ip 设置为 127.0.0.1 以外的任何内容,那么您甚至需要从本地机器显式添加 ip。所以只需使用您在本地盒子上的远程盒子上使用的相同方法。至少这就是对我所做的。