文章

MongoDB部署

Ubuntu 20.04 LTS 中安装 mongodb 6.0

Ubuntu 20.04 上添加 MongoDB 存储库

1
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list

集成MongoDB GPG密钥

1
2
3
4
5
6
7
8
9
10
11
2.集成MongoDB GPG密钥

为了确保我们将收到的用于在我们的 Linux 上安装此数据库的软件包来自真实来源。 添加数据库服务器开发人员的 GPG 签名密钥。

curl -sSL https://www.mongodb.org/static/pgp/server-6.0.asc  -o mongoserver.asc

gpg --no-default-keyring --keyring ./mongo_key_temp.gpg --import ./mongoserver.asc

gpg --no-default-keyring --keyring ./mongo_key_temp.gpg --export > ./mongoserver_key.gpg

sudo mv mongoserver_key.gpg /etc/apt/trusted.gpg.d/

安装

1
2
3
4
5
apt install mongodb-org

systemctl enable --now mongod

systemctl status mongod

查看状态

1
2
mongod --version
mongosh 进入命令行

更新

1
apt update && sudo apt upgrade

配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
storage:
  dbPath: /data/mongodb
  journal:
    enabled: true

#  engine: mmapv1
#  mmapv1:
#  wiredTiger:

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0


#processManagement:

security:
  authorization: enabled

登录进入MongoDB

1
2
3
4
5
6
7
bash> mongosh
Current Mongosh Log ID:	63e51d21435b808fe1db5c3a
Connecting to:		mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.6.2
Using MongoDB:		6.0.4
Using Mongosh:		1.6.2

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

创建用户及设置密码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
=======================进入MongoDB==================
test> show dbs

#切换用户:
test> use admin
switched to db admin

# 设置管理员用户名和密码
admin> db.createUser({ user: "admin", pwd: "******", roles: [{ role: "userAdminAnyDatabase", db: "admin" }] })

#验证是否成功:返回1成功
admin> db.auth("admin", "******")
{ ok: 1 }

# 查询用户
admin> db.system.users.find()

创建用户角色

1
db.createRole({ role: "allDBRole", privileges: [ { resource: { db: "", collection: "" }, actions: ["find", "insert", "update", "remove"] }], roles: [] })

授予用户权限

1
db.grantRolesToUser("admin", ["allDBRole"])
本文由作者按照 CC BY 4.0 进行授权