Cannot connect to mongodb errno:61 Connection refused
我最近用自制软件安装了MongoDB-2.6.0。安装成功后,我尝试使用
1 2 3 4 5 | Failed to connect to 127.0.0.1:27017, reason: errno:61 Connection refused 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 |
当MongoDB服务不在Mac上运行时,可能会发生这种情况。为了开始,我试着
1 | brew services start mongodb |
它奏效了。
编辑:根据关于自制的本公关讨论:https://github.com/homebrew/homebrew/issues/30628
我遇到了完全相同的问题,这里有一个清晰的逐步过程来避免这个错误。
步骤1-安装(如果已安装MongoDB,请不要执行此步骤):
1 2 | brew update brew install mongodb |
步骤2-运行mongo守护进程:
1 2 | mkdir -p /data/db sudo mongod |
步骤3-运行mongo shell接口:
1 | mongo |
在这个序列中,我可以运行
要解决您的问题,您需要遵循BREW在使用"BREW安装MongoDB"后提供给您的说明。
要让launchd在登录时启动mongodb:
1 | ln -sfv /usr/local/opt/mongodb/*.plist ~/Library/LaunchAgents |
然后立即加载mongodb:
1 | launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist |
或者,如果您不想/不需要launchctl,您可以运行:
1 | mongod --config /usr/local/etc/mongod.conf |
您也许可以运行最后一个命令,但它对我不起作用,我需要运行第二个命令。为了将来救我。我刚执行了第一个命令。希望有帮助!
编辑赫里希使用
在另一个选项卡中,您可以使用
mongod
然后返回上一个选项卡并重试。如果您在设置MongoShell时遇到问题,请查看MongoShell上的链接:http://docs.mongodb.org/manual/tutorial/manage-mongodb-processes/或者安装MongoDB的链接:http://docs.mongodb.org/manual/tutorial/install-mongodb-on-os-x/
从命令行调用
我打电话给
我犯了同样的错误,但根本原因不同。我想我会把解决方案贴在这里,以防其他人遇到问题。当我运行
tl;dr:我通过删除损坏的
详细信息:即使在尝试通过BREW或LaunchCtl启动或重新启动MongoDB服务后,我也一直收到错误信息。最后我跑了
mongod --dbpath /usr/local/var/mongodb
[initandlisten] bad .ns file: /usr/local/var/mongodb/foo.ns
[initandlisten] User Assertion: 10079:bad .ns file length, cannot open database
对于我来说,在OSX上,我必须杀死旧的运行实例,然后重新启动才能工作。
1 2 3 4 5 6 | $>ps -aef | grep mongo 502 8047 1 0 11:52AM ?? 0:00.23 /usr/local/opt/mongodb/bin/mongod --config /usr/local/etc/mongod.conf $>sudo kill 8047 $>sudo mongod |
在我用自制软件升级到Mongo 3.6之后,出现了这个错误。
日志
我在另一个问题中找到了两个解决方案:
- 删除数据库文件夹(
/usr/local/var/mongodb ) - 或遵循升级程序
我选择升级。在我的例子中,这意味着我必须降级到3.4,在Mongo控制台中运行一个命令,然后再次升级。Mongo要求您一次升级一个主要版本,因此根据您之前的情况,可能会有其他步骤。医生会指导你的。
我也有同样的问题,在查看日志文件时,我看到了:
1 2 3 | {2019-03-09T11:57:32.136-0500 I STORAGE [initandlisten] exception in initAndListen: NonExistentPath: Data directory /usr/local/var/mongodb not found., terminating} |
所以我创建了目录
如果您已经安装了mongodb,那么首先尝试以sudo用户的身份运行mongod,因为没有以超级用户的身份运行mongod,所以我面临这个问题。
我已经将命令(mongod和sudo mongod)的o/p都粘贴到了最底部,您也可以检查一下,但是
先试试这个
1 | sudo mongod |
不是这个
1 | mongod |
通过按顺序运行以下命令,我在Mac OS X Sierra 10.12.6上安装了MongoDB。
1 2 3 | brew update brew install mongodb --devel brew services start mongodb |
然后创建了一个Mongod进程将写入数据的目录,这是可选的,因为Mongod进程默认使用该目录,请参阅https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x上的有用指南。/
1 | sudo mkdir -p /data/db |
最后启动了Mongod流程,如下所示
1 | sudo mongod |
在我的终端上输出mongod(失败)和sudo mongod(成功)命令。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | MacBook-Pro-2:appscheck admin$ mongod 2017-12-10T08:12:06.166+0530 I CONTROL [initandlisten] MongoDB starting : pid=2698 port=27017 dbpath=/data/db 64-bit host=MacBook-Pro-2.local 2017-12-10T08:12:06.166+0530 I CONTROL [initandlisten] db version v3.4.10 2017-12-10T08:12:06.166+0530 I CONTROL [initandlisten] git version: 078f28920cb24de0dd479b5ea6c66c644f6326e9 2017-12-10T08:12:06.166+0530 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2n 7 Dec 2017 2017-12-10T08:12:06.166+0530 I CONTROL [initandlisten] allocator: system 2017-12-10T08:12:06.166+0530 I CONTROL [initandlisten] modules: none 2017-12-10T08:12:06.166+0530 I CONTROL [initandlisten] build environment: 2017-12-10T08:12:06.166+0530 I CONTROL [initandlisten] distarch: x86_64 2017-12-10T08:12:06.166+0530 I CONTROL [initandlisten] target_arch: x86_64 2017-12-10T08:12:06.166+0530 I CONTROL [initandlisten] options: {} 2017-12-10T08:12:06.166+0530 I STORAGE [initandlisten] exception in initAndListen: 20 Attempted to create a lock file on a read-only directory: /data/db, terminating 2017-12-10T08:12:06.166+0530 I NETWORK [initandlisten] shutdown: going to close listening sockets... 2017-12-10T08:12:06.166+0530 I NETWORK [initandlisten] shutdown: going to flush diaglog... 2017-12-10T08:12:06.167+0530 I CONTROL [initandlisten] now exiting 2017-12-10T08:12:06.167+0530 I CONTROL [initandlisten] shutting down with code:100 MacBook-Pro-2:appscheck admin$ sudo mongod Password: 2017-12-10T08:12:14.084+0530 I CONTROL [initandlisten] MongoDB starting : pid=2700 port=27017 dbpath=/data/db 64-bit host=MacBook-Pro-2.local 2017-12-10T08:12:14.084+0530 I CONTROL [initandlisten] db version v3.4.10 2017-12-10T08:12:14.084+0530 I CONTROL [initandlisten] git version: 078f28920cb24de0dd479b5ea6c66c644f6326e9 2017-12-10T08:12:14.084+0530 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2n 7 Dec 2017 2017-12-10T08:12:14.084+0530 I CONTROL [initandlisten] allocator: system 2017-12-10T08:12:14.084+0530 I CONTROL [initandlisten] modules: none 2017-12-10T08:12:14.084+0530 I CONTROL [initandlisten] build environment: 2017-12-10T08:12:14.084+0530 I CONTROL [initandlisten] distarch: x86_64 2017-12-10T08:12:14.084+0530 I CONTROL [initandlisten] target_arch: x86_64 2017-12-10T08:12:14.084+0530 I CONTROL [initandlisten] options: {} 2017-12-10T08:12:14.084+0530 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=7680M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0), 2017-12-10T08:12:14.472+0530 I CONTROL [initandlisten] 2017-12-10T08:12:14.472+0530 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database. 2017-12-10T08:12:14.472+0530 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted. 2017-12-10T08:12:14.472+0530 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended. 2017-12-10T08:12:14.472+0530 I CONTROL [initandlisten] 2017-12-10T08:12:14.472+0530 I CONTROL [initandlisten] 2017-12-10T08:12:14.472+0530 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000 2017-12-10T08:12:14.560+0530 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data' 2017-12-10T08:12:14.707+0530 I INDEX [initandlisten] build index on: admin.system.version properties: { v: 2, key: { version: 1 }, name:"incompatible_with_version_32", ns:"admin.system.version" } 2017-12-10T08:12:14.707+0530 I INDEX [initandlisten] building index using bulk method; build may temporarily use up to 500 megabytes of RAM 2017-12-10T08:12:14.719+0530 I INDEX [initandlisten] build index done. scanned 0 total records. 0 secs 2017-12-10T08:12:14.720+0530 I COMMAND [initandlisten] setting featureCompatibilityVersion to 3.4 2017-12-10T08:12:14.720+0530 I NETWORK [thread1] waiting for connections on port 27017 |
然后我打开新的终端开始查询MongoDB,它工作了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | MacBook-Pro-2:appscheck admin$ mongo MongoDB shell version v3.4.10 connecting to: mongodb://127.0.0.1:27017 MongoDB server version: 3.4.10 Welcome to the MongoDB shell. For interactive help, type"help". For more comprehensive documentation, see http://docs.mongodb.org/ Questions? Try the support group http://groups.google.com/group/mongodb-user Server has startup warnings: 2017-12-10T08:12:14.472+0530 I CONTROL [initandlisten] 2017-12-10T08:12:14.472+0530 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database. 2017-12-10T08:12:14.472+0530 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted. 2017-12-10T08:12:14.472+0530 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended. 2017-12-10T08:12:14.472+0530 I CONTROL [initandlisten] 2017-12-10T08:12:14.472+0530 I CONTROL [initandlisten] 2017-12-10T08:12:14.472+0530 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000 > show dbs; admin 0.000GB local 0.000GB > use practice switched to db practice > |
就是这样。
我的类似错误是通过在尝试运行mongod时删除"sudo rm/data/db/mongod.lock"文件来解决的。现在你可以运行mongod,然后运行mongo。
My guess is that you do not have a running database while trying to access testing through the"mongo" command.
First run this command in terminal:
1 | mongod |
Then open another terminal window and run:
蒙戈
Everything should be working now.