Yum 安装
vi /etc/yum.repos.d/mongodb-org-6.0.repo
填充内容如下:
[mongodb-org-6.0]
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
sudo yum install -y mongodb-org
用这个命令可以指定版本:
sudo yum install -y mongodb-org-6.0.2 mongodb-org-database-6.0.2 mongodb-org-server-6.0.2 mongodb-mongosh-6.0.2 mongodb-org-mongos-6.0.2 mongodb-org-tools-6.0.2
vi /etc/mongod.conf
修改
# where to write logging data.
systemLog:destination: filelogAppend: truepath: /var/log/mongodb/mongod.log# Where and how to store data.
storage:dbPath: /var/lib/mongojournal:enabled: true
# engine:
# wiredTiger:# how the process runs
processManagement:fork: true # fork and run in backgroundpidFilePath: /var/run/mongodb/mongod.pid # location of pidfiletimeZoneInfo: /usr/share/zoneinfo# network interfaces
net:port: 2007bindIp: 0.0.0.0 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
启动:
systemctl start mongod
遇到问题:
journalctl -xe
10月 10 06:07:17 localhost.localdomain systemd[1]: Unit mongod.service entered failed state.
10月 10 06:07:17 localhost.localdomain systemd[1]: mongod.service failed.
10月 10 06:07:17 localhost.localdomain polkitd[787]: Unregistered Authentication Agent for unix-process:32471:1252991 (system bus name :1.227, object path /org/freedesktop/PolicyKit1/AuthenticationAgent,
10月 10 06:07:19 localhost.localdomain polkitd[787]: Registered Authentication Agent for unix-process:32496:1253177 (system bus name :1.228 [/usr/bin/pkttyagent --notify-fd 5 --fallback], object path /org
10月 10 06:07:19 localhost.localdomain systemd[1]: Starting MongoDB Database Server... -- Subject: Unit mongod.service has begun start-up -- Defined-By: systemd -- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel -- -- Unit mongod.service has begun starting up.
10月 10 06:07:19 localhost.localdomain mongod[32511]: about to fork child process, waiting until server is ready for connections.
10月 10 06:07:19 localhost.localdomain mongod[32511]: forked process: 32513
10月 10 06:07:19 localhost.localdomain mongod[32511]: ERROR: child process failed, exited with 48
10月 10 06:07:19 localhost.localdomain mongod[32511]: To see additional information in this output, start without the "--fork" option.
10月 10 06:07:19 localhost.localdomain systemd[1]: mongod.service: control process exited, code=exited status=48
10月 10 06:07:19 localhost.localdomain systemd[1]: Failed to start MongoDB Database Server. -- Subject: Unit mongod.service has failed -- Defined-By: systemd -- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel -- -- Unit mongod.service has failed. -- -- The result is failed.
查看日志 vi /var/log/mongodb/mongod.log, 得到:
{"t": {"$date": "2022-10-10T06:36:19.084-04:00"},"s": "E","c": "CONTROL","id": 20568,"ctx": "initandlisten","msg": "Error setting up listener","attr": {"error": {"code": 9001,"codeName": "SocketException","errmsg": "Permission denied"}}
}
解决方案:
用命令 mongod -f /etc/mongod.conf --nounixsocket --fork 启动
不用 systemctl start
附: nounixsocket: 套接字文件,默认为false ,意为生成socket文件。可在 conf 中配置 当设置为true时,不会生成socket文件。 nounixsocket = false
参考:
Install MongoDB Community Edition on Red Hat or CentOS — MongoDB Manual
https://www.jianshu.com/p/ba63f6c5ad04
https://blog.csdn.net/kakalairen/article/details/119395309