MongoDB 正在运行但无法使用 shell 连接
CentOS 5.x Linux 与 MongoDB 2.0.1(尝试了 main 和legacy-static)
MongoDB 正在运行:
root 31664 1.5 1.4 81848 11148 ? Sl 18:40 0:00 ./mongod -f mongo.conf -vvvvv --fork
使用简单的 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
端口 28017 上的 Web 界面加载正常,就像使用 MongoDB shell 一样远程 Linux 主机。还可以 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
>
日志对这个主题完全保持沉默:
.....
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
追踪 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)
文件系统是干净的,没有 ulimit 限制(以 root 身份运行进行测试)。我可以从strace中看到mongo
客户端正在尝试通过TCP(AF_INET
)进行连接,但由于它是本地的并且MongoDB创建了一个文件套接字,有没有办法告诉客户端通过它进行连接?或者更好的是,为什么客户端会抛出 EBADF?
编辑:我的基本 Mongo 配置:
dbpath=/root/mongodb-linux-i686-2.0.1/data
logpath=/root/mongodb-linux-i686-2.0.1/logs/mongo.log
slowms=15
rest=1
CentOS 5.x Linux with MongoDB 2.0.1 (tried main and legacy-static)
MongoDB is running:
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:
[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.
$ ./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:
[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?
EDIT: My basic 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
如果您收到以下消息,则
您必须尝试以 root 以外的用户身份启动 mongodb 服务。您必须是 root 用户。因此,以 root 身份登录,然后运行以下命令,如下
所示:
If you are getting the following message
You must be trying to start the mongodb service as user other than root. You must be root user. Thus log in as root and then run following command as follows:
followed by
如果您的 mongoDB 服务器(远程服务器)的版本高于 4.0.3,那么您将面临此问题。因此,您应该将当前的 mongo-client shell 替换为以下 mongo :
然后您的 mongo 客户端将能够连接您的删除 mongodb
If your mongoDB server(remote server)'s version is greater then 4.0.3, then you will face this issue. Hence you should replace your current mongo-client shell with below mongo :
Then you mongo client will be able to connect your remove mongodb
启动mongod后
获取shell
After starting the mongod
to get shell
在 Ubuntu 上:
1 月 27 日星期三 10:21:32 错误:无法连接到服务器 127.0.0.1 shell/mongo.js:84
异常:连接失败
解决方案
通过以下命令查找 mongodb 是否正在运行:
如果 mongo 没有运行,您会得到:
您看到 mongo 守护进程不存在。
然后通过配置文件启动它(使用root priev):
您可以看到其他详细信息:
打开一个新终端查看 mongod --config /etc/mongodb.conf & 的结果
然后输入 mongo。它应该正在运行或 grep
现在
您获得了 mongoDB shell
这不是故事的结束。我将发布修复方法,以便它每次都会自动启动,大多数开发机器每天都会关闭,并且VM必须在下次启动时自动启动mongo。
On Ubuntu:
Wed Jan 27 10:21:32 Error: couldn't connect to server 127.0.0.1 shell/mongo.js:84
exception: connect failed
Solution
look for if mongodb is running by following command:
If mongo is not running you get:
You are seeing that the mongo daemon is not there.
Then start it through configuration file(with root priev):
you can see the other details:
Open a new terminal to see the result of mongod --config /etc/mongodb.conf &
then type mongo. It should be running or grep
NOW
you get the mongoDB shell
This is not the end of story. I will post the repair method so that it starts automatically every time, most development machine shutdowns every day and the VM must have mongo started automatically at next boot.
我认为这个答案还没有被接受,所以我只添加我的 2 美分。
我刚才也遇到了完全相同的问题。一段时间后,我意识到我已经在 iptables 规则中将 localhost 锁定了。因此,请检查您的防火墙。
I don't see this having an accepted answer yet, so I'll just add my 2 cents.
I had the exact same issue just now. After a while I realized I've locked localhost out in my iptables rules. So, check your firewall.
与其说是一个答案,不如说是一个仅供参考:我刚刚点击这个并通过搜索发现了这个问题。以下是我的经历的详细信息:
Shell error
Mongo 日志显示
这可能表明另一个答案(JaKi)正在经历同样的事情,其中一些连接被清除并且 shell(其他客户端)再次可以访问
Not so much an answer but more of an FYI:I've just hit this and found this question as a result of searching. Here is the details of my experience:
Shell error
Mongo logs reveal
This perhaps indicates the other answer (JaKi) was experiencing the same thing, where some connections were purged and access made possible again for the shell (other clients)
如果您的 bind_ip 设置为 127.0.0.1 以外的任何值,那么您甚至需要从本地计算机显式添加 IP。因此,只需在本地盒子上使用与远程盒子相同的方法即可。至少这对我来说是这样的。
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.
面临与上述 Garrett 描述的错误相同的问题。
1. 启用日志功能的 MongoDB 服务器正在运行,如使用 ps 命令所示
2. Mongo客户端或Mongoose驱动无法连接数据库。
解决方案:
1. 删除 Mongo.lock 文件似乎可以使 CentOS 服务器恢复正常。
2. 我们在生产环境中运行 MongoDB 还相当陌生,并且每周都会看到同样的问题出现几次。
3. 我们设置了一个 cron 计划来定期清理锁定文件并通知管理员发生了事件。
寻找此问题的错误修复或任何其他更永久的方法来解决它。
Facing the same issue with the error described by Garrett above.
1. MongoDB Server with journaling enabled is running as seen using ps command
2. Mongo client or Mongoose driver are unable to connect to the database.
Solution :
1. Deleting the Mongo.lock file seems to bring life back to normal on the CentOS server.
2. We are fairly new in running MongoDB in production and have been seeing the same issue cropping up a couple of times a week.
3. We've setup a cron schedule to regularly cleanup the lock file and intimate the admin that an incident has occurred.
Searching for a bug fix to this issue or any other more permanent way to resolve it.
这实际上不是一个错误...这里发生的是 Mongo 依赖一个守护进程来运行本地数据库服务器,因此为了“启动”mongo 服务器< /em> 在你的 shell 中,你必须首先启动 mongo 服务。
对于Fedora Linux(这是我使用的发行版),您必须运行这些命令:
现在您已经完成了!服务器即将运行。现在,如果你想要 Mongo 服务
要在系统启动时启动,您必须运行:
仅此而已!如果您这样做,现在您只需在 shell 中按顺序输入 mongo
启动服务器,但仅此而已,问题是你必须先启动 SERVICE,然后再启动 SERVER :)
PS 我发布的命令也可能适用于其他 Linux 发行版,而不仅仅是 Fedora ......以防万一也许您必须根据您使用的发行版调整一些单词;)
This is actually not an error... What happens here is that Mongo relies on a daemon in order to run the local database server, so in order to "fire up" the mongo server in your shell, you have to start the mongo service first.
For Fedora Linux (wich is the Distro I use) You have to run these commands:
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:
And that's all! If you do that, now in the shell you just have to type mongo in 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 :)
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 words depending on the distro you're using ;)
默认情况下,mongodb 配置为仅侦听 localhost。摘自mongodb默认配置文件:
需要注释bind_ip以侦听外部实体。
除非您开始侦听非本地接口,否则您将无法添加分片。
HTH,
阿拜·丹德卡
By default, mongodb is configured to listen only to localhost. Excerpt from mongodb default config file :
One needs to comment the bind_ip to listen from external entities.
You wont be able to add shards unless you start listening on non-local interfaces.
HTH,
Abhay Dandekar
我也有这个问题。你的 MongoDB 正在记录日志吗?
我注意到日志文件中存在以下“预分配”条目。
当我看到最后一行“等待端口上的连接”时,我就可以连接了。
请注意,这种“更快”的模式需要 12 分钟才能初始化。
威廉
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.
William
打开文件 /etc/mongod.conf 并将您要连接的计算机的 IP 地址添加到 bind_ip
bind_ip = 127.0.0.1,此处为您的远程计算机 IP 地址
例如:-
重新启动 mongodb 服务:
确保 mongodb 端口在防火墙中打开。
如果您不担心安全问题,也可以评论该行。
Open the file /etc/mongod.conf and add the ip of the machine from where you are connecting, to bind_ip
bind_ip = 127.0.0.1,your Remote Machine Ip Address Here
Ex:-
Restart mongodb service:
Make sure mongodb port is opened in the firewall.
You can also comment the line, if you are not worried about security.
我有同样的问题。就我而言,MongoDB 服务器没有运行。
尝试在网络浏览器中打开它:
如果不能,这意味着您必须启动 MongoDB 服务器。
在另一个终端选项卡中运行 mongod 。
然后在主选项卡中运行 mongo,它是连接到 MongoDB 服务器的 shell。
I had same problem. In my case MongoDB server wasn't running.
Try to open this in your web browser:
If you can't, this means that you have to start MongoDB server.
Run
mongod
in another terminal tab.Then in your main tab run
mongo
which is is the shell that connects to your MongoDB server.删除
/var/lib/mongodb/mongod.lock
,然后发出sudo service mongodb start
,然后发出mongo
。Delete
/var/lib/mongodb/mongod.lock
, then issuesudo service mongodb start
, thenmongo
.我遇到了类似的问题,实际上是相同的(mongo 进程正在运行但无法连接到它)。
我所做的是转到我的数据库路径并删除 mongod.lock,然后再试一次(重新启动 mongo)。之后就成功了。
希望它也适合你。
ubuntu 上的 mongodb 修复
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.
Hope it works for you too.
mongodb repair on ubuntu
您可能需要检查您的配置以查看是否设置了bind_ip
如果是,则仅允许本地登录。注释掉它并重新启动 mongo,这可能会有所帮助。
You may want to check your config to see if the bind_ip is set
If it is then this permits only local logins. Comment this out and restart mongo, this may help.
我认为这个版本的 mongoDb 客户端缺少一些默认配置。尝试运行:
这很奇怪,但后来我遇到了问题消失了:)(所以简单的命令“mongo”没有任何参数开始再次为我工作)
I think there is some default config what is missing in this version of mongoDb client. Try to run:
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)