dockerはmongodb 4を構築する.xセットをコピーしspring boot接続を使用する

3259 ワード

192.168.98.9:27008
192.168.98.9:27007
     
mkdir /mongo
cd mongo 
mkdir m1 m2 
cd m1 
mkdir log conf db 
cd m2 
mkdir log conf db 
chmod 777 log
log           mongodb       
cd /m1/conf 
      
vim mongod.conf    

 
storage:
  dbPath: /data/db
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:
systemLog:
  destination: file
  logAppend: true
  path: /data/log/mlogs
# where to write logging data.
# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0


# how the process runs
security:
  #    
  authorization: enabled
  #   keyfile  
  clusterAuthMode: keyFile 
  keyFile: /data/conf/mongodb.keyfile
  
#operationProfiling:
replication:
  oplogSizeMB: 10240
#     
  replSetName: rs1
            
cp mongod.conf /mongo/m2/conf/


openssl rand -base64 736 > /mongo/m1/conf/mongodb.keyfile
keyfile                    
chmod 600 mongodb.keyfile
docker         
chown 999 mongodb.keyfile
cp mongodb.keyfile /mongo/m2/conf/
           

docker run -d -p 27007:27017 --name="m1"   -v /mongo/m1:/data -d mongo:4.2.6 --config /data/conf/mongod.conf  

docker run -d -p 27008:27017 --name="m2"   -v /mongo/m2:/data -d mongo:4.2.6 --config /data/conf/mongod.conf  
    
docker exec -it m1 bash
  mongo
mongo

rs1               
myconf = {"_id":"rs1","members":[{"_id":0,"host":"192.168.98.9:27007"},{"_id":1,"host":"192.168.98.9:27008"}]}  
rs.initiate(myconf)   //       
      
rs.status()
            
rs.slaveOk()
 
 
 
     admin       admin           
 use admin 
 db.createUser( {user: "admin",pwd: "123456",roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]})
 db.auth("admin", "123456")
   admin         
    root  
 db.createUser({user:"root",pwd:"123456",roles:["root"]})
     
 db.createUser(  {    user: "test",    pwd: "test",    roles: [ { role: "dbOwner", db: "check" } ]  })

2つのクライアントのログインユーザが一致しないことによる統合ログインであればよい
"operationTime" : Timestamp(1591156470, 1),
	"ok" : 0,
	"errmsg" : "not authorized on admin to execute command { replSetGetStatus: 1.0, lsid: { id: UUID(\"af04026d-c9e9-4cae-a6d3-933f89ce5e28\") }, $clusterTime: { clusterTime: Timestamp(1591156460, 1), signature: { hash: BinData(0, E58BFCE34514E495F96F60E0AFF6F563293BE0B3), keyId: 6833964099525672961 } }, $db: \"admin\" }",
	"code" : 13,
	"codeName" : "Unauthorized",
	"$clusterTime" : {
		"clusterTime" : Timestamp(1591156470, 1),
		"signature" : {
			"hash" : BinData(0,"n+dTy2gBDNlXcY5Gi2qwIZYgjfE="),
			"keyId" : NumberLong("6833964099525672961")
       
      
      、
      
      
      
        spring boot  mongodb   
        
        
uri: mongodb://check:[email protected]:27008,192.168.98.9:27007/check?replSet=rs1