MongoDB - 无法连接到服务器 127.0.0.1:27017,原因是:连接被拒绝:connect@src/mongo/shell/mongo.js:374:17

MongoDB - couldn't connect to server 127.0.0.1:27017, caused by :Connection refused : connect@src/mongo/shell/mongo.js:374:17

我用自制软件安装了 mongodb,我使用 Mac OS Big Sur 当我用

启动 mongo db 时
$ brew services start mongodb-community

它说 mongodb 它成功启动但是当我 运行

$ brew services list

mongodb-community 的状态是“错误”,如果我尝试在 cmd 中键入命令“mongo”,它给了我这个问题标题中解释的问题:

MongoDB shell version v4.4.3
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :
connect@src/mongo/shell/mongo.js:374:17
@(connect):2:6
exception: connect failed
exiting with code 1

我运行命令行中的以下内容来解决问题。

mongod --dbpath /usr/local/var/mongodb

这是重新安装后的结果。

提示我的错误消息是:

{"t":{"$date":"2021-04-22T11:33:48.608-04:00"},"s":"E", "c":"STORAGE", "id":20557, "ctx":"initandlisten","msg":"DBException in initAndListen, terminating","attr":{"error":"NonExistentPath: Data directory /data/db not found. Create the missing directory or specify another path using (1) the --dbpath command line option, or (2) by adding the 'storage.dbPath' option in the configuration file."}}

更具体地说

"error":"NonExistentPath: Data directory /data/db not found. Create the missing directory or specify another path using (1) the --dbpath command line option, or (2) by adding the 'storage.dbPath' option in the configuration file."

-Archlinux

搜索 [.mongodb / mongosh /]

给配置执行权限。

如果您想 运行 mongodb 在终端上输入:

mongod --dbpath /usr/local/var/mongodb

如果不起作用,请仅使用:

mongod

如果你想通过 SHELL 或 TERMINAL 在你的数据库中创建 CRUD,运行 这个:

mongo