根据yaml文件修改

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
apiVersion: v1
kind: ConfigMap
metadata:
name: mongo-config
namespace: public
data:
mongodb.conf: |-
dbpath=/data/mongodb
#logpath=/data/mongodb/mongodb.log
pidfilepath=/data/mongodb/master.pid
directoryperdb=true
logappend=true
bind_ip=0.0.0.0
port=27017

---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mongodb
namespace: public
spec:
replicas: 1
serviceName: mongodb
selector:
matchLabels:
name: mongodb
template:
metadata:
labels:
name: mongodb
spec:
containers:
- name: mongodb
image: mongo:4.2.1
command:
- sh
- -c
- "exec mongod -f /data/mongodb/conf/mongodb.conf"
imagePullPolicy: IfNotPresent
ports:
- containerPort: 27017
name: mongodb
protocol: TCP
volumeMounts:
- name: mongo-config
mountPath: /data/mongodb/conf/
- name: data
mountPath: /data/mongodb/
volumes:
- name: mongo-config
configMap:
name: mongo-config
- name: data
hostPath:
path: /data/mongodb/

---
kind: Service
apiVersion: v1
metadata:
labels:
name: mongodb
name: mongodb
namespace: public
spec:
ports:
- name: mongodb
port: 27017
targetPort: 27017
selector:
name: mongodb

登录mongodb创建库和用户

1
2
3
4
5
6
7
8
9
db.auth('root','1VklxvchenD1x')
show dbs
use test
db.test.insert({name:"John",age:25})

# 创建test用户并授权
test = db.getSiblingDB('test');
test.createUser({user: "test",pwd: "test123",roles: [{ role: "dbOwner", db: "test" }]});