目 录CONTENT

文章目录
DB

mongodb7随记

平凡的运维之路
2025-04-21 / 0 评论 / 0 点赞 / 8 阅读 / 8906 字

mongodb7部署安装随记

  • ./mongosh “mongodb://cdesk:NABJneNhtyBs@gj&Se@10.130.47.197:30000/cdesk”

  • #初始化shard集群

rs.initiate({_id: "shard1",members: [{
_id: 0,
      host: "10.130.47.197:27017",  // 服务器 A:主节点
      priority: 2  // 优先成为主节点
    },
    {
      _id: 1,
      host: "10.130.47.224:27017",  // 服务器 B:从节点
      priority: 1
    },
    {
      _id: 2,
      host: "10.130.47.243:27017",  // 服务器 C:仲裁节点(不存数据)
      arbiterOnly: true
    }
  ]
});


rs.initiate({
  _id: "shard2",
  members: [
    {
      _id: 0,
      host: "10.130.47.197:27018",  // 服务器 A:从节点
      priority: 1
    },
    {
      _id: 1,
      host: "10.130.47.224:27018",  // 服务器 B: 仲裁节点
     arbiterOnly: true
    },
    {
      _id: 2,
      host: "10.130.47.243:27018",  // 服务器 C: 主节点
       priority: 2
    }
  ]
});


rs.initiate({
  _id: "shard3",
  members: [
    {
      _id: 0,
      host: "10.130.47.197:27019",  // 服务器 A:仲裁节点
       arbiterOnly: true
    },
    {
      _id: 1,
      host: "10.130.47.224:27019",  // 服务器 B:主节点
      priority: 2
    },
    {
      _id: 2,
      host: "10.130.47.243:27019",  // 服务器 C: 从节点
       priority: 1
    }
  ]
});
  • #需要依次在分片主节点上运行执行创建密码
db.createUser({user:"root", pwd:"wN8zB9xG4kT1oP3", roles:[{role:"root", db:"admin"}]});

db.createUser({"user"  : "cdesk","pwd"   : "wN8zB9xG4kT1oP3","roles" : [{ role: "readwrite", db: "cdesk" }]}); #基于非admin认证的创建用户方式。

use cdesk
db.test1.insert({"age":1})
db.createUser({
  user: "cdesk",
  pwd: "wN8zB9xG4kT1oP3",
  roles: [{ 
    role: "readWrite",  // 注意大小写
    db: "cdesk"         // 绑定到 cdesk 数据库
  }]
})
db.auth("cdesk","wN8zB9xG4kT1oP3");

添加分片节点

  • #需要确认主节点ip: primary,并添加分片集群
db.runCommand({addshard:"shard1/10.130.47.197:27017", name:"shard1"})
db.runCommand({addshard:"shard2/10.130.47.243:27018", name:"shard2"})
db.runCommand({addshard:"shard3/10.130.47.224:27019", name:"shard3"})
db.runCommand({addshard:"config/10.130.47.224:20000", name:"config"})

配置文件说明

  • shard配置文件
[mongodb@test0011 config]$ cat shard1.conf 
systemLog:
   destination: file
   path: "/home/mongodb/mongodb-7.0.18/log/shard1.log"
   logAppend: true

storage:
   dbPath: /home/mongodb/mongodb-7.0.18/data/shard1
   directoryPerDB: true
   engine: wiredTiger
   wiredTiger:
      engineConfig:
         cacheSizeGB: 2
   oplogMinRetentionHours: 168

processManagement:
   fork: true
   pidFilePath: /tmp/shard11.pid

net:
   bindIp: 10.130.47.197,127.0.0.1
   port: 27017
   maxIncomingConnections: 10000

security:
   keyFile: /home/mongodb/mongodb-7.0.18/config/keyFile.key
   authorization: enabled

setParameter:
   replWriterThreadCount: 32
   initialSyncMethod: fileCopyBased
   enableLocalhostAuthBypass: true

sharding:
   clusterRole: shardsvr

replication:
   replSetName: shard1
   enableMajorityReadConcern: true


[mongodb@test0011 config]$ cat shard2.conf 
systemLog:
   destination: file
   path: "/home/mongodb/mongodb-7.0.18/log/shard2.log"
   logAppend: true

storage:
   dbPath: /home/mongodb/mongodb-7.0.18/data/shard2
   directoryPerDB: true
   engine: wiredTiger
   wiredTiger:
      engineConfig:
         cacheSizeGB: 2
   oplogMinRetentionHours: 168

processManagement:
   fork: true
   pidFilePath: /tmp/shard2.pid

net:
   bindIp: 10.130.47.197,127.0.0.1
   port: 27018
   maxIncomingConnections: 10000

security:
   keyFile: /home/mongodb/mongodb-7.0.18/config/keyFile.key
   authorization: enabled

setParameter:
   replWriterThreadCount: 32
   initialSyncMethod: fileCopyBased
   enableLocalhostAuthBypass: true

sharding:
   clusterRole: shardsvr

replication:
   replSetName: shard2
   enableMajorityReadConcern: true

[mongodb@test0011 config]$ cat shard3.conf 
systemLog:
   destination: file
   path: "/home/mongodb/mongodb-7.0.18/log/shard3.log"
   logAppend: true

storage:
   dbPath: /home/mongodb/mongodb-7.0.18/data/shard3
   directoryPerDB: true
   engine: wiredTiger
   wiredTiger:
      engineConfig:
         cacheSizeGB: 2
   oplogMinRetentionHours: 168

processManagement:
   fork: true
   pidFilePath: /tmp/shard3.pid

net:
   bindIp: 10.130.47.197,127.0.0.1
   port: 27019
   maxIncomingConnections: 10000

security:
   keyFile: /home/mongodb/mongodb-7.0.18/config/keyFile.key
   authorization: enabled

setParameter:
   replWriterThreadCount: 32
   initialSyncMethod: fileCopyBased
   enableLocalhostAuthBypass: true

sharding:
   clusterRole: shardsvr

replication:
   replSetName: shard3
   enableMajorityReadConcern: true

  • config配置文件
[mongodb@test0011 config]$ cat config3.conf 
systemLog:
   destination: file
   path: "/home/mongodb/mongodb-7.0.18/log/config1.log"
   logAppend: true

storage:
   dbPath: /home/mongodb/mongodb-7.0.18/data/config1
   directoryPerDB: true
   engine: wiredTiger
   wiredTiger:
      engineConfig:
         cacheSizeGB: 2
   oplogMinRetentionHours: 168

processManagement:
   fork: true
   pidFilePath: /tmp/config1.pid

net:
   bindIp: 10.130.47.197,127.0.0.1
   port: 20000
   maxIncomingConnections: 10000

security:
   keyFile: /home/mongodb/mongodb-7.0.18/config/keyFile.key
   authorization: enabled

setParameter:
   replWriterThreadCount: 32
   initialSyncMethod: fileCopyBased
   enableLocalhostAuthBypass: true

sharding:
   clusterRole: configsvr

replication:
   replSetName: config
   enableMajorityReadConcern: true

  • mongos配置文件
[mongodb@test0011 config]$ more mongos3.conf 
net:
  port: 30000
  bindIp: 127.0.0.1,10.130.47.197

systemLog:
  destination: file
  path: /home/mongodb/mongodb-7.0.18/log/mongos1.log
  logAppend: true

sharding:
  configDB: config/10.130.47.197:20000,10.130.47.224:20000,10.130.47.243:20000

security:
  keyFile: /home/mongodb/mongodb-7.0.18/config/keyFile.key

processManagement:
  fork: true
  pidFilePath: /tmp/mongos1.pid
0

评论区