跳到主要内容

MongoDB

系统:Debian 12

安装必要软件

sudo apt-get install gnupg curl

导入密钥

curl -fsSL https://pgp.mongodb.com/server-7.0.asc | \
sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg \
--dearmor

添加源

如果是 Debian 11 的话,把 bookworm 改成 bullseye

echo "deb [ signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] http://repo.mongodb.org/apt/debian bookworm/mongodb-org/7.0 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list

安装

sudo apt-get update
sudo apt-get install -y mongodb-org

启动 MongoDB

sudo systemctl start mongod
sudo systemctl enable mongod

运行远程访问

/etc/mongod.conf
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0
systemctl restart mongod

新建用户

连接到 MongoDB

mongosh

新建用户:

use admin
db.createUser({
user: "yourUsername",
pwd: "yourPassword",
roles: [
{ role: "readWriteAnyDatabase", db: "admin" }
]
})

验证用户是否存在:

db.getUsers()

参考

https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-debian/