MongoDB单机版安装

MongoDB单机版安装

在CentOS Linux release 7.9.2009 (Core)下安装MongoDB的步骤如下:

1 创建用户和组(可选,根据需要)

  • 如果您希望以非root用户运行MongoDB服务,可以创建一个专用的用户和组。
shell 复制代码
groupadd mongodb
useradd -r -g mongodb mongodb

2 解压MongoDB压缩包

将下载的MongoDB压缩包解压到指定位置。

shell 复制代码
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-4.4.9.tgz
tar -zxvf mongodb-linux-x86_64-rhel62-4.4.9.tgz -C /opt/
cd /opt/
mv mongodb-linux-x86_64-rhel62-4.4.9 mongodb

3 创建目录

创建MongoDB所需的数据、日志和配置文件所在的目录,并设置正确的权限。

shell 复制代码
mkdir -p /opt/mongodb/{data,logs}
touch /opt/mongodb/mongod.pid
chown -R mongodb:mongodb /opt/mongodb/

4 添加环境变量(可选)

编辑~/.bashrc或者/etc/profile文件,将MongoDB的bin目录添加到PATH中。

shell 复制代码
echo 'export PATH=$PATH:/opt/mongodb/bin' >> ~/.bashrc
source ~/.bashrc

5 创建配置文件

MongoDB 部署,配置文件更加清晰,易于理解和修改。

使用文本编辑器创建一个MongoDB配置文件,比如mongod.conf,并添加以下内容:

yaml 复制代码
cat << \EOF > /opt/mongodb/mongod.conf
systemLog:
  destination: file
  logAppend: true
  path: /opt/mongodb/logs/mongod.log
storage:
  dbPath: /opt/mongodb/data
  journal:
    enabled: true
processManagement:
  fork: true
  pidFilePath: /opt/mongodb/mongod.pid
net:
  port: 27017
  bindIp: 0.0.0.0
EOF

7 配置系统自启(可选)

为了让 MongoDB 服务在系统重启后自动启动,可以创建一个 systemd 服务单元文件。

创建一个新的文件 /etc/systemd/system/mongodb.service

ini 复制代码
cat << \EOF > /etc/systemd/system/mongodb.service
[Unit]
Description=MongoDB Database Server
Documentation=https://docs.mongodb.org/manual
After=network.target

[Service]
User=mongodb
Group=mongodb
ExecStart=/opt/mongodb/bin/mongod --config /opt/mongodb/mongod.conf
ExecReload=/bin/kill -HUP $MAINPID
ExecStop=/bin/kill --signal SIGINT $MAINPID
PIDFile=/opt/mongodb/mongod.pid
TimeoutSec=60
Restart=on-failure
LimitNOFILE=64000
LimitNPROC=64000

# 如果您希望在容器或虚拟化环境中使用,请取消注释下面的行。
# TasksMax=infinity

[Install]
WantedBy=multi-user.target
EOF

创建 systemd 配置文件后,重新加载 systemd 配置并启动 MongoDB 服务:

shell 复制代码
systemctl daemon-reload
systemctl start mongodb

如果希望 MongoDB 开机自启:

shell 复制代码
systemctl enable mongodb

8 防火墙配置(如需要)

这里提前开启端口,为了方便后续的测试验证及使用

shell 复制代码
firewall-cmd --zone=public --add-port=27017/tcp --permanent
firewall-cmd --reload

或者直接禁用、关闭防火墙:

shell 复制代码
systemctl disable firewalld
systemctl stop firewalld

9. 检查 MongoDB 是否运行正常

你可以通过以下命令检查 MongoDB 服务是否成功启动:

shell 复制代码
ps aux | grep mongod

或者尝试连接 MongoDB:

shell 复制代码
/opt/mongodb/bin/mongo

如果连接成功,表示 MongoDB 已经正常运行。

引用Reference

相关推荐
jllllyuz15 分钟前
如何为服务器生成TLS证书
运维·服务器·数据库
伍六星1 小时前
Flask和Django,你怎么选?
数据库·django·flask
杜哥无敌1 小时前
ORACLE 修改端口号之后无法启动?
数据库·oracle
远方16091 小时前
0x-4-Oracle 23 ai-sqlcl 25.1.1 独立安装-配置和优化
数据库·ci/cd·oracle
远方16092 小时前
0x-3-Oracle 23 ai-sqlcl 25.1 集成安装-配置和优化
数据库·ide·ai·oracle
喵叔哟3 小时前
第1章:Neo4j简介与图数据库基础
数据库·oracle·neo4j
喵叔哟3 小时前
第6章:Neo4j数据导入与导出
数据库·oracle·neo4j
甜甜的资料库3 小时前
基于微信小程序的睡眠宝系统源码数据库文档
数据库·微信小程序·小程序
代码老y3 小时前
Redis:现代应用开发的高效内存数据存储利器
数据库·redis·缓存
远方16094 小时前
15-Oracle 23ai Vector Search Similarity Search-向量相似性和混合搜索-实操
数据库·ai·oracle