运维管理8 分钟阅读
MongoDB 5.0 单机二进制安装
以前基本用不到 MongoDB,所以没学习过,最近正好需要安装一套,研究了一下,本文记录一下安装过程。 安装前卸载系统自带的 MongoDB 版本: bash root@lpc ~ rpm -qa grep mongodb root@lpc ~ ps -ef grep mongodb root 12591 12530 0 13:42 pts/0 00:00:00 gre
2025年9月25日阅读—点赞—收藏—
database
在知识库中专注阅读,并随时返回相关工具与课程
以前基本用不到 MongoDB,所以没学习过,最近正好需要安装一套,研究了一下,本文记录一下安装过程。 安装前卸载系统自带的 MongoDB 版本: bash root@lpc ~ rpm -qa grep mongodb root@lpc ~ ps -ef grep mongodb root 12591 12530 0 13:42 pts/0 00:00:00 gre
本章目标:掌握本章核心知识点 前置要求:完成前序章节学习 预计时长:60 分钟
以前基本用不到 MongoDB,所以没学习过,最近正好需要安装一套,研究了一下,本文记录一下安装过程。
安装前卸载系统自带的 MongoDB 版本:
1[root@lpc ~]# rpm -qa | grep mongodb2[root@lpc ~]# ps -ef | grep mongodb3root 12591 12530 0 13:42 pts/0 00:00:00 grep --color=auto mongodb4[root@lpc ~]# yum erase -y $(rpm -qa | grep mongodb-org)5No packages marked for removal.6Dependencies resolved.7Nothing to do.8Complete!上传 MongoDB 安装包:
1[root@lpc ~]# mkdir /soft2[root@lpc ~]# mv /tmp/mongodb-linux-x86_64-rhel80-5.0.0.tgz /soft/3[root@lpc soft]# cd /soft/4[root@lpc soft]# ls5mongodb-linux-x86_64-rhel80-5.0.0.tgz解压二进制安装包:
1# 解压2[root@lpc soft]# mkdir /data3[root@lpc soft]# tar -xzvf mongodb-linux-x86_64-rhel80-5.0.0.tgz -C /data4mongodb-linux-x86_64-rhel80-5.0.0/LICENSE-Community.txt5mongodb-linux-x86_64-rhel80-5.0.0/MPL-26mongodb-linux-x86_64-rhel80-5.0.0/README7mongodb-linux-x86_64-rhel80-5.0.0/THIRD-PARTY-NOTICES8mongodb-linux-x86_64-rhel80-5.0.0/bin/install_compass9mongodb-linux-x86_64-rhel80-5.0.0/bin/mongo10mongodb-linux-x86_64-rhel80-5.0.0/bin/mongod11mongodb-linux-x86_64-rhel80-5.0.0/bin/mongos重命名目录:
1[root@lpc soft]# cd /data/2[root@lpc data]# mv /data/mongodb-linux-x86_64-rhel80-5.0.0 /data/mongodb-5.0.0配置环境变量:
1[root@lpc data]# cat<<-EOF>>/etc/profile2export MONGO_HOME=/data/mongodb-5.0.03export PATH=/data/mongodb-5.0.0/bin:$PATH4EOF56[root@lpc data]# source /etc/profile在 MongoDB 安装目录中创建数据、日志目录:
1[root@lpc soft]# cd /data/mongodb-5.0.02[root@lpc mongodb-5.0.0]# mkdir data logs3[root@lpc mongodb-5.0.0]# ls4bin data LICENSE-Community.txt logs MPL-2 README THIRD-PARTY-NOTICES编辑 MongoDB 配置信息:
1[root@lpc mongodb-5.0.0]# echo $MONGO_HOME2/data/mongodb-5.0.034[root@lpc mongodb-5.0.0]# cat<<-EOF>mongodb.conf5## 端口6port=37177## 允许远程连接8bind_ip=0.0.0.09## 数据文件存放路径10dbpath=$MONGO_HOME/data11## 日志文件存放路径12logpath=$MONGO_HOME/logs/mongodb.log13## 后台运行14fork=true15EOF配置 MongoDB 为系统服务:
1[root@lpc mongodb-5.0.0]# echo $MONGO_HOME2/data/mongodb-5.0.034[root@lpc mongodb-5.0.0]# cat<<-EOF>/usr/lib/systemd/system/mongodb.service5[Unit]6Description=mongodb-server7After=network.target89[Service]10Type=forking11ExecStart=$MONGO_HOME/bin/mongod --config $MONGO_HOME/mongodb.conf12PrivateTmp=true1314[Install]15WantedBy=multi-user.target16EOF1718[root@lpc mongodb-5.0.0]# chmod 755 /usr/lib/systemd/system/mongodb.service设置开机自启动:
1[root@lpc mongodb-5.0.0]# systemctl start mongodb2[root@lpc mongodb-5.0.0]# systemctl status mongodb3● mongodb.service - mongodb-server4 Loaded: loaded (/usr/lib/systemd/system/mongodb.service; enabled; vendor preset: disabled)5 Active: active (running) since Thu 2025-09-25 13:55:28 CST; 628ms ago6 Process: 12722 ExecStart=/data/mongodb-5.0.0/bin/mongod --config /data/mongodb-5.0.0/mongodb.conf (code=exited, status=0/SUCCESS)7 Main PID: 12724 (mongod)8 Tasks: 34 (limit: 22548)9 Memory: 54.6M10 CGroup: /system.slice/mongodb.service11 └─12724 /data/mongodb-5.0.0/bin/mongod --config /data/mongodb-5.0.0/mongodb.conf1213Sep 25 13:55:27 lpc systemd[1]: Starting mongodb-server...14Sep 25 13:55:27 lpc mongod[12722]: about to fork child process, waiting until server is ready for connections.15Sep 25 13:55:27 lpc mongod[12724]: forked process: 1272416Sep 25 13:55:28 lpc mongod[12722]: child process started successfully, parent exiting17Sep 25 13:55:28 lpc systemd[1]: Started mongodb-server.连接 MongoDB:
1[root@lpc mongodb-5.0.0]# mongo --port 37172MongoDB shell version v5.0.03connecting to: mongodb://127.0.0.1:3717/?compressors=disabled&gssapiServiceName=mongodb4Implicit session: session { "id" : UUID("824697d1-a40a-42af-a6dd-a0a93bb441f7") }5MongoDB server version: 5.0.06================7Warning: the "mongo" shell has been superseded by "mongosh",8which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in9an upcoming release.10We recommend you begin using "mongosh".11For installation instructions, see12https://docs.mongodb.com/mongodb-shell/install/13================14Welcome to the MongoDB shell.15For interactive help, type "help".16For more comprehensive documentation, see17 https://docs.mongodb.com/18Questions? Try the MongoDB Developer Community Forums19 https://community.mongodb.com20---21The server generated these startup warnings when booting: 22 2025-09-25T13:55:27.537+08:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem23 2025-09-25T13:55:28.248+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted24 2025-09-25T13:55:28.248+08:00: You are running this process as the root user, which is not recommended25 2025-09-25T13:55:28.248+08:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'26 2025-09-25T13:55:28.248+08:00: Failed to read /sys/kernel/mm/transparent_hugepage/defrag27 2025-09-25T13:55:28.248+08:00: error: ** WARNING: unrecognized transparent Huge Pages mode of operation in /sys/kernel/mm/transparent_hugepage/defrag: 'defer''28 2025-09-25T13:55:28.248+08:00: Soft rlimits for open file descriptors too low29 2025-09-25T13:55:28.248+08:00: currentValue: 102430 2025-09-25T13:55:28.248+08:00: recommendedMinimum: 6400031---32---33 Enable MongoDB's free cloud-based monitoring service, which will then receive and display34 metrics about your deployment (disk utilization, CPU, operation statistics, etc).3536 The monitoring data will be available on a MongoDB website with a unique URL accessible to you37 and anyone you share the URL with. MongoDB may use this information to make product38 improvements and to suggest MongoDB products and deployment options to you.3940 To enable free monitoring, run the following command: db.enableFreeMonitoring()41 To permanently disable this reminder, run the following command: db.disableFreeMonitoring()42---43> db.version()445.0.0创建一个 root 用户:
1> use admin2switched to db admin3> db.createUser({user:"root", pwd:"**********", roles:[{role:"dbAdminAnyDatabase", db:"admin"}]})4Successfully added user: {5 "user" : "root",6 "roles" : [7 {8 "role" : "dbAdminAnyDatabase",9 "db" : "admin"10 }11 ]12}13> exit14bye至此,MongoDB 安装完成。