node.js 在 OSX 上安装和运行 MongoDB
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18452023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Installing and Running MongoDB on OSX
提问by user2517788
If someone can provide some insights here I would GREATLY appreciate it.
如果有人能在这里提供一些见解,我将不胜感激。
I am new to MongoDB, and (relatively) new to the command line.
我是 MongoDB 的新手,并且(相对)命令行的新手。
I had a express/node.js app running on MongoDB locally successfully, but upon restarting my computer, I attempted to restart the Mongo server and it began giving errors and wouldn't start. Since then, I have re-installed Mongo several times only to find the same error occurring. this is what I am receiving:
我在本地成功地在 MongoDB 上运行了一个 express/node.js 应用程序,但是在重新启动计算机后,我尝试重新启动 Mongo 服务器,它开始出现错误并且无法启动。从那以后,我重新安装了 Mongo 几次,却发现同样的错误发生了。这是我收到的:
privee:mongodb-osx-x86_64-2.4.6 jonlinton$ ./bin/mongo
MongoDB shell version: 2.4.6
connecting to: test
Mon Aug 26 14:48:47.168 Error: couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:145
exception: connect failed
Am I missing a step? Should I be creating a config file? Some basic guidance would be really helpful. Thanks in advance for any insights.
我错过了一步吗?我应该创建一个配置文件吗?一些基本的指导会非常有帮助。提前感谢您的任何见解。
采纳答案by Jesus Ruiz
Assuming you have created the data/db directory under bin after install.
假设您在安装后在 bin 下创建了 data/db 目录。
- Start a terminal for your mongo server
- Go to
<mongodb-install-directory>/bindirectory Run the command
./mongod
Start a terminal for your mongo shell
- Go to
<mongodb-install-directory>/bindirectory Run the command (make sure you put the name of the database)
./mongo test
- 为你的 mongo 服务器启动一个终端
- 进入
<mongodb-install-directory>/bin目录 运行命令
./mongod
为你的 mongo shell 启动一个终端
- 进入
<mongodb-install-directory>/bin目录 运行命令(确保你输入了数据库的名称)
./mongo 测试
回答by gaurav.singharoy
If you have installed mongodb through homebrewthen you can simply start mongodb through
如果您已经安装了 mongodb,homebrew那么您只需通过以下方式启动 mongodb
brew services start mongodb
Then access the shell by
然后通过访问shell
mongo
You can shut down your db by
您可以通过以下方式关闭数据库
brew services stop mongodb
You can restart your db by
您可以通过以下方式重新启动数据库
brew services restart mongodb
For more options
更多选择
brew info mongodb
回答by Rix
Updated answer (9/2/2019):
更新答案 (9/2/2019):
Homebrew has removed mongodb formula from its core repository, see this pull request.
Homebrew 已从其核心存储库中删除了 mongodb 公式,请参阅此拉取请求。
The new way to install mongodb using Homebrew is as follows:
使用 Homebrew 安装 mongodb 的新方法如下:
~> brew tap mongodb/brew
~> brew install mongodb-community
After installation you can start the mongodb service by following the caveats:
安装后,您可以按照以下注意事项启动 mongodb 服务:
~> brew info mongodb-community
mongodb/brew/mongodb-community: stable 4.2.0
High-performance, schema-free, document-oriented database
https://www.mongodb.com/
Not installed
From: https://github.com/mongodb/homebrew-brew/blob/master/Formula/mongodb-community.rb
==> Caveats
To have launchd start mongodb/brew/mongodb-community now and restart at login:
brew services start mongodb/brew/mongodb-community
Or, if you don't want/need a background service you can just run:
mongod --config /usr/local/etc/mongod.conf
Deprecated answer (8/27/2019):
已弃用的答案 (8/27/2019):
I assume you are using Homebrew. You can see the additional information that you need using brew info $FORMULA
我假设您正在使用 Homebrew。您可以查看需要使用的附加信息brew info $FORMULA
~> brew info mongo 255
mongodb: stable 2.4.6, devel 2.5.1
http://www.mongodb.org/
/usr/local/Cellar/mongodb/2.4.5-x86_64 (20 files, 287M) *
Built from source
From: https://github.com/mxcl/homebrew/commits/master/Library/Formula/mongodb.rb
==> Caveats
To reload mongodb after an upgrade:
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist
Caveats is what you need to follow after installation.
注意事项是安装后需要遵循的内容。
回答by secretgenes
Problem here is you are trying to open a mongo shell without starting a mongo db which is listening to port 127.0.0.1:27017(deafault for mongo db) thats what the error is all about:
这里的问题是你试图打开一个 mongo shell 而不启动一个正在监听端口 127.0.0.1:27017(mongo db 的默认值)的 mongo db,这就是错误的全部内容:
Error: couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:145 exception: connect failed
The easiest solution is to open the terminal and type
最简单的解决方案是打开终端并输入
$ mongod --dbpath ~/data/db
Note: dbpath here is "Users/user" where data/db directories are created
注意:这里的 dbpath 是“Users/user”,其中创建了 data/db 目录
i.e., you need to create directory dataand sub directory dbin your user folder. For e.g say `
即,您需要在您的用户文件夹中创建目录data和子目录db。例如说`
/Users/johnny/data
/用户/约翰尼/数据
After mongo db is up. Open another terminal in new window and type
mongo db 启动后。在新窗口中打开另一个终端并输入
$ mongo
it will open mongo shell with your mongo db connection opened in another terminal.
它将打开 mongo shell,并在另一个终端中打开您的 mongo db 连接。
回答by karthiks
mongo => mongo-db console
mongodb => mongo-db server
If you're on Mac and looking for a easier way to start/stop your mongo-db server, then MongoDB Preference Paneis something that you should look into. With it, you start/stop your mongo-db instance via UI. Hope it helps!
如果您使用的是 Mac 并且正在寻找一种更简单的方法来启动/停止您的 mongo-db 服务器,那么MongoDB Preference Pane是您应该研究的东西。有了它,您可以通过 UI 启动/停止 mongo-db 实例。希望能帮助到你!
回答by Edwin Ikechukwu Okonkwo
additionally you may want mongo to run on another port, then paste this command on terminal,
此外,您可能希望 mongo 在另一个端口上运行,然后将此命令粘贴到终端上,
mongod --dbpath /data/db/ --port 27018
where 27018is the port we want mongo to run on
其中27018是我们希望 mongo 运行的端口
assumptions
假设
- mongod exists in your bin i.e
/usr/local/bin/for mac ( which would be if you installed with brew), otherwise you'd need to navigate to the path where mongo is installed - the folder
/data/db/exists
- mongod 存在于你的 bin 中,即
/usr/local/bin/对于 mac(如果你用 brew 安装的话),否则你需要导航到安装 mongo 的路径 - 文件夹
/data/db/存在
回答by jasonleonhard
Mac Installation:
Mac 安装:
Install brew
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"Update and verify you are good with
brew update brew doctorInstall mongodb with
brew install mongodbCreate folder for mongo data files:
mkdir -p /data/dbSet permissions
sudo chown -R `id -un` /data/dbOpen another terminal window & run and keep running a mongo server/daemon
mongodReturn to previous terminal and run a mongodb shell to access data
mongo
安装 brew
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"更新并验证您是否擅长
brew update brew doctor安装mongodb
brew install mongodb为 mongo 数据文件创建文件夹:
mkdir -p /data/db设置权限
sudo chown -R `id -un` /data/db打开另一个终端窗口并运行并继续运行 mongo 服务器/守护进程
mongod返回上一个终端并运行 mongodb shell 来访问数据
mongo
To quit each of these later:
稍后退出这些:
The Shell:
quit()The Server
ctrl-c
贝壳:
quit()服务器
ctrl-c
回答by Piyush Bansal
Make sure you are logged in as root user in your terminal.
确保您在终端中以 root 用户身份登录。
Steps to start mongodb server in your mac
在 Mac 中启动 mongodb 服务器的步骤
- Open Terminal
- Run the command
sudo su - Enter your administrator password
- run the command
mongod - MongoDb Server starts
- 打开终端
- 运行命令
sudo su - 输入您的管理员密码
- 运行命令
mongod - MongoDb 服务器启动
Hope it helps you. Thanks
希望对你有帮助。谢谢
回答by drjorgepolanco
Nothing less likely to be outdated that the official docs: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/
没有什么比官方文档更过时的了:https: //docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/

