单机

配置

processManagement:
   fork: true
net:
   bindIp: localhost
   port: 27017
storage:
   dbPath: /Volumes/SAO/data/mongo/27017/db
systemLog:
   destination: file
   path: "/Volumes/SAO/data/mongo/27017/mongod.log"
   logAppend: true
storage:
   journal:
      enabled: true

启动命令

mongod -f /Volumes/SAO/data/mongo/27017/mongod.conf --fork

副本集

配置

replication:
   oplogSizeMB: 64
   replSetName: rs2

添加集群

rs.initiate(
   {
      _id: "rs2",
      version: 1,
      members: [
         { _id: 0, host : "127.0.0.1:27017" },
         { _id: 1, host : "127.0.0.1:27018" },
         { _id: 2, host : "127.0.0.1:27019" }
      ]
   }
)

启动指令

mongod -f /Volumes/SAO/data/mongo/27017/mongod.conf --fork
mongod -f /Volumes/SAO/data/mongo/27018/mongod.conf --fork
mongod -f /Volumes/SAO/data/mongo/27019/mongod.conf --fork

常用指令

https://www.mongodb.com/docs/v5.0/reference/replication/

隐藏副本集成员

cfg = rs.conf()
cfg.members 里面 hidden 设置为 true priority 设置为 0

节点操作

//添加
rs.add( { host: "mongodb3.example.net:27017", priority: 1, votes: 1} )
//删除
rs.remove( { host: "mongodb3.example.net:27017"} )

添加仲裁器

//只投票,不保存数据
rs.addArb("m1.example.net:27017")

允许查询

rs.secondaryOk()

优雅关闭

ps -ef | grep mongo | grep -v grep | awk '{print "kill -15 "$2}'

状态查看

//检查关于操作日志的副本集成员的当前状态
rs.printReplicationInfo()
rs.status()