当前位置: 首页 > article >正文

[ Spring ] Install MongoDB on Ubuntu24

文章目录

          • Disable THP Service
          • Remove File and Process Count Limitation
          • Enable Swappiness for MongoDB
          • Install MongoDB
          • Enable MongoDB Service
          • Create MongoDB Admin User
          • Enable MongoDB Authentication
          • Create a Normal Database
          • Update User Roles
          • Insert Document
          • Uninstall MongoDB
          • Configure MongoDB Connection in SpringBoot
          • Preference Guidance

Disable THP Service

this allow you create a startup service for mongo db

sudo nano /etc/systemd/system/disable-thp.service

append this script to file end, press Ctrl+X Y Enter to save

[Unit]
Description=Disable Transparent Huge Pages (THP)

[Service]
Type=simple
ExecStart=/bin/sh -c "echo 'never' > /sys/kernel/mm/transparent_hugepage/enabled && echo 'never' > /sys/kernel/mm/transparent_hugepage/defrag"

[Install]
WantedBy=multi-user.target

reload systemd to take effect

sudo systemctl daemon-reload
sudo systemctl enable --now disable-thp.service
Remove File and Process Count Limitation
sudo nano /etc/security/limits.d/mongodb.conf

append this script to file end, press Ctrl+X Y Enter to save

mongod soft nproc 64000
mongod hard nproc 64000
mongod soft nofile 64000
mongod hard nofile 64000
Enable Swappiness for MongoDB
sudo nano /etc/sysctl.conf

append this script to file end, press Ctrl+X Y Enter to save

fs.file-max = 2097152
vm.max_map_count = 262144
vm.swappiness = 1

apply changes

sudo sysctl -p
Install MongoDB
sudo apt update
sudo apt install gnupg curl

add GPG key and repository for mongo db

curl -fsSL https://www.mongodb.org/static/pgp/server-8.0.asc | \
sudo gpg -o /usr/share/keyrings/mongodb-server-8.0.gpg \
--dearmor
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] https://repo.mongodb.org/apt/ubuntu noble/mongodb-org/8.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-8.0.list

install mongo db

sudo apt update
sudo apt install mongodb-org
Enable MongoDB Service
sudo systemctl daemon-reload
sudo systemctl enable --now mongod
sudo systemctl status mongod

press Ctrl+Z to exit status details

Create MongoDB Admin User
mongosh
disableTelemetry()
use admin
db.createUser({
  user: "root",
  pwd: passwordPrompt(),
  roles: [
    {
      role: "userAdminAnyDatabase",
      db: "admin"
    },
    {
      role: "readWriteAnyDatabase",
      db: "admin"
    }
  ]
})
quit()
Enable MongoDB Authentication
sudo nano /etc/mongod.conf

append this script to file end, press Ctrl+X Y Enter to save

when you want to reset admin user, you should disable this option, and restart mongod

security:
  authorization: enabled

reload mongo db service to take effect

sudo systemctl restart mongod

try a login with password

mongosh --port 27017 --authenticationDatabase "admin" -u "root" -p
Create a Normal Database
use user

create a new user for this normal db

db.createUser({
  user: "useradmin",
  pwd: passwordPrompt(),
  roles: [
    {
      role: "readWrite",
      db: "user"
    }
  ]
})
quit()

try a login with password

mongosh --port 27017 --authenticationDatabase "user" -u "useradmin" -p
Update User Roles
mongosh --port 27017 --authenticationDatabase "admin" -u "root" -p
use user
db.createUser({
  user: "root",
  pwd: passwordPrompt(),
  roles: []
})
db.grantRolesToUser("root", [{role:"readWrite", db:"user"}])
Insert Document
db.user.insertOne({
    id:"1",
    name:"MongoA"
})
Uninstall MongoDB
sudo apt purge mongodb-org
Configure MongoDB Connection in SpringBoot
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=user
spring.data.mongodb.username=root
spring.data.mongodb.password=123456789
Preference Guidance

https://www.howtoforge.com/tutorial/install-mongodb-on-ubuntu


http://www.kler.cn/a/500331.html

相关文章:

  • VMware中Ubuntu如何连接网络?安排!
  • 《新闻大厦抢先版》V0.18.105+Dlcs官方学习版
  • C++ Json库的使用
  • 太原理工大学软件设计与体系结构 --javaEE
  • windows及linux 安装 Yarn 4.x 版本
  • 记录一个在增量更新工具类
  • SpringBoot操作spark处理hdfs文件
  • 第432场周赛:跳过交替单元格的之字形遍历、机器人可以获得的最大金币数、图的最大边权的最小值、统计 K 次操作以内得到非递减子数组的数目
  • IDEA中创建maven项目
  • Redis之秒杀活动
  • django基于Python的智能停车管理系统
  • 限制图层列表
  • (2025,Cosmos,世界基础模型 (WFM) 平台,物理 AI,数据处理,分词器,世界基础模型预训练/后训练,3D一致性)
  • 【JVM-1】深入解析JVM:Java虚拟机的核心原理与工作机制
  • 【MySQL学习笔记】MySQL视图View
  • 解决nginx多层代理后应用部署后访问发现css、js、图片等样式加载失败
  • CPU缓存架构详解与Disruptor高性能内存队列实战
  • 《零基础Go语言算法实战》【题目 2-5】函数参数的值传递和引用传递
  • 【python A* pygame 格式化 自定义起点、终点、障碍】
  • C++中的unordered_set,unordered_map,哈希表/散列表以及哈希表的模拟实现
  • SqlServer: An expression services limit has been reached异常处理
  • 如何让QPS提升20倍
  • 【学习路线】Python爬虫 详细知识点学习路径(附学习资源)
  • [程序设计]—代理模式