[ Spring ] Install MongoDB on Ubuntu24

文章目录

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

this allow you create a startup service for mongo db

bash 复制代码
sudo nano /etc/systemd/system/disable-thp.service

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

bash 复制代码
[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

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

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

bash 复制代码
mongod soft nproc 64000
mongod hard nproc 64000
mongod soft nofile 64000
mongod hard nofile 64000
Enable Swappiness for MongoDB
bash 复制代码
sudo nano /etc/sysctl.conf

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

bash 复制代码
fs.file-max = 2097152
vm.max_map_count = 262144
vm.swappiness = 1

apply changes

bash 复制代码
sudo sysctl -p
Install MongoDB
bash 复制代码
sudo apt update
sudo apt install gnupg curl

add GPG key and repository for mongo db

bash 复制代码
curl -fsSL https://www.mongodb.org/static/pgp/server-8.0.asc | \
sudo gpg -o /usr/share/keyrings/mongodb-server-8.0.gpg \
--dearmor
bash 复制代码
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

bash 复制代码
sudo apt update
sudo apt install mongodb-org
Enable MongoDB Service
bash 复制代码
sudo systemctl daemon-reload
sudo systemctl enable --now mongod
sudo systemctl status mongod

press Ctrl+Z to exit status details

Create MongoDB Admin User
bash 复制代码
mongosh
bash 复制代码
disableTelemetry()
use admin
db.createUser({
  user: "root",
  pwd: passwordPrompt(),
  roles: [
    {
      role: "userAdminAnyDatabase",
      db: "admin"
    },
    {
      role: "readWriteAnyDatabase",
      db: "admin"
    }
  ]
})
bash 复制代码
quit()
Enable MongoDB Authentication
bash 复制代码
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

bash 复制代码
security:
  authorization: enabled

reload mongo db service to take effect

bash 复制代码
sudo systemctl restart mongod

try a login with password

bash 复制代码
mongosh --port 27017 --authenticationDatabase "admin" -u "root" -p
Create a Normal Database
bash 复制代码
use user

create a new user for this normal db

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

try a login with password

bash 复制代码
mongosh --port 27017 --authenticationDatabase "user" -u "useradmin" -p
Update User Roles
bash 复制代码
mongosh --port 27017 --authenticationDatabase "admin" -u "root" -p
bash 复制代码
use user
bash 复制代码
db.createUser({
  user: "root",
  pwd: passwordPrompt(),
  roles: []
})
bash 复制代码
db.grantRolesToUser("root", [{role:"readWrite", db:"user"}])
Insert Document
bash 复制代码
db.user.insertOne({
    id:"1",
    name:"MongoA"
})
Uninstall MongoDB
bash 复制代码
sudo apt purge mongodb-org
Configure MongoDB Connection in SpringBoot
properties 复制代码
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

相关推荐
行思理3 小时前
MongoDB慢查询临时开启方法讲解
数据库·mongodb
cookqq15 小时前
mongodb源码分析session异步接受asyncSourceMessage()客户端流变Message对象
数据库·sql·mongodb·nosql
月初,18 小时前
MongoDB学习和应用(高效的非关系型数据库)
学习·mongodb·nosql
在云上(oncloudai)4 天前
AWS DocumentDB vs MongoDB:数据库的技术抉择
数据库·mongodb·aws
Despacito0o4 天前
瀚文机械键盘固件开发详解:HWKeyboard.cpp文件解析与应用
数据库·mongodb·计算机外设
运维老曾5 天前
MongoDB-6.0.24 主从复制搭建和扩容缩容详解
数据库·mongodb
清风细雨_林木木5 天前
不同的数据库操作方式:MongoDB(NoSQL)和 MySQL/SQL
数据库·mongodb·nosql
ontheway-xx5 天前
MongoDB账号密码笔记
数据库·mongodb
清风细雨_林木木6 天前
数据库 MongoDB (NoSQL) 与 MySQL (SQL) 的写法对比
数据库·mongodb·nosql
泰勒疯狂展开6 天前
Java研学-MongoDB(一)
java·开发语言·mongodb