关于mongo在mac下安装和自动开机启动

本文介绍如何在Mac OS X上安装MongoDB数据库,并通过launchd配置MongoDB为启动时自动运行的服务。此外,还介绍了如何调整系统的路径变量以方便使用MongoDB的相关工具。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

$sudo launchctl load /Library/LaunchDaemons/org.mongodb.mongod.plist



curl -O http://downloads.mongodb.org/osx/mongodb-osx-x86_64-1.4.0.tgz
tar xzf mongodb-osx-x86_64-1.4.0.tgz
sudo mv mongodb-osx-x86_64-1.4.0 /usr/local/mongodb
sudo mkdir /usr/local/mongodb_data /var/log/mongodb
sudo chown -R root /usr/local/mongodb


#Save as: /usr/local/mongodb/mongod.conf

# Store data alongside MongoDB instead of the default, /data/db/
dbpath = /usr/local/mongodb_data

# Only accept local connections
bind_ip = 127.0.0.1



Now, we’ll make a launchd job to register the server as an OS X daemon. launchd will start the server at startup, stop it before shutdown, make sure it stays up, and redirect its output to a nice log file.

#Save as: /Library/LaunchDaemons/org.mongodb.mongod.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>org.mongodb.mongod</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/mongodb/bin/mongod</string>
<string>run</string>
<string>--config</string>
<string>/usr/local/mongodb/mongod.conf</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>WorkingDirectory</key>
<string>/usr/local/mongodb</string>
<key>StandardErrorPath</key>
<string>/var/log/mongodb/output.log</string>
<key>StandardOutPath</key>
<string>/var/log/mongodb/output.log</string>
</dict>
</plist>



Now we just need to load the launchd job:


sudo launchctl load /Library/LaunchDaemons/org.mongodb.mongod.plist
And that should do it! Try visiting http://localhost:28017 to see the status console for your database.

One last thing: you should probably add /usr/local/mongodb/bin to your $PATH. That way you can use the other binaries that ship with MongoDB, like the mongo console, mongoexport, and so on.

You can adjust your path the regular way by editing your shell’s profile, or you can use this nice paths.d mechanism that OS X provides:


sudo sh -c 'echo "/usr/local/mongodb/bin" > /etc/paths.d/mongodb'

Thanks to theozaurus in the comments below for that tip.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值