Linux Errr 'mongo.js:L112 错误:无法连接到服务器 127.0.0.1:27017 在 src/mongo/shell/mongo.js:L112'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16120397/
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
Errr 'mongo.js:L112 Error: couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:L112'
提问by user1992275
I can't connect to MongoDB. In Ubuntu it works, but I'm working in CentOSnow. This is the error message:
我无法连接到MongoDB。在 Ubuntu 中它可以工作,但我现在在CentOS 中工作。这是错误消息:
MongoDB shell version: 2.4.2
connecting to: test
Sat Apr 20 07:22:09.390 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.2
连接到:测试
Sat Apr 20 07:22:09.390 JavaScript 执行失败:错误:无法连接到服务器 127.0.0.1:27017 在 src/mongo/shell/mongo.js:L112
异常:连接失败
I tried removing the mongod.lock
file, but it doesn't work.
我尝试删除该mongod.lock
文件,但它不起作用。
采纳答案by ccma
Try to remove /var/lib/mongodb/mongod.lock and restart mongdo service
尝试删除 /var/lib/mongodb/mongod.lock 并重新启动 mongdo 服务
sudo rm /var/lib/mongodb/mongod.lock
sudo service mongodb restart
回答by lkrzysiak
I had the same problem in the past. In my case, I had insufficient free space for journal files. Freeing some space solved the problem.
我过去也遇到过同样的问题。就我而言,日志文件的可用空间不足。释放一些空间解决了这个问题。
回答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, use
此方法仅适用于要修复数据文件而不保留原始文件的情况。要查找 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 the above lines. Don't press Ctrl+ Cor quit it.) Type the command to start mongo
now in another window.
(确保让终端运行,并在其中运行了上面的行。不要按Ctrl+C或退出它。)mongo
现在在另一个窗口中键入命令以启动。
I hope this works for you! For those who want to repair your data files while preserving the original files, see mongo
recover.
我希望这对你有用!对于那些想要在保留原始文件的同时修复数据文件的人,请参阅mongo
恢复。
回答by gittinOld
You also get this error if you've changed the default port for mongoDB in /etc/mongo.conf
如果您在 /etc/mongo.conf 中更改了 mongoDB 的默认端口,您也会收到此错误
To connect via the shell in this case use:-
在这种情况下要通过外壳连接,请使用:-
$mongo 127.0.0.1:your_new_port_number
OR
或者
$mongo -u <user> -p <pass> --host <host> --port your_new_port_number
from MongoDB docs
回答by Sumit Ramteke
If you are running Ubuntu, then there is an issue with folder ownership.
如果您运行的是 Ubuntu,则文件夹所有权存在问题。
Run these commands:
运行这些命令:
Stop the MongoDB service
sudo service mongodb stop
Remove the MongoDB lock file
sudo rm /var/lib/mongodb/mongod.lock
Change ownership from root to the MongoDB path
sudo chown -R mongodb:mongodb /var/lib/mongodb/
Start the MongoDB service
sudo service mongodb start
Test the mongo application
mongo
停止MongoDB服务
sudo service mongodb stop
删除 MongoDB 锁定文件
sudo rm /var/lib/mongodb/mongod.lock
将所有权从 root 更改为 MongoDB 路径
sudo chown -R mongodb:mongodb /var/lib/mongodb/
启动MongoDB服务
sudo service mongodb start
测试 mongo 应用程序
mongo
Then you will be able to execute successfully (I hope).
然后你就可以成功执行(我希望)。
参考:Stack Exchange 站点数据库管理员对错误的回答:更改 mongodb 数据目录时无法连接到服务器 127.0.0.1:27017 src/mongo/shell/mongo.js:91!
回答by edelans
I don't know why @lkrzysiak answer was downvoted: it worked for me ! Insufficient free space CAN be the source of this error that I got too, see the logs I got:
我不知道为什么@lkrzysiak 的回答被否决了:它对我有用!可用空间不足也可能是我得到的这个错误的根源,请参阅我得到的日志:
Mon Aug 12 17:02:59.159 [initandlisten] recover : no journal files present, no recovery needed
Mon Aug 12 17:02:59.159 [initandlisten]
Mon Aug 12 17:02:59.159 [initandlisten] ERROR: Insufficient free space for journal files
Mon Aug 12 17:02:59.159 [initandlisten] Please make at least 3379MB available in /var/lib/mongodb/journal or use --smallfiles
Mon Aug 12 17:02:59.159 [initandlisten]
Mon Aug 12 17:02:59.159 [initandlisten] exception in initAndListen: 15926 Insufficient free space for journals, terminating
Mon Aug 12 17:02:59.159 dbexit:
Mon Aug 12 17:02:59.159 [initandlisten] shutdown: going to close listening sockets...
After some cleaning to free some space, it worked like a charm !
经过一些清理以释放一些空间后,它就像一个魅力!
回答by modon
I faced the same problem. Running mongod inside bin (where you have your mongodb installed) worked for me. I also had insufficient space.
我遇到了同样的问题。在 bin (安装 mongodb 的地方)内运行 mongod 对我有用。我也没有足够的空间。
回答by Chinmay
It could be combination of $PATH and Permission issue.
它可能是 $PATH 和权限问题的组合。
Try following steps given below:
尝试以下步骤:
Update your $PATH variable to point to your MongoDB bin file. In my case brew install MongoDB to this folder:
更新您的 $PATH 变量以指向您的 MongoDB bin 文件。在我的情况下 brew install MongoDB 到这个文件夹:
/usr/local/Cellar/mongodb/2.4.6/
In order to update your $PATH variable, do following:
为了更新您的 $PATH 变量,请执行以下操作:
$ sudo vi /etc/paths
Then, press ‘i' to insert text in Vi and append the your MongoDB path to the end of the ‘paths' file and restart the terminal.
然后,按“i”在 Vi 中插入文本并将您的 MongoDB 路径附加到“路径”文件的末尾并重新启动终端。
/usr/local/Cellar/mongodb/2.4.6/bin
Use ‘Esc : w q' to save and exit from Vi editor.
使用 'Esc : wq' 保存并退出 Vi 编辑器。
Use echo to display your path variable:
使用 echo 显示您的路径变量:
$ echo $PATH
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/Cellar/mongodb/2.4.6/bin
Now try to check the Mongo version, if you get following, then you are on the right track!
现在尝试检查 Mongo 版本,如果您得到关注,那么您就在正确的轨道上!
$ mongo --version
MongoDB shell version: 2.4.6
Now we need to create the database directory. I used the default ‘/data/db' location suggested in MongoDB docs. I also created a log directory to avoid any permission issues while Mongo tries to create any logs. Change ownership and that will do the job.
现在我们需要创建数据库目录。我使用了 MongoDB 文档中建议的默认“/data/db”位置。我还创建了一个日志目录,以避免在 Mongo 尝试创建任何日志时出现任何权限问题。改变所有权,这将完成工作。
$ sudo mkdir /data/db
$ sudo mkdir /data/log
$ whoami
username
$ chown -R username /data
Now, we will create a default config file for MongoDB to be provided for the first time we run ‘mongod' command. Now, I will also like to point out that ‘mongod' will start a service, which will listen for incoming data connections. This is similar having ‘$service mysqld start' executed.Let's go ahead and create the config file. Please keep in mind that I have created very basic config file. However, you can add many other variables to configure MongoDB. This is the first time I am playing with MongoDB, so I just know as much as I read on MongoDB docs!I created ‘mongodb.conf'.
现在,我们将为 MongoDB 创建一个默认配置文件,以便在我们第一次运行 'mongod' 命令时提供。现在,我还要指出 'mongod' 将启动一个服务,该服务将侦听传入的数据连接。这与执行 '$service mysqld start' 类似。让我们继续创建配置文件。请记住,我已经创建了非常基本的配置文件。但是,您可以添加许多其他变量来配置 MongoDB。这是我第一次使用 MongoDB,所以我知道的和我在 MongoDB 文档上读到的一样多!我创建了“mongodb.conf”。
$ sudo vi /etc/mongodb.conf
Add following:
添加以下内容:
fork = true
port = 27017
quiet = true
dbpath = /data/db
logpath = /data/log/mongod.log
logappend = true
journal = true
Please note that the default port for MongoDB server is 27017. Use your own path for dbpath and logpath you created in Step – 5. Don't forget to close and save the conf file.
请注意,MongoDB 服务器的默认端口是 27017。使用您在步骤 – 5 中创建的 dbpath 和 logpath 的自己的路径。不要忘记关闭并保存 conf 文件。
Now we are all set to start our MongoDB service. Open two instances of Terminal.In Terminal 1, type in:
现在我们都准备好启动我们的 MongoDB 服务了。打开两个终端实例。在终端 1 中,输入:
$ sudo mongod -f /etc/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 3516
all output going to: /data/log/mongod.log
child process started successfully, parent exiting
If you get above message, then know that you have successfully started your Mongod service.
如果您收到以上消息,则说明您已成功启动您的 Mongod 服务。
Now, to connect to it, in Terminal 2 type following:
现在,要连接到它,请在终端 2 中键入以下内容:
$mongo test
MongoDB shell version: 2.4.6
connecting to: test
Server has startup warnings:
Tue Sep 3 16:55:43.527 [initandlisten]
Tue Sep 3 16:55:43.527 [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
>
Ignore the warnings, but you are successfully connected to the ‘test' database! Cool!
忽略警告,但您已成功连接到“测试”数据库!凉爽的!
That's all. I applied this solution, when I tried to install copy of MongoDB on my Mac for the first time. See if this help you too.
就这样。当我第一次尝试在我的 Mac 上安装 MongoDB 副本时,我应用了这个解决方案。看看这是否对你也有帮助。
For detailed post you can go here - http://arcanebytes.com/2013/09/03/mongodb-installation-on-mac-os-x/#comment-1036112094.
有关详细信息,您可以访问这里 - http://arcanebytes.com/2013/09/03/mongodb-installation-on-mac-os-x/#comment-1036112094。
I hope it helps!
我希望它有帮助!
Cheers, Chinmay
干杯,钦美