[英]Mongoose NodeJS macOS Big Sur MongooseServerSelectionError: connect ECONNREFUSED ::1:27017
I'm using brew mongodb-community on macOS Big Sur and can connect to mongo shell with mongosh.我在 macOS Big Sur 上使用 brew mongodb-community,可以使用 mongosh 连接到 mongo shell。 But when I try to run NodeJS and Mongoose app I get MongooseServerSelectionError: connect ECONNREFUSED::1:27017
.但是当我尝试运行 NodeJS 和 Mongoose 应用程序时,我得到MongooseServerSelectionError: connect ECONNREFUSED::1:27017
。 Why can I connect to mongodb with mongo shell but not NodeJS and Mongoose?为什么我可以使用 mongo shell 连接到 mongodb 而不是 NodeJS 和 Mongoose?
Edit: Sample code from my NodeJS Mongoose code编辑:来自我的 NodeJS Mongoose 代码的示例代码
import mongoose from 'mongoose';
mongoose.connect('mongodb://localhost:27017/mydb', {
useNewUrlParser: true,
useUnifiedTopology: true
});
My solution was to use 127.0.0.1
and NOT localhost
.我的解决方案是使用127.0.0.1
而不是localhost
。 For some reason Mongoose can read mongodb://127.0.0.1:27017/mydb
and NOT mongodb://localhost:27017/mydb
由于某种原因猫鼬可以读取mongodb://127.0.0.1:27017/mydb
而不是mongodb://localhost:27017/mydb
Your MongoDB Server is not up, Try brew services start mongodb-community@6.0.您的 MongoDB 服务器未启动,请尝试 brew services start mongodb-community@6.0。
if not started kindly follow below commands.如果没有开始,请按照以下命令操作。
Step-1: install brew from download brew第 1 步:从下载 brew 安装 brew
" At the end brew shows to add path in your terminal "(which many people forget to add) “最后 brew 显示在您的终端中添加路径”(很多人忘记添加)
For reference, The below commands will appear in your terminal: ==> Next steps:作为参考,以下命令将出现在您的终端中: ==> 后续步骤:
echo '# Set PATH, MANPATH, etc., for Homebrew.' echo '# 为 Homebrew 设置 PATH、MANPATH 等。 >> /Users/xxxx/.zprofile >> /Users/xxxx/.zprofile
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/xxxxx/.zprofile echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/xxxxx/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"评估“$(/opt/homebrew/bin/brew shellenv)”
Step-2: install MongoDB:第二步:安装MongoDB:
brew install mongodb-community@6.0 brew 安装 mongodb-community@6.0
Step-3: Start MongoDB Server:第三步:启动MongoDB服务器:
brew services start mongodb-community@6.0 brew 服务启动 mongodb-community@6.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.