March 3, 2022
在m1安装mongodb遇到的一些坑
安装流程
- install home brew
brew tap mongodb/brewbrew install [email protected]brew services list
坑
Two ways to start service
根据mongodb文档, step 3和step 4中间还有一步 brew services start [email protected].
我们首先需要知道mongodb有两部分构成, server和client. 开启server的命令为 mongod (other parameters...), 开启client的命令为mongo.
而官网所说的brew services start [email protected] 其实会直接运行default mongod command. 而默认的server db存储位置为/data/db. / 是mac的read-only space, 我们不可以在这里mkdir, 而他本来就没有/data/db这个folder, 所以我们运行官网给出的brew services start [email protected] 后再运行 brew services list 就会显示 3584 error.
Fail to stop service
当使用brew services start [email protected] 时我们可以用 brew services stop [email protected] 停止server.
当使用 mongod (other parameters..)时我们有两种方法停止server.
- Ctrl+C on the terminal which is running mongod
- open a new terminal
- type
mongoand hit enter use admindb.shutdownServer()
- type
填坑
很简单, 不使用brew services start即可. 我们可以使用sudo mkdir -p /System/Volumes/Data/data/db 在可写区域创建好db储存的文件夹. 然后用sudo mongod --dbpath /System/Volumes/Data/data/db 指定server运行时的数据位置. 这时使用brew services list 则会显示service status started. 之后就可以打开另一个terminal开启client了!
注意: 在做这些事前请确保没有正在运行的mongod(server service).
卸载流程
launchctl list | grep mongoto check the running mongo servicelaunchctl unload ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plistbased on which file exists in~/Library/LaunchAgentsrm -f ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plistlaunchctl remove homebrew.mxcl.mongodbpkill -f mongodKill all the processes related to mongodbrew uninstall mongodbrm -rf /opt/homebrew/var/mongodbData Directorybrew cleanup -s mongodb-communitybrew cleanup --prune-prefix
Resource
