在Ubuntu上配置MongoDB时,有一些技巧可以帮助你确保其高效和安全地运行。以下是一些关键步骤和配置技巧:
安装和配置MongoDB:
wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
sudo apt-get update
sudo apt-get install -y mongodb-org
sudo systemctl start mongod
sudo systemctl status mongod
配置文件调整:
/etc/mongod.conf
:sudo nano /etc/mongod.conf
net:
port: 27017
bindIp: 0.0.0.0
security:
authorization: enabled
sudo systemctl restart mongod
性能优化:
/etc/mongod.conf
文件,调整以下参数:net:
maxIncomingConnections: 2000
storage:
wiredTiger:
engineConfig:
cacheSizeGB: 4
mongostat
和 mongotop
监控数据库性能。安全配置:
sudo nano /etc/systemd/system/disable-thp.service
内容:[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
重新加载并启用服务:sudo systemctl daemon-reload
sudo systemctl enable --now disable-thp.service
sudo nano /etc/security/limits.d/mongodb.conf
内容:mongod soft nproc 64000
mongod hard nproc 64000
mongod soft nofile 64000
mongod hard nofile 64000
mongosh
use admin
db.createUser({
user: "admin",
pwd: "yourAdminPassword",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
})
通过这些步骤和技巧,你可以在Ubuntu上高效、安全地配置和管理MongoDB。