代码改变世界

Debian 11上安装MongoDB 5

2022-11-18 08:53  abce  阅读(434)  评论(0编辑  收藏  举报
关闭numa和transparent_hugepage
$ sudo vi /etc/default/grub
添加
GRUB_CMDLINE_LINUX_DEFAULT="quiet numa=off transparent_hugepage=never"
$ sudo grub-mkconfig -o /boot/grub/grub.cfg
$ sudo reboot
 
关闭numa和transparent_hugepage的其他方式
$ sudo vi /etc/rc.local
$ sudo more /etc/rc.local
if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
   echo never > /sys/kernel/mm/transparent_hugepage/enabled
fi
$ sudo chmod +x /etc/rc.d/rc.local

或运行时关闭
$ sudo echo never > /sys/kernel/mm/transparent_hugepage/enabled
 
或运行时关闭
$ sudo echo never > /sys/kernel/mm/transparent_hugepage/enabled
 
安装依赖包
sudo apt-get install libcurl4 openssl liblzma5
 
安装MongoDB repository
sudo apt-get install gnupg
wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -
运行成功会返回OK
 
将MongoDB repository添加到系统
##On Debian 11
echo "deb http://repo.mongodb.org/apt/debian bullseye/mongodb-org/5.0 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
##On Debian 10
echo "deb http://repo.mongodb.org/apt/debian buster/mongodb-org/5.0 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
 
更新APT package index
sudo apt install mongodb-org
 
安装MongoDB
sudo apt install mongodb-org
#安装指定的包
#sudo apt install mongodb-org mongodb-org-database mongodb-org-server mongodb-org-shell mongodb-org-mongos mongodb-org-tools
 
#也可以安装指定的版本
#sudo apt-get install -y mongodb-org=<version> mongodb-org-database=<version> mongodb-org-server=<version> mongodb-org-shell=<version> mongodb-org-mongos=<version> mongodb-org-tools=<version>
sudo apt-get install -y mongodb-org=5.0.13 mongodb-org-database=5.0.13 mongodb-org-server=5.0.13 mongodb-org-shell=5.0.13 mongodb-org-mongos=5.0.13 mongodb-org-tools=5.0.13
 
编辑配置文件
有些配置在初始化后就不能修改了,索引建议先配置好,然后启动初始化
sudo vim /etc/mongod.conf
storage:
dbPath: /mongo_data/data
directoryPerDB: true
 
journal:
enabled: true
# engine:
# wiredTiger:
 
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /mongo_data/log/mongod.log
 
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1,192.168.56.2
 
# how the process runs
processManagement:
timeZoneInfo: /usr/share/zoneinfo
 
#security:
# authorization: "enabled"
 
启动并设置开机自动启动
$ sudo systemctl enable --now mongod
Created symlink /etc/systemd/system/multi-user.target.wants/mongod.service → /lib/systemd/system/mongod.service.
 
如果开启了防火墙
$ sudo ufw allow 27017 
 
查看版本
$ mongod --version
 
登陆创建用户
$ sudo mongosh
 
创建超级用户
use admin
db.createUser({user:"admin",pwd:"admin_pw",roles:["root"]})
 
创建admin用户
use admin
db.createUser(
{
user: "mongouser",
pwd: passwordPrompt(), // or cleartext password
roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
}
)
 
创建一个普通用户和数据库
use testdb
db.createUser({user: "testuser", pwd: "test_pw", roles: [{ role: "dbOwner", db: "testdb" }]})
db.auth('testuser','test_pw')
 
创建用户后,重启开启密码验证
$ sudo vim /etc/mongod.conf
$ sudo systemctl restart mongod