
📣【快捷部署系列】017期信息
| 编号 | 选型 | 版本 | 操作系统 | 部署形式 | 部署模式 | 复检时间 | 
|---|---|---|---|---|---|---|
| 017 | MongoDB | 6.0.14 | Ubuntu 20.04 | apt | 单机 | 2024-04-11 | 
一、快捷部署
            
            
              shell
              
              
            
          
          #!/bin/bash
#################################################################################
# 作者:cxy@toctalk@hwy 2024-04-10                                                 
# 功能:自动部署MongoDB 6.0 社区版
# 说明:修改了默认端口,bindIp,数据及日志存储路径,未配置鉴权。                    
#################################################################################
info(){
  echo -e "\033[34m 【`date '+%Y-%m-%d %H:%M:%S'`】\033[0m" "\033[35m$1\033[0m "
}
#自定义mongodb配置文件
diy_MongoDB_config(){
	
 sudo mkdir -p /cxy/mongodb/data
 sudo mkdir -p /cxy/mongodb/logs
 chmod 777 /cxy/mongodb/data
 chmod 777 /cxy/mongodb/logs
 #备份原始配置文件
 cp /etc/mongod.conf /cxy/mongodb/mongod.conf.bak
 sudo rm -f /etc/mongod.conf
 sudo touch /etc/mongod.conf
 # 创建 MongoDB 配置文件
sudo cat > /etc/mongod.conf <<EOF
# Where and how to store data.
storage:
  dbPath: /cxy/mongodb/data
#  engine:
#  wiredTiger:
# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /cxy/mongodb/logs/mongod.log
# network interfaces
net:
  port: 27123
  bindIp: 0.0.0.0
# how the process runs
processManagement:
  timeZoneInfo: /usr/share/zoneinfo
#security:
EOF
}
# 安装 MongoDB 6.0 社区版
install_MongoDB(){
 
 sudo apt-get install -y gnupg curl
 #导入MongoDB公共GPG密钥
 curl -fsSL https://www.mongodb.org/static/pgp/server-6.0.asc | \
   sudo gpg -o /usr/share/keyrings/mongodb-server-6.0.gpg \
   --dearmor
 # 添加 MongoDB 仓库源
 echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-6.0.gpg ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
 # 更新软件包列表
 sudo apt-get update
 #安装指定版本
 info "开始安装 MongoDB(6.0.14)..."
 sudo apt-get install -y mongodb-org=6.0.14 mongodb-org-database=6.0.14 mongodb-org-server=6.0.14 mongodb-org-mongos=6.0.14 mongodb-org-tools=6.0.14
 # 调整ulimit限制,Ubuntu 22.04 默认好像已经设置了,可自行确认,如没设置,可手动执行
 info "安装完毕,开始处理ulimit设置..."
 #echo "* soft nofile 65536" | sudo tee -a /etc/security/limits.conf
 #echo "* hard nofile 65536" | sudo tee -a /etc/security/limits.conf
 ulimit -n 65535
 #固定当前安装版本(可选操作)
 echo "mongodb-org hold" | sudo dpkg --set-selections
 echo "mongodb-org-database hold" | sudo dpkg --set-selections
 echo "mongodb-org-server hold" | sudo dpkg --set-selections
 echo "mongodb-mongosh hold" | sudo dpkg --set-selections
 echo "mongodb-org-mongos hold" | sudo dpkg --set-selections
 echo "mongodb-org-tools hold" | sudo dpkg --set-selections
 #自定义配置文件
 diy_MongoDB_config
 # 启动 MongoDB 服务
 sudo systemctl start mongod
 # 检查 MongoDB 服务状态
 sudo systemctl status mongod
 info "部署完毕!相关部署信息如下:"
 info "连接IP地址参考:$(hostname -I)、$(curl ifconfig.me/ip)"
 info "端口:27123"
 info "数据存储目录:/cxy/mongodb/data"
 info "日志目录:/cxy/mongodb/logs"
}
install_MongoDB
        使用方法:
            
            
              shell
              
              
            
          
          $ vim install-MongoDB6-Ubuntu20.sh
$ chmod +x install-MongoDB6-Ubuntu20.sh
$ ./install-MongoDB6-Ubuntu20.sh
# 感谢淘客科技提供的实验资源环境
        
验证:
            
            
              shell
              
              
            
          
          #查看服务状态
$ sudo systemctl status mongod
#查看 ulimit配置情况
$ ulimit -a
$ cat /etc/security/limits.conf
#查看自定义的配置信息
$ cat /etc/mongod.conf
        二、入门体验
            
            
              shell
              
              
            
          
          # 连接 MongoDB 数据库
mongosh mongodb://127.0.0.1:27123
# 创建数据库
use mydb
# 创建集合
db.createCollection("demo")
# 插入文档
db.demo.insertOne({ "name": "CXY", "age": 99 })
# 查询文档
db.demo.find()
        
由于是入门系列,所以仅列举了简单的体验场景。
更多信息可访问官网:https://www.mongodb.com/zh-cn/docs/v6.0/tutorial/install-mongodb-on-ubuntu/
当然,您也可以关注我,关注后续相关博文。
往期精彩内容推荐
云原生:5分钟了解一下Kubernetes是什么
【快捷部署】015_Minio(latest)
【快捷部署】014_elasticsearch(7.6)
【快捷部署】011_PostgreSQL(16)
【快捷部署】010_MySQL(5.7.27)
【快捷部署】009_Redis(6.2.14)
「快速部署」第一期清单