1. 创建文件
vim /etc/yum.repos.d/mongodb-org-4.4.repo
[mongodb-org-4.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.4.asc
2. 安装
yum install -y mongodb-org
默认数据位置
/var/lib/mongo
默认日志位置
/var/log/mongodb
配置文件位置
vim /etc/mongod.conf
修改监听ip和端口
net:
port: 3717
bindIp: 0.0.0.0
3. 开放3717端口
3.1 SElinux
semanage port -a -t mongod_port_t -p tcp 3717
3.2 防火墙
firewall-cmd --zone=public --add-port=3717/tcp --permanent
firewall-cmd --reload
4. 开启服务 mongod
systemctl start mongod # 开启
systemctl stop mongod # 关闭
systemctl enable mongod # 开机启动
若启动失败
● mongod.service - MongoDB Database Server
Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since 二 2021-08-17 15:13:45 CST; 10s ago
Docs: https://docs.mongodb.org/manual
Process: 7562 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=1/FAILURE)
Process: 7559 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS)
Process: 7555 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS)
Process: 7553 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS)
8月 17 15:13:45 zngw systemd[1]: Starting MongoDB Database Server...
8月 17 15:13:45 zngw mongod[7562]: about to fork child process, waiting until server is ready for connections.
8月 17 15:13:45 zngw mongod[7562]: forked process: 7565
8月 17 15:13:45 zngw mongod[7562]: ERROR: child process failed, exited with 1
8月 17 15:13:45 zngw mongod[7562]: To see additional information in this output, start without the "--fork" option.
8月 17 15:13:45 zngw systemd[1]: mongod.service: control process exited, code=exited status=1
8月 17 15:13:45 zngw systemd[1]: Failed to start MongoDB Database Server.
8月 17 15:13:45 zngw systemd[1]: Unit mongod.service entered failed state.
8月 17 15:13:45 zngw systemd[1]: mongod.service failed.
很大的可能是权限问题,因为在/usr/lib/systemd/system/mongod.service
默认配置中,我们看到是以mongod用户组和用户来启动的。mongod对数据文件和日志文件没有访问权限。添加一下再重试就可以了
chown -Rc mongod. /var/log/mongodb
chown -Rc mongod. /var/lib/mongo
5. 启动客户端 mongo
mongo --host 127.0.0.1:3717
创建root用户
mongo --host 127.0.0.1:3717 admin --eval "db.createUser({ user: 'root', pwd: '123456', roles: [ { role: 'root', db: 'admin' } , 'readWriteAnyDatabase'] })"
6. 卸载
6.1 删除安装包
sudo yum erase $(rpm -qa | grep mongodb-org)
6.2 删除数据文件
rm -r /var/log/mongodb
rm -r /var/lib/mongo
7. 内存优化
MongoDB 3.2 及以后,默认使用 WiredTiger 存储引擎,可通过 cacheSizeGB 选项配置 WiredTiger 引擎使用内存的上限,默认值是 Max( (RAM – 1GB) * 0.6, 1GB)(在 GitHub 上源码)
size_t cacheSizeGB = wiredTigerGlobalOptions.cacheSizeGB;
if (cacheSizeGB == 0) {
// Since the user didn't provide a cache size, choose a reasonable default value.
// We want to reserve 1GB for the system and binaries, but it's not bad to
// leave a fair amount left over for pagecache since that's compressed storage.
ProcessInfo pi;
double memSizeMB = pi.getMemSizeMB();
if (memSizeMB > 0) {
double cacheMB = (memSizeMB - 1024) * 0.6;
cacheSizeGB = static_cast<size_t>(cacheMB / 1024);
if (cacheSizeGB < 1)
cacheSizeGB = 1;
}
}
可以在配置文件中修改/etc/mongod.conf
storage:
...
wiredTiger:
engineConfig:
cacheSizeGB: 1
目前有4个可配置的参数来支持 wiredtiger 存储引擎的 eviction 策略(一般不需要修改),其含义是:
参数 | 默认值 | 含义 |
---|---|---|
eviction_target | 80 | 当 cache used 超过 eviction_target,后台evict线程开始淘汰 CLEAN PAGE |
eviction_trigger | 95 当 cache used 超过 eviction_trigger,用户线程也开始淘汰 CLEAN PAGE | |
eviction_dirty_target | 5 | 当 cache dirty 超过 eviction_dirty_target,后台evict线程开始淘汰 DIRTY PAGE |
eviction_dirty_trigger | 20 | 当 cache dirty 超过 eviction_dirty_trigger, 用户线程也开始淘汰 DIRTY PAGE |
在这个规则下,一个正常运行的 MongoDB 实例,cache used 一般会在 0.8 * cacheSizeGB 及以下,偶尔超出问题不大;如果出现 used>=95% 或者 dirty>=20%,并一直持续,说明内存淘汰压力很大,用户的请求线程会阻塞参与page淘汰,请求延时就会增加,这时可以考虑「扩大内存」或者 「换更快的磁盘提升IO能力」。