无法连接到 mongodb errno: 61连接被拒绝

我最近用 自酿的安装了 mongodb-2.6.0。 成功安装之后,我尝试使用 mongo命令进行连接。我收到以下错误,不允许我连接:

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
105334 次浏览

To solve your issue you need to follow the instructions which are then given to you by brew after you use "brew install mongodb".

To have launchd start mongodb at login:

ln -sfv /usr/local/opt/mongodb/*.plist ~/Library/LaunchAgents

Then to load mongodb now:

launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist

Or, if you don't want/need launchctl, you can just run:

mongod --config /usr/local/etc/mongod.conf

You may be able to just run that last command, but it didn't work for me and I needed to run the second command. To save me in the future. I just ran the first command too. Hope that helps!

EDIT Hrishi's method of using brew services mongodb start worked good for me. I think they should include this in the mongo docs.

It can happen when the mongodb service is not running on the mac. To start it, I tried

brew services start mongodb

and it worked.

Edit: According to the discussion on this PR on homebrew: https://github.com/Homebrew/homebrew/issues/30628

brew services is deprecated, I looked around on SO and found these answers now answer the question: What is the correct way to start a mongod service on linux / OS X?

In another tab, you can start the mongo shell with

mongod

Then return to the previous tab and try again. If you're having trouble setting up your mongoshell, check out this link on the mongo shell: http://docs.mongodb.org/manual/tutorial/manage-mongodb-processes/ or this link on installing mongodb: http://docs.mongodb.org/manual/tutorial/install-mongodb-on-os-x/

I had the same error but a different root cause. Thought I'd post the solution here in case anyone else runs into the problem. I got this error after my Mac improperly shut down while I was running mongorestore -d foo dump/foo/.

tl;dr: I fixed the problem by removing the damaged foo.ns file along with foo.0, foo.1,... from my data folder /usr/local/var/mongodb/. Then I restarted the mongo server with brew services restart mongodb and I was back to normal.

Details: I kept getting the error even after trying to start or restart the mongodb service via brew or launchctl. Eventually I ran mongod --dbpath /usr/local/var/mongodb and saw that the service was not actually starting, and the start sequence included the following error: [initandlisten] bad .ns file: /usr/local/var/mongodb/foo.ns [initandlisten] User Assertion: 10079:bad .ns file length, cannot open database I got rid of the bad .ns file and the rest of the data files, and the next time I started the service I was good to go.

I was having the same issue when calling mongod from the command line.

I resolved this by calling instead sudo mongod.

I encountered the exact same issue and here is a clear step by step process to avoid this error.

Step 1 - Installation ( Don't follow this step if you have already installed MongoDB ):

brew update
brew install mongodb

Step 2 - Run Mongo Daemon:

mkdir -p /data/db
sudo mongod

Step 3 - Run Mongo Shell Interface:

mongo

In this sequence, I was able to run the mongo command without any error. I have also detailed the error trace and its solution on this blog.

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:

mongod

Then open another terminal window and run:

mongo

Everything should be working now.

for me on osx, I had to kill old running instance, then restarting worked.

$>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

My similar error is resolved by deleting "sudo rm /data/db/mongod.lock" file while trying to run mongod. Now u can run mongod and then mongo.

If you have already installed MongoDB then first try to run mongod as a sudo user, I was facing the issue because of not running mongod as a superuser.

I have pasted the o/p for both the commands(mongod and sudo mongod) at very bottom, you can check that too but

First try this

sudo mongod

not this

mongod

I had installed MongoDB on my MAC OS X Sierra 10.12.6 by by running the following command in sequence.

brew update
brew install mongodb --devel
brew services start mongodb

then created a directory to which mongod process will write the data, this is optional as mongod process takes it by default, see this useful guide at https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/

sudo mkdir -p /data/db

Finally started mongod process as follows

sudo mongod

Oputput of mongod (failed) and sudo mongod (succeeded) commands on my terminal.

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

Then I opened new terminal to start querying the MongoDB, it worked.

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
>

That's it.

I got this error after I upgraded to mongo 3.6 with homebrew.

The log /usr/local/var/log/mongodb/mongo.log contained the message shutting down with code:62. This error code means that the existing database is too old to run with the current version of mongo.

I found 2 solutions in another SO question:

  • Delete your database folder (/usr/local/var/mongodb)
  • Or follow the upgrade procedure

I chose to upgrade. In my case, that meant I had to downgrade to 3.4, run a command in the mongo console, then upgrade again. Mongo requires you to upgrade one major version at a time, so depending on how far back you were, there could be additional steps. The docs will guide you.

The brew switch * and brew services restart commands made swapping between versions relatively painless.

I had the same issue and in looking in the log file I saw this:

{2019-03-09T11:57:32.136-0500 I STORAGE  [initandlisten]
exception in initAndListen: NonExistentPath:
Data directory /usr/local/var/mongodb not found., terminating}

so I created the directory /usr/local/var/mongodb issued the brew services restart mongodb command I was able to open the mongo console.

For anyone who is here looking for the answer for this after your mac has updated to OS Catalina

run

brew services start mongodb-community


find more info here: https://medium.com/@semmons245/arghhhh-thanks-to-the-catalina-os-update-this-now-needs-changing-987f416ebcbe