文章目录
- 步骤 1:配置 MongoDB 官方源
- 步骤 2:安装 MongoDB
- 步骤 3:启动 MongoDB 服务
- 步骤 4:验证安装
- 步骤 5:可选配置
- 注意事项
以下是在 CentOS 系统上直接安装 MongoDB 4.0.25 的详细步骤:
步骤 1:配置 MongoDB 官方源
-
使用文本编辑器创建
mongodb-org-4.0.repo
文件:sudo vi /etc/yum.repos.d/mongodb-org-4.0.repo
-
添加以下内容:
[mongodb-org-4.0] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc
-
保存并退出文件。
步骤 2:安装 MongoDB
-
安装指定版本的 MongoDB:
sudo yum install -y mongodb-org-4.0.25
[root@localhost ~]# sudo yum install -y mongodb-org-4.0.25 已加载插件:fastestmirror, langpacks base | 3.6 kB 00:00:00 centos-sclo-rh | 3.0 kB 00:00:00 centos-sclo-sclo | 3.0 kB 00:00:00 docker-ce-stable | 3.5 kB 00:00:00 extras | 2.9 kB 00:00:00 mongodb-org-4.0 | 2.5 kB 00:00:00 Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=updates&infra=stock error was 14: curl#6 - "Could not resolve host: mirrorlist.centos.org; 未知的错误" updates | 2.9 kB 00:00:00 (1/3): docker-ce-stable/7/x86_64/primary_db | 152 kB 00:00:00 (2/3): mongodb-org-4.0/7/primary_db | 118 kB 00:00:00 (3/3): updates/7/x86_64/primary_db | 27 MB 00:00:13 Determining fastest mirrors * base: mirrors.nju.edu.cn * centos-sclo-rh: mirrors.nju.edu.cn * centos-sclo-sclo: mirrors.nju.edu.cn * extras: mirrors.nju.edu.cn * updates: mirrors.nju.edu.cn 正在解决依赖关系 --> 正在检查事务 ---> 软件包 mongodb-org.x86_64.0.4.0.25-1.el7 将被 安装 --> 正在处理依赖关系 mongodb-org-mongos = 4.0.25,它被软件包 mongodb-org-4.0.25-1.el7.x86_64 需要 --> 正在处理依赖关系 mongodb-org-shell = 4.0.25,它被软件包 mongodb-org-4.0.25-1.el7.x86_64 需要 --> 正在处理依赖关系 mongodb-org-tools = 4.0.25,它被软件包 mongodb-org-4.0.25-1.el7.x86_64 需要 --> 正在处理依赖关系 mongodb-org-server = 4.0.25,它被软件包 mongodb-org-4.0.25-1.el7.x86_64 需要 --> 正在检查事务 ---> 软件包 mongodb-org-mongos.x86_64.0.4.0.25-1.el7 将被 安装 ---> 软件包 mongodb-org-server.x86_64.0.4.0.25-1.el7 将被 安装 ---> 软件包 mongodb-org-shell.x86_64.0.4.0.25-1.el7 将被 安装 ---> 软件包 mongodb-org-tools.x86_64.0.4.0.25-1.el7 将被 安装 --> 解决依赖关系完成 依赖关系解决 ================================================================================================================================== Package 架构 版本 源 大小 ================================================================================================================================== 正在安装: mongodb-org x86_64 4.0.25-1.el7 mongodb-org-4.0 6.2 k 为依赖而安装: mongodb-org-mongos x86_64 4.0.25-1.el7 mongodb-org-4.0 9.6 M mongodb-org-server x86_64 4.0.25-1.el7 mongodb-org-4.0 17 M mongodb-org-shell x86_64 4.0.25-1.el7 mongodb-org-4.0 10 M mongodb-org-tools x86_64 4.0.25-1.el7 mongodb-org-4.0 41 M 事务概要 ================================================================================================================================== 安装 1 软件包 (+4 依赖软件包) 总下载量:78 M 安装大小:259 M Downloading packages: 警告:/var/cache/yum/x86_64/7/mongodb-org-4.0/packages/mongodb-org-4.0.25-1.el7.x86_64.rpm: 头V3 RSA/SHA1 Signature, 密钥 ID e52529d4: NOKEY mongodb-org-4.0.25-1.el7.x86_64.rpm 的公钥尚未安装 (1/5): mongodb-org-4.0.25-1.el7.x86_64.rpm | 6.2 kB 00:00:00 (2/5): mongodb-org-mongos-4.0.25-1.el7.x86_64.rpm | 9.6 MB 00:00:10 (3/5): mongodb-org-server-4.0.25-1.el7.x86_64.rpm | 17 MB 00:00:15 (4/5): mongodb-org-shell-4.0.25-1.el7.x86_64.rpm | 10 MB 00:00:10 (5/5): mongodb-org-tools-4.0.25-1.el7.x86_64.rpm | 41 MB 00:00:23 ---------------------------------------------------------------------------------------------------------------------------------- 总计 2.0 MB/s | 78 MB 00:00:39 从 https://www.mongodb.org/static/pgp/server-4.0.asc 检索密钥 导入 GPG key 0xE52529D4: 用户ID : "MongoDB 4.0 Release Signing Key <packaging@mongodb.com>" 指纹 : 9da3 1620 334b d75d 9dcb 49f3 6881 8c72 e525 29d4 来自 : https://www.mongodb.org/static/pgp/server-4.0.asc Running transaction check Running transaction test Transaction test succeeded Running transaction 正在安装 : mongodb-org-shell-4.0.25-1.el7.x86_64 1/5 正在安装 : mongodb-org-mongos-4.0.25-1.el7.x86_64 2/5 正在安装 : mongodb-org-tools-4.0.25-1.el7.x86_64 3/5 正在安装 : mongodb-org-server-4.0.25-1.el7.x86_64 4/5 Created symlink from /etc/systemd/system/multi-user.target.wants/mongod.service to /usr/lib/systemd/system/mongod.service. 正在安装 : mongodb-org-4.0.25-1.el7.x86_64 5/5 验证中 : mongodb-org-4.0.25-1.el7.x86_64 1/5 验证中 : mongodb-org-server-4.0.25-1.el7.x86_64 2/5 验证中 : mongodb-org-tools-4.0.25-1.el7.x86_64 3/5 验证中 : mongodb-org-mongos-4.0.25-1.el7.x86_64 4/5 验证中 : mongodb-org-shell-4.0.25-1.el7.x86_64 5/5 已安装: mongodb-org.x86_64 0:4.0.25-1.el7 作为依赖被安装: mongodb-org-mongos.x86_64 0:4.0.25-1.el7 mongodb-org-server.x86_64 0:4.0.25-1.el7 mongodb-org-shell.x86_64 0:4.0.25-1.el7 mongodb-org-tools.x86_64 0:4.0.25-1.el7 完毕!
-
确保所有组件(
mongodb-org-server
、mongodb-org-shell
等)版本一致:sudo yum install -y mongodb-org-server-4.0.25 mongodb-org-shell-4.0.25 mongodb-org-mongos-4.0.25 mongodb-org-tools-4.0.25
[root@localhost ~]# sudo yum install -y mongodb-org-server-4.0.25 mongodb-org-shell-4.0.25 mongodb-org-mongos-4.0.25 mongodb-org-tools-4.0.25 已加载插件:fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: mirrors.nju.edu.cn * centos-sclo-rh: mirrors.nju.edu.cn * centos-sclo-sclo: mirrors.nju.edu.cn * extras: mirrors.nju.edu.cn * updates: mirrors.nju.edu.cn 软件包 mongodb-org-server-4.0.25-1.el7.x86_64 已安装并且是最新版本 软件包 mongodb-org-shell-4.0.25-1.el7.x86_64 已安装并且是最新版本 软件包 mongodb-org-mongos-4.0.25-1.el7.x86_64 已安装并且是最新版本 软件包 mongodb-org-tools-4.0.25-1.el7.x86_64 已安装并且是最新版本 无须任何处理
步骤 3:启动 MongoDB 服务
-
启动 MongoDB:
sudo systemctl start mongod
-
设置开机自动启动:
sudo systemctl enable mongod
-
检查 MongoDB 服务状态:
sudo systemctl status mongod
[root@localhost ~]# sudo systemctl status mongod ● mongod.service - MongoDB Database Server Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled) Active: active (running) since 三 2024-11-20 16:05:27 CST; 29s ago Docs: https://docs.mongodb.org/manual Process: 85819 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=0/SUCCESS) Process: 85816 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS) Process: 85813 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS) Process: 85809 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS) Main PID: 85824 (mongod) Memory: 43.7M CGroup: /system.slice/mongod.service └─85824 /usr/bin/mongod -f /etc/mongod.conf 11月 20 16:05:27 localhost.localdomain systemd[1]: Starting MongoDB Database Server... 11月 20 16:05:27 localhost.localdomain mongod[85819]: about to fork child process, waiting until server is ready for connections. 11月 20 16:05:27 localhost.localdomain mongod[85819]: forked process: 85824 11月 20 16:05:27 localhost.localdomain mongod[85819]: child process started successfully, parent exiting 11月 20 16:05:27 localhost.localdomain systemd[1]: Started MongoDB Database Server.
步骤 4:验证安装
-
使用 MongoDB 客户端连接:
mongo
[root@localhost ~]# mongo MongoDB shell version v4.0.25 connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb Implicit session: session { "id" : UUID("47475de1-f2ee-4899-930f-4af70514c21a") } MongoDB server version: 4.0.25 Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see http://docs.mongodb.org/ Questions? Try the support group http://groups.google.com/group/mongodb-user Server has startup warnings: 2024-11-20T16:05:27.656+0800 I CONTROL [initandlisten] 2024-11-20T16:05:27.656+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database. 2024-11-20T16:05:27.656+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted. 2024-11-20T16:05:27.656+0800 I CONTROL [initandlisten] 2024-11-20T16:05:27.657+0800 I CONTROL [initandlisten] 2024-11-20T16:05:27.657+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. 2024-11-20T16:05:27.657+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2024-11-20T16:05:27.657+0800 I CONTROL [initandlisten] 2024-11-20T16:05:27.657+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. 2024-11-20T16:05:27.657+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2024-11-20T16:05:27.657+0800 I CONTROL [initandlisten] --- Enable MongoDB's free cloud-based monitoring service, which will then receive and display metrics about your deployment (disk utilization, CPU, operation statistics, etc). The monitoring data will be available on a MongoDB website with a unique URL accessible to you and anyone you share the URL with. MongoDB may use this information to make product improvements and to suggest MongoDB products and deployment options to you. To enable free monitoring, run the following command: db.enableFreeMonitoring() To permanently disable this reminder, run the following command: db.disableFreeMonitoring() --- >
-
检查版本:
db.version()
> db.version() 4.0.25
步骤 5:可选配置
-
默认配置文件路径:
/etc/mongod.conf
[root@localhost ~]# cd /etc/ [root@localhost etc]# cat mongod.conf # mongod.conf # for documentation of all options, see: # http://docs.mongodb.org/manual/reference/configuration-options/ # where to write logging data. systemLog: destination: file logAppend: true path: /var/log/mongodb/mongod.log # Where and how to store data. storage: dbPath: /var/lib/mongo journal: enabled: true # engine: # mmapv1: # wiredTiger: # how the process runs processManagement: fork: true # fork and run in background pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile timeZoneInfo: /usr/share/zoneinfo # network interfaces net: port: 27017 bindIp: 127.0.0.1 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting. #security: #operationProfiling: #replication: #sharding: ## Enterprise-Only Options #auditLog: #snmp:
-
编辑配置文件(如修改监听端口或数据存储路径):
sudo vi /etc/mongod.conf
- 修改数据目录:
storage: dbPath: /var/lib/mongo
- 修改监听端口:
net: port: 27017 bindIp: 0.0.0.0
- 修改数据目录:
-
保存后重启服务:
sudo systemctl restart mongod
注意事项
- 默认数据目录:
/var/lib/mongo
- 默认日志目录:
/var/log/mongodb
- 默认监听端口:
27017
如需更改这些路径,请在 /etc/mongod.conf
中编辑并保存后重启服务。
这样就可以在 CentOS 上安装 MongoDB 4.0.25 并运行!