Mongodb总结

警告
本文最后更新于 2022-12-11,文中内容可能已过时。

Mongodb安装

(1)导入公匙

1
wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -

(2)创建mongodb列表文件

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

(3)重新加载包数据

1
sudo apt-get update

(4)重新加载包数据

1
sudo apt-get install -y mongodb-org=5.0.2 mongodb-org-database=5.0.2 mongodb-org-server=5.0.2 mongodb-org-shell=5.0.2 mongodb-org-mongos=5.0.2 mongodb-org-tools=5.0.2

(5)服务启动

1
sudo systemctl start mongod

Mongodb配置修改

(1)打开配置

1
sudo vim /etc/mongod.conf

(2)修改配置

 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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# Where and how to store data.
storage:
  dbPath: /var/lib/mongodb
  journal:
    enabled: true
#  engine:
#  wiredTiger:

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

# network interfaces
net:
  port: 10095
  bindIp: 0.0.0.0


# how the process runs
processManagement:
  timeZoneInfo: /usr/share/zoneinfo
  
# 登录是否需要密码
#security:
#  authorization: enabled
#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options:

Mongodb添加用户

(1)连接Mongodb

1
mongo mongodb://127.0.0.1:10095

(2)创建普通用户

1
2
3
4
5
6
7
8
use Production
db.createUser(
   {
     user: "production",
     pwd: "production@123",  // passwordPrompt() Or  "<cleartext password>"
     roles: [ "readWrite", "dbAdmin" ]
   }
)

(3)创建超级用户

1
2
3
4
5
6
7
8
use admin
db.createUser(
   {
     user: "mongoAdmin",
     pwd: passwordPrompt(),  // passwordPrompt() Or  "<cleartext password>"
     roles: [ "readWriteAnyDatabase", "userAdminAnyDatabase", "dbAdminAnyDatabase"]
   }
)

(4)登录

1
db.auth("production")

Mongodb更新用户权限

(1)连接Mongodb

1
mongo mongodb://127.0.0.1:10095

(2)更新用户权限

1
2
3
4
use Production
# 更新用户权限
db.updateUser("production",{roles : [{"role" : "readWriteAnyDatabase","db" : "Stock"},{"role" : "dbAdminAnyDatabase","db" : "Stock"}]})
readWriteAnyDatabase
0%