文章目录
-
-
-
-
- [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