MongoDB establishes a stand-alone cluster and establishes a root user

1. Generate keyfile file

openssl rand -base64 745 > /home/data/key/key

chmod 600 /home/data/key/key

# 该key的权限必须是600

 

2. Create a conf file

systemLog:
  destination: file
  logAppend: true
  path: /home/data/log/rs0.log #日志目录
 
storage:
  dbPath: /home/data/rs0 #存储路径
  journal:
    enabled: true
  wiredTiger:
    engineConfig:
      cacheSizeGB: 0.2 #最高使用内存数
 
processManagement:
  fork: true       #使用后台线程运行

net:
  port: 27017
  bindIp: 0.0.0.0  

security:
  keyFile: /home/data/key/key #密钥文件路径
  authorization: disabled #是否开启权限验证
 
replication:
  oplogSizeMB:  256  #同步日志大小
  replSetName:  rs0  #集群名称

3. Start mongodb with the mongod --config /home/data/conf/rs0.yml command

4. Use the mongo command to enter the console, execute the command, and complete the cluster construction and user configuration

config = {
    _id:"rs0",
    members:[
        {_id:0,host:"xxx.xxx.xxx.xxx:27017"}
    ]  
};

rs.initiate(config);

use admin;

db.createUser({user:"root",pwd:"1qaz2WSX3edc", roles: [ { role: "root", db: "admin" } ]}); #创建最高权限root用户

 4. Kill the mongod process, modify the configuration file security.authorization: enabled, and start mongod again

complete

 

 

{{o.name}}
{{m.name}}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324135095&siteId=291194637