I'm using VS Code, MongoDB Community Server 5.0, node.js backend, and just updated to Monterey 12.0.1. I've followed the instructions to install MongoDB using Mongo instructions here. Homebrew installed successfully, MongoDB 5.0 looks like it's installed successfully in the opt/homebrew directory and looks like it's good to go.
There's two problems I'm facing when trying to start and connect to the database from VS Code:
brew services start mongodb-community
(or mongodb-community@5.0) command.After checking the status using brew services
, it initially says started with a fresh install/reinstall, but then changes to error immediately. MongoDB was working fine earlier this week, but stopped working randomly and then completely after I updated to 12.0.1. The reason I updated was due to the next issue.
Earlier in the week MongoDB started up fine and I was able to connect to the server from VSCode using npm run dev
, but I began getting the following issue randomly:
MongoNetworkError: failed to connect to server [localhost:27017] on first connect [Error: connect ECONNREFUSED ::1:27017 at TCPConnectWrap.afterConnect [as oncomplete]
Initially I reinstalled MongoDB and was able to connect again, but then MongoDB began having problems with starting like I mentioned in the first issue. When it was starting, VS Code refused to connect to the database and kept giving the same error.
I haven't made any changes to the backend code/configuration that would have impacted the ability for me to connect from VS Code. When MongoDB was able to start, I could connect to the database through MongoDB Compass just fine, but VS Code still refused to connect. We did some research and tried to change the mongoose.connect from localhost to localhost:27017, tried starting with a fresh repo, reinstalling MongoDB a few times, but with no luck.
We've been trying to resolve this for days now and I'm wondering if this is just an issue with using a new M1 and having to wait for the software to catch up. Any help would be greatly appreciated!
For anyone that finds this question - we figured it out after 3 days of trial and error.
We ended up specifying the database folder for the mongod
command since brew services
wasn't working using mongo: alias mongod="mongo --port 27017 --dbpath /opt/homebrew/var/mongodb”
. This solved the first issue and we are able to start the server using mongod
.
To solve the second issue, I replaced localhost with the specified ip for the mongoose.connect configuration for the app:
mongoose.connect('mongodb://127.0.0.1:27017/app')
For some reason localhost was unable to connect, but using 127.0.0.1:27017 worked.