安装部署
RHEL - 在线安装
创建安装源
bash
vi /etc/yum.repos.d/mongodb-org.repo
安装源阿里云镜像内容如下
bash
[mongodb-org]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/6.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-6.0.asc
安装源官方镜像内容如下
bash
[mongodb-org]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/6.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-6.0.asc
安装
bash
sudo yum install -y mongodb-org
RHEL - 手工安装
安装
bash
# 解压缩
tar zxvf mongodb-linux-x86_64-rhel70-4.0.10.tgz -C ../tools
# 新建链接,方便后续升级
ln -s /data/app/tools/mongodb-linux-x86_64-rhel70-4.0.10 /data/app/tools/mongodb
# 创建数据目录和日志目录
cd /data/app/data
mkdir mongodb
cd mongodb
mkdir data
mkdir logs
直接启动
bash
# 直接启动
./mongod --dbpath=/data/app/data/mongodb/data --logpath=/data/app/data/mongodb/logs/mongo.log
后台启动
新建配置文件,内容如下
bash
# 数据目录
dbpath=/data/app/data/mongodb/data
# 日志文件
logpath=/data/app/data/mongodb/logs/mongo.log
# 端口号
port=27017
# 设置为以守护进程的方式运行,即在后台运行
fork = true
启动
bash
./mongod --config=/data/app/tools/mongodb/mongodb.config
自启动
新建配置文件
bash
vi /lib/systemd/system/mongodb.service
内容参考如下
bash
[Unit]
Description=mongodb
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/data/app/tools/mongodb/bin/mongod --config /data/app/tools/mongodb/mongodb.config
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/data/app/tools/mongodb/bin/mongod --shutdown --config /data/app/tools/mongodb/mongodb.config
PrivateTmp=true
[Install]
WantedBy=multi-user.target
执行授权
bash
chmod 754 /lib/systemd/system/mongodb.service
系统服务
bash
# 启动服务
systemctl start mongodb.service
# 关闭服务
systemctl stop mongodb.service
# 开机启动
systemctl enable mongodb.service
# 检查端口确认是否已启动
netstat -lanp | grep 27017
# 查看进程
ps -ef | grep mongodb
MacOS - 在线安装
bash
MacOS - 手工安装
安装
bash
# 准备安装目录
mkdir -p /Users/elvea/Tools/mongodb/server
mkdir -p /Users/elvea/Tools/mongodb/data
mkdir -p /Users/elvea/Tools/mongodb/logs
# 解压缩
tar zxvf mongodb-macos-x86_64-4.4.1.tgz
mv mongodb-macos-x86_64-4.4.1/* /Users/elvea/Tools/mongodb/server
# 删除源代码目录
rm -rf mongodb-macos-x86_64-4.4.1
rm -rf mongodb-macos-x86_64-4.4.1.tgz
# 测试启动
mongod --dbpath ../../data --logpath ../../logs/mongo.log
系统服务
新增配置文件
bash
sudo vim ~/Library/LaunchAgents/mongodb.plist
配置内容如下
bash
<?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>mongodb</string>
<key>ProgramArguments</key>
<array>
<string>/Users/elvea/Tools/mongodb/server/bin/mongod</string>
<string>--dbpath</string>
<string>/Users/elvea/Tools/mongodb/data</string>
<string>--logpath</string>
<string>/Users/elvea/Tools/mongodb/logs/mongo.log</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>WorkingDirectory</key>
<string>/Users/elvea/Tools/mongodb/server/</string>
<key>StandardErrorPath</key>
<string>/Users/elvea/Tools/mongodb/logs/output.log</string>
<key>StandardOutPath</key>
<string>/Users/elvea/Tools/mongodb/logs/output.log</string>
<key>HardResourceLimits</key>
<dict>
<key>NumberOfFiles</key>
<integer>10240</integer>
</dict>
<key>SoftResourceLimits</key>
<dict>
<key>NumberOfFiles</key>
<integer>10240</integer>
</dict>
</dict>
</plist>
服务操作
bash
# 验证配置文件
plutil -lint ~/Library/LaunchAgents/mongodb.plist
# 添加开机启动并立刻启动
launchctl load -w ~/Library/LaunchAgents/mongodb.plist
# 取消开机启动并立刻停止
launchctl unload -w ~/Library/LaunchAgents/mongodb.plist
Windows
bash
# 安装服务
mongod --install --serviceName MongoDB --serviceDisplayName MongoDB --logpath D:\Dev\Database\Mongo\log\MongoDB.Log --dbpath D:\Dev\Database\Mongo\data --directoryperdb
# 启动服务
net start MongoDB
# 停止服务
net stop MongoDB
# 移除服务
mongod --remove --serviceName MongoDB