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