mangodb 之 初出茅庐

mongodb 是使用c++编写,面向文档的非关系型数据库。数据以bson(类似json)的格式存储。

mongodb中包含的主要术语:

  1. 库(database): 类似MySQL中的库
  2. 集合(collection):类似MySQL中的表,集合没有数据结构,可以存放任意类型的文档数据
  3. 文档(document):类似MySQL中的row,是一条条bson格式的数据记录
  4. 域(field): 类似MySQL 中的列column
  5. 索引(index): 类似MySQL 中的索引
  6. 主键(_id): mongodb 自动生成主键 _id

快速开始

测试版本 5.0 2024-04-07 20:30

download

wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-5.0.13.tgz
tar xf mongodb-linux-x86_64-rhel70-5.0.13.tgz 
cd mongodb-linux-x86_64-rhel70-5.0.13/bin

mkdir ../{logs,data}
./mongod --port=27017 --logpath=../logs/mongod.log --dbpath=../data --fork
bin]# ./mongo
> show databases;
admin   0.000GB
config  0.000GB
local   0.000GB
> show dbs;
admin   0.000GB
config  0.000GB
local   0.000GB

CRUD

库操作

  • 创建库

    # 创建并切换到 newdatabase,数据库名不能包含 " " 、 特殊字符,最多64个字符,建议全部小写
    > use newdatabase;
    switched to db newdatabas
    
  • 删除库

    # 删除当前数据库,这个操作更像一个清空数据的动作
    > db.dropDatabase()
    { "ok" : 1 }
    
  • 修改库

  • 查询库

    # 查看数据库 
    # show dbs;
    # show databases;
    # 此时我们没有看到 newdatabase ,因为在mongo中默认显示有集合的db
    > show databases;
    admin   0.000GB  # 存放用户和权限信息
    config  0.000GB  # 在复制集和分片中 local 库中的数据不会复制到其他mongod 实例
    local   0.000GB  # 分片集群保存分片相关信息
    
    # 查看当前所在的数据库
    > db
    newdatabase
    
    # 此时在查看databases 发现已经可以显示了
    > db.newtable.insert({name: "wangendao"})
    WriteResult({ "nInserted" : 1 })
    > show dbs;
    admin        0.000GB
    config       0.000GB
    local        0.000GB
    newdatabase  0.000GB
    

集合操作

  • 创建集合

    # 创建集合
    db.createCollection('userinfo')
    
    db.userinfo.insert({name:'user01',tel: 17727842169, level: 'vip3',skills:["python","go","redis","mongo"]})
    
  • 删除集合

    # 删除集合
    > db.userinfo.drop()
    true
    
  • 修改集合

  • 查看集合

    # 查看集合
    show collections
    show tables
    

文档操作

文档操作增、删、改、查

  • 新增文档

    // 切换或创建数据库
    use newdatabase
    
    • insert 在3.6 版本中提示已经废弃

      插入单条数据

      db.userinfo.insert(
          {name:'user001',tel: 17727842169, level: 'vip3',skills:["python","go","redis","mongo"]}
      )
      

      插入多条数据

      db.userinfo.insert([
          {name:'user001',tel: 17727842169, level: 'vip3',skills:["python","go","redis","mongo"]},
          {name:"user002",tel: "",level: ""}
      ])
      
      for(let i=1;i<10;i++){
        db.userinfo.insert({name:'user00'+i,tel: 17727842169, level: 'vip3',skills:["python","go","redis","mongo"]})
      }
      
    • insertOne

      db.userinfo.insertOne({name: 'user002',tel: 17727842169,level: 'vip4',skills:["python,go,mongo"]})
      

      插入多条数据

      for(let i=1;i<10;i++){
        db.userinfo.insertOne({name:'user00'+i,tel: 17727842169, level: 'vip3',skills:["python","go","redis","mongo"]})
      }
      
    • insertMany

      db.userinfo.insertMany([
          {name:'user001',tel: 17727842169, level: 'vip3',skills:["python","go","redis","mongo"]},
          {name:"user002",tel: "",level: ""}
      ])
      
    • bulkWrite

  • 删除文档

    • remove 提示在3.2 版本中提示已经废弃

      // 删除所有内容
      db.userinfo.remove({})
      
      // 删除指定内容
      db.userinfo.remove({_id: 1})
      
    • deleteOne

      // 按照升序顺序删除一条文档
      db.userinfo.deleteOne({})
      

      删除指定内容

      db.userinfo.deleteOne({skills:['python','go','redis','mongo']})
      
      db.userinfo.deleteOne({name:{$in:['zhangsan3','zhangsan2']}})
      
    • deleteMany

      // 删除所有内容
      db.userinfo.deleteMany({})
      

      删除指定内容

      db.userinfo.deleteMany({skills:['python','go','redis','mongo']})
      
      db.userinfo.deleteMany({name:{$in:['zhangsan3','zhangsan2']}})
      
    • findOneAndDelete

      删除并返回被删除的内容,雷同于python 中数组的pop()

      newdatabse> db.userinfo.findOneAndDelete({name: 'user9'})
      { _id: ObjectId('6612938d63e940a434c00e8a'), name: 'user9', age: 9 }
      
    • findOneAndReplace

      使用新的集合替换完全替换旧文档内容,并返回旧文档

      newdatabases> db.userinfo.findOneAndReplace({name: 'user6'},{name: "user6",tel: '17727842169'})
      { _id: ObjectId('66129732413ddd1570c00e83'), name: 'user6', age: 6 }
      
    • findOneAndUpdate

      更新文档内容,并返回旧文档内容

      newdatabases> db.userinfo.findOneAndUpdate({age: 7},{$set: {age: 17}})
      { _id: ObjectId('66129732413ddd1570c00e84'), name: 'user7', age: 7 }
      
    • bulkWrite

  • 修改文档

    • update

      db.集合名称.update({选择条件},{更新后字段},{可选项}),在6.0版本中提示已经废除

  • 查询文档

  • insert

    use test
    
  • update

    基本语法:

    # 如果不带$set 更新后只保留{level:"vip2}
    db.userinfo.update({name: "user001"},{level: "vip2"})
    
    db.userinfo.update({name: 'user002'},{$set:{level: "level2"}})
    
    db.userinfo.update({level: 'vip2'},{$set:{address: 'gd',name: 'user002'}})
    
    # 如果不带{multi :true} 则只更新第一个匹配的记录
    db.userinfo.update({level: 'vip3'},{$set:{level: 'vip4'}},{multi: true})
    
    updataMany
    db.userinfo.updateMany         db.userinfo.updateOne          db.userinfo.updateSearchIndex
    
    # 把age 字段+1
    > db.userinfo.update({name: "user003"},{$inc:{age:NumberInt(1)}})
    WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
    > db.userinfo.find()
    { "_id" : ObjectId("660c0c8455d92d32d47c7f4f"), "name" : "user001", "tel" : 17727842169, "level" : "vip3", "skills" : [ "python", "go", "redis", "mongo" ] }
    { "_id" : ObjectId("660c0c8455d92d32d47c7f50"), "name" : "user002", "tel" : "", "level" : "" }
    { "_id" : ObjectId("660c114d55d92d32d47c7f51"), "name" : "user003", "age" : 34 }
    
  • find

    # 查询集合中所有文档
    db.userinfo.find({})
    db.userinfo.find({}).pretty()
    
    db.userinfo.find({level: 'vip2'}).pretty()
    
    # 只显示指定字段 1 表示显示,0 表示不显示
    > db.userinfo.find(     {level: 'vip3'},     {name:1} )
    { "_id" : ObjectId("660c0c8455d92d32d47c7f4f"), "name" : "user001" }
    
    # 只显示指定字段
    > db.userinfo.find(     {level: 'vip3'},     {name:1,_id:0} )
    { "name" : "user001" }
    

    正则查询,支持js的正则表达

    db.userinfo.find({level: /^vip[0-3]+/})
    

    比较查询 < <= > >= = !=

    db.userinfo.find({name: {$lt: 'user005'}})
    
    db.userinfo.find({name: {$lte: 'user005'}})
    
    db.userinfo.find({name: {$gt: 'user005'}})
    
    db.userinfo.find({name: {$gte: 'user005'}})
    
    db.userinfo.find({name: {$eq: 'user005'}})
    
    db.userinfo.find({name: {$ne: 'user005'}})
    

    **包含查询 in nin **

    db.userinfo.find({name: {$in: ["user001","user002"]}})
    
    db.userinfo.find({name: {$nin: ["user001","user002"]}})
    

    逻辑 and or

    and

    db.userinfo.find({name: {$ne: 'user005'},level: 'vip2'})
    
    db.userinfo.find({
        $and:[
            {name: {$lt:'user005'}},
            {level: 'vip2'}
        ]
    })
    

    or

    db.userinfo.find({
        $or:[
            {name: 'user005'},
            {level: 'vip2'}
        ]
    })
    

    匹配skill 数组有4个元素的

    db.userinfo.find({skills:{$size: 4}})
    

    按照名称排序

    1 升序

    -1 降序

    db.userinfo.find({skills:{$size: 4}}).sort({name: -1})
    
    db.userinfo.find({skills:{$size: 4}}).sort({name: -1}).skip(1).limit(2)
    

    分页查询

    # 查询集合中有多少条文档
    db.userinfo.count()
    
    # 限制返回一条
    db.userinfo.find().limit(1)
    
    # 限制返回一条,并跳过第一条。等同于返回第二条数据
    > db.userinfo.find().limit(1).skip(1)
    
    db.userinfo.count({skills:{$size: 4}})
    
    db.userinfo.find({skills:{$size: 4}}).count()
    
    去重
    > db.userinfo.distinct('level')
    [ "vip2", "vip4" ]
    

索引

mongodDB 使用 b-tree 索引,单字段索引和符合索引

  • 创建索引

    db.alert.createIndexes(keys,options)
    
    monitor> db.alert.getIndexes()
    [ { v: 2, key: { _id: 1 }, name: '_id_' } ]
    monitor> db.alert.createIndex({alertname:1})
    alertname_1
    monitor> db.alert.getIndexes()
    [
      { v: 2, key: { _id: 1 }, name: '_id_' },
      { v: 2, key: { alertname: 1 }, name: 'alertname_1' }
    ]
    
    monitor> db.alert.createIndex({alertname:1,instance:1})
    alertname_1_instance_1
    monitor> db.alert.getIndexes()
    [
      { v: 2, key: { _id: 1 }, name: '_id_' },
      { v: 2, key: { alertname: 1 }, name: 'alertname_1' },
      {
        v: 2,
        key: { alertname: 1, instance: 1 },
        name: 'alertname_1_instance_1'
      }
    ]
    
  • 查看索引

    monitor> db.alert.getIndexes()
    // v:2 表示v2版本
    // key: { _id: 1}  以_id 为field为索引,_id:1 其中1表示 升序
    // name: '_id_' 索引名称
    [ { v: 2, key: { _id: 1 }, name: '_id_' } ]
    
  • 删除索引

    # 按照索引名称
    db.alert.dropIndex('alertname_1')
    
    # 按照索引条件
    db.alert.dropIndex({alertname:1,instance:1})
    
    # 删除所有索引
    db.alert.dropIndexes()
    

查询计划

查询是否使用了索引

monitor> db.alert.find({_id: ObjectId('660f919214f296d707ec7cfd')}).explain()
{
  explainVersion: '1',
  queryPlanner: {
    namespace: 'monitor.alert',
    indexFilterSet: false,
    parsedQuery: { _id: { '$eq': ObjectId('660f919214f296d707ec7cfd') } },
    queryHash: 'A300CFDE',
    planCacheKey: 'D6154327',
    maxIndexedOrSolutionsReached: false,
    maxIndexedAndSolutionsReached: false,
    maxScansToExplodeReached: false,
    winningPlan: { stage: 'IDHACK' },
    rejectedPlans: []
  },
  command: {
    find: 'alert',
    filter: { _id: ObjectId('660f919214f296d707ec7cfd') },
    '$db': 'monitor'
  },
  serverInfo: {
    host: '01cd0479423b',
    port: 27017,
    version: '5.0.25',
    gitVersion: 'dab8c370be035063df257746cc1a2de1bf36bcd4'
  },
  serverParameters: {
    internalQueryFacetBufferSizeBytes: 104857600,
    internalQueryFacetMaxOutputDocSizeBytes: 104857600,
    internalLookupStageIntermediateDocumentMaxSizeBytes: 104857600,
    internalDocumentSourceGroupMaxMemoryBytes: 104857600,
    internalQueryMaxBlockingSortMemoryUsageBytes: 104857600,
    internalQueryProhibitBlockingMergeOnMongoS: 0,
    internalQueryMaxAddToSetBytes: 104857600,
    internalDocumentSourceSetWindowFieldsMaxMemoryBytes: 104857600
  },
  ok: 1
}

副本集

  • 主节点 primary : 支持读写
  • 从节点 secondaries: 支持读操作
  • 仲裁者 arbiter: 不保留任何数据的副本,只具有投票选举作用。 也可以将仲裁服务与副本服务部署在一起

分片集

分片集群包含如下组件:

  • shard: 是一个复制集,存储分片数据集的一部分数据
  • mongos: 为客户端提供了访问mongo的路由
  • config servers:存储集群的元数据和配置信息。从3.4开始 config servers 必须部署为复制集架构

架构图

部署config servers

  1. 启动config server

    配置文件 config/configsvr.yaml

    sharding:
      clusterRole: configsvr
    replication:
      replSetName: config
    net:
      bindIp: 0.0.0.0
    storage:
      dbPath: /tmp
      directoryPerDB: true
      engine: wiredTiger
      journal:
        enabled: true
      wiredTiger:
        collectionConfig:
          blockCompressor: snappy
        engineConfig:
          cacheSizeGB: 0.25
          directoryForIndexes: true
        indexConfig:
          prefixCompression: true
    

    docker-compose.yaml

    version: "3.3"
    networks:
      mongodb:
        driver: bridge
        ipam:
          config:
          - subnet: 172.31.4.0/24
    services:
      mongoconfig1:
        networks:
          mongodb:
            ipv4_address: 172.31.4.11
    #        aliases:
    #        - mongoconfig1
        image: mongodb/mongodb-community-server:5.0-ubuntu2004
        volumes:
        - "./config/configsvr.yaml:/etc/configsvr.yaml"
        entrypoint: ["mongod","-f","/etc/configsvr.yaml"]  
    
      mongoconfig2:
        networks:
          mongodb:
            ipv4_address: 172.31.4.12
     #       aliases:
     #       - mongoconfig2
        image: mongodb/mongodb-community-server:5.0-ubuntu2004
        volumes:
        - "./config/configsvr.yaml:/etc/configsvr.yaml"
        entrypoint: ["mongod","-f","/etc/configsvr.yaml"]
    
      mongoconfig3:
        networks:
          mongodb:
            ipv4_address: 172.31.4.13
      #      aliases:
      #      - mongoconfig3
        image: mongodb/mongodb-community-server:5.0-ubuntu2004
        volumes:
        - "./config/configsvr.yaml:/etc/configsvr.yaml"
        entrypoint: ["mongod","-f","/etc/configsvr.yaml"]
    
     docker-compose up -d
    
  2. 构建config server 复制集

    echo 'rs.initiate(
      {
        _id: "config",
        configsvr: true,
        members: [
          { _id : 0, host : "mongoconfig1:27019" },
          { _id : 1, host : "mongoconfig2:27019" },
          { _id : 2, host : "mongoconfig3:27019" }
        ]
      }
    )'|mongosh --host mongoconfig2 --port 27019
    
    

    查看复制集状态

    config [direct: primary] test> rs.status()
    {
      set: 'config',
      date: ISODate('2024-04-06T09:21:58.702Z'),
      myState: 1,
      term: Long('1'),
      syncSourceHost: '',
      syncSourceId: -1,
      configsvr: true,
      heartbeatIntervalMillis: Long('2000'),
      majorityVoteCount: 2,
      writeMajorityCount: 2,
      votingMembersCount: 3,
      writableVotingMembersCount: 3,
      optimes: {
        lastCommittedOpTime: { ts: Timestamp({ t: 1712395318, i: 1 }), t: Long('1') },
        lastCommittedWallTime: ISODate('2024-04-06T09:21:58.474Z'),
        readConcernMajorityOpTime: { ts: Timestamp({ t: 1712395318, i: 1 }), t: Long('1') },
        appliedOpTime: { ts: Timestamp({ t: 1712395318, i: 1 }), t: Long('1') },
        durableOpTime: { ts: Timestamp({ t: 1712395318, i: 1 }), t: Long('1') },
        lastAppliedWallTime: ISODate('2024-04-06T09:21:58.474Z'),
        lastDurableWallTime: ISODate('2024-04-06T09:21:58.474Z')
      },
      lastStableRecoveryTimestamp: Timestamp({ t: 1712395299, i: 1 }),
      electionCandidateMetrics: {
        lastElectionReason: 'electionTimeout',
        lastElectionDate: ISODate('2024-04-06T09:21:50.263Z'),
        electionTerm: Long('1'),
        lastCommittedOpTimeAtElection: { ts: Timestamp({ t: 1712395299, i: 1 }), t: Long('-1') },
        lastSeenOpTimeAtElection: { ts: Timestamp({ t: 1712395299, i: 1 }), t: Long('-1') },
        numVotesNeeded: 2,
        priorityAtElection: 1,
        electionTimeoutMillis: Long('10000'),
        numCatchUpOps: Long('0'),
        newTermStartDate: ISODate('2024-04-06T09:21:50.297Z'),
        wMajorityWriteAvailabilityDate: ISODate('2024-04-06T09:21:50.831Z')
      },
      members: [
        {
          _id: 0,
          name: 'mongoconfig1:27019',
          health: 1,
          state: 2,
          stateStr: 'SECONDARY',
          uptime: 19,
          optime: { ts: Timestamp({ t: 1712395317, i: 1 }), t: Long('1') },
          optimeDurable: { ts: Timestamp({ t: 1712395317, i: 1 }), t: Long('1') },
          optimeDate: ISODate('2024-04-06T09:21:57.000Z'),
          optimeDurableDate: ISODate('2024-04-06T09:21:57.000Z'),
          lastAppliedWallTime: ISODate('2024-04-06T09:21:58.474Z'),
          lastDurableWallTime: ISODate('2024-04-06T09:21:58.474Z'),
          lastHeartbeat: ISODate('2024-04-06T09:21:58.274Z'),
          lastHeartbeatRecv: ISODate('2024-04-06T09:21:57.279Z'),
          pingMs: Long('0'),
          lastHeartbeatMessage: '',
          syncSourceHost: 'mongoconfig2:27019',
          syncSourceId: 1,
          infoMessage: '',
          configVersion: 1,
          configTerm: 1
        },
        {
          _id: 1,
          name: 'mongoconfig2:27019',
          health: 1,
          state: 1,
          stateStr: 'PRIMARY',
          uptime: 150,
          optime: { ts: Timestamp({ t: 1712395318, i: 1 }), t: Long('1') },
          optimeDate: ISODate('2024-04-06T09:21:58.000Z'),
          lastAppliedWallTime: ISODate('2024-04-06T09:21:58.474Z'),
          lastDurableWallTime: ISODate('2024-04-06T09:21:58.474Z'),
          syncSourceHost: '',
          syncSourceId: -1,
          infoMessage: 'Could not find member to sync from',
          electionTime: Timestamp({ t: 1712395310, i: 1 }),
          electionDate: ISODate('2024-04-06T09:21:50.000Z'),
          configVersion: 1,
          configTerm: 1,
          self: true,
          lastHeartbeatMessage: ''
        },
        {
          _id: 2,
          name: 'mongoconfig3:27019',
          health: 1,
          state: 2,
          stateStr: 'SECONDARY',
          uptime: 19,
          optime: { ts: Timestamp({ t: 1712395317, i: 1 }), t: Long('1') },
          optimeDurable: { ts: Timestamp({ t: 1712395317, i: 1 }), t: Long('1') },
          optimeDate: ISODate('2024-04-06T09:21:57.000Z'),
          optimeDurableDate: ISODate('2024-04-06T09:21:57.000Z'),
          lastAppliedWallTime: ISODate('2024-04-06T09:21:58.474Z'),
          lastDurableWallTime: ISODate('2024-04-06T09:21:58.474Z'),
          lastHeartbeat: ISODate('2024-04-06T09:21:58.273Z'),
          lastHeartbeatRecv: ISODate('2024-04-06T09:21:57.276Z'),
          pingMs: Long('0'),
          lastHeartbeatMessage: '',
          syncSourceHost: 'mongoconfig2:27019',
          syncSourceId: 1,
          infoMessage: '',
          configVersion: 1,
          configTerm: 1
        }
      ],
      ok: 1,
      '$gleStats': {
        lastOpTime: Timestamp({ t: 1712395299, i: 1 }),
        electionId: ObjectId('7fffffff0000000000000001')
      },
      lastCommittedOpTime: Timestamp({ t: 1712395318, i: 1 }),
      '$clusterTime': {
        clusterTime: Timestamp({ t: 1712395318, i: 1 }),
        signature: {
          hash: Binary.createFromBase64('AAAAAAAAAAAAAAAAAAAAAAAAAAA=', 0),
          keyId: Long('0')
        }
      },
      operationTime: Timestamp({ t: 1712395318, i: 1 })
    }
    
    

部署shard

  1. 配置文件

    [root@lavm-ioreaqndwv mongoshare]# cat config/mongod.yaml 
    sharding:
      clusterRole: shardsvr
    replication:
      replSetName: shard1
    net:
      bindIp: 0.0.0.0
    storage:
      dbPath: /tmp
      directoryPerDB: true
      engine: wiredTiger
      journal:
        enabled: true
      wiredTiger:
        collectionConfig:
          blockCompressor: snappy
        engineConfig:
          cacheSizeGB: 0.25
          directoryForIndexes: true
        indexConfig:
          prefixCompression: true
    
  2. docker-composer.yaml

    version: "3.3"
    networks:
      mongodb:
        driver: bridge
        ipam:
          config:
          - subnet: 172.31.4.0/24
    services:
      mongoconfig1:
        networks:
          mongodb:
            ipv4_address: 172.31.4.11
            aliases:
            - config
        image: mongodb/mongodb-community-server:5.0-ubuntu2004
        volumes:
        - "./config/configsvr.yaml:/etc/configsvr.yaml"
        entrypoint: ["mongod","-f","/etc/configsvr.yaml"]  
    
      mongoconfig2:
        networks:
          mongodb:
            ipv4_address: 172.31.4.12
            aliases:
            - config
        image: mongodb/mongodb-community-server:5.0-ubuntu2004
        volumes:
        - "./config/configsvr.yaml:/etc/configsvr.yaml"
        entrypoint: ["mongod","-f","/etc/configsvr.yaml"]
    
      mongoconfig3:
        networks:
          mongodb:
            ipv4_address: 172.31.4.13
            aliases:
            - config
        image: mongodb/mongodb-community-server:5.0-ubuntu2004
        volumes:
        - "./config/configsvr.yaml:/etc/configsvr.yaml"
        entrypoint: ["mongod","-f","/etc/configsvr.yaml"]
        
      shard1:
        networks:
          mongodb:
            ipv4_address: 172.31.4.21
            aliases:
            - shard
        image: mongodb/mongodb-community-server:5.0-ubuntu2004
        volumes:
        - "./config/mongod.yaml:/etc/mongod.yaml"
        entrypoint: ["mongod","-f","/etc/mongod.yaml"]
        
      shard2:
        networks:
          mongodb:
            ipv4_address: 172.31.4.22
            aliases:
            - shard
        image: mongodb/mongodb-community-server:5.0-ubuntu2004
        volumes:
        - "./config/mongod.yaml:/etc/mongod.yaml"
        entrypoint: ["mongod","-f","/etc/mongod.yaml"] 
        
      shard3:
        networks:
          mongodb:
            ipv4_address: 172.31.4.23
            aliases:
            - shard
        image: mongodb/mongodb-community-server:5.0-ubuntu2004
        volumes:
        - "./config/mongod.yaml:/etc/mongod.yaml"
        entrypoint: ["mongod","-f","/etc/mongod.yaml"]
    
  3. 初始化

    echo 'rs.initiate(
      {
        _id: "shard1",
        configsvr: false,
        members: [
          { _id : 0, host : "shard1:27018" },
          { _id : 1, host : "shard2:27018" },
          { _id : 2, host : "shard3:27018" }
        ]
      }
    )'|mongosh --host shard3 --port 27018
    
    
    shard1 [direct: primary] test> rs.status()
    {
      set: 'shard1',
      date: ISODate('2024-04-06T09:49:57.241Z'),
      myState: 1,
      term: Long('1'),
      syncSourceHost: '',
      syncSourceId: -1,
      heartbeatIntervalMillis: Long('2000'),
      majorityVoteCount: 2,
      writeMajorityCount: 2,
      votingMembersCount: 3,
      writableVotingMembersCount: 3,
      optimes: {
        lastCommittedOpTime: { ts: Timestamp({ t: 1712396989, i: 5 }), t: Long('1') },
        lastCommittedWallTime: ISODate('2024-04-06T09:49:49.570Z'),
        readConcernMajorityOpTime: { ts: Timestamp({ t: 1712396989, i: 5 }), t: Long('1') },
        appliedOpTime: { ts: Timestamp({ t: 1712396989, i: 5 }), t: Long('1') },
        durableOpTime: { ts: Timestamp({ t: 1712396989, i: 5 }), t: Long('1') },
        lastAppliedWallTime: ISODate('2024-04-06T09:49:49.570Z'),
        lastDurableWallTime: ISODate('2024-04-06T09:49:49.570Z')
      },
      lastStableRecoveryTimestamp: Timestamp({ t: 1712396979, i: 1 }),
      electionCandidateMetrics: {
        lastElectionReason: 'electionTimeout',
        lastElectionDate: ISODate('2024-04-06T09:49:49.514Z'),
        electionTerm: Long('1'),
        lastCommittedOpTimeAtElection: { ts: Timestamp({ t: 1712396979, i: 1 }), t: Long('-1') },
        lastSeenOpTimeAtElection: { ts: Timestamp({ t: 1712396979, i: 1 }), t: Long('-1') },
        numVotesNeeded: 2,
        priorityAtElection: 1,
        electionTimeoutMillis: Long('10000'),
        numCatchUpOps: Long('0'),
        newTermStartDate: ISODate('2024-04-06T09:49:49.560Z'),
        wMajorityWriteAvailabilityDate: ISODate('2024-04-06T09:49:51.021Z')
      },
      members: [
        {
          _id: 0,
          name: 'shard1:27018',
          health: 1,
          state: 2,
          stateStr: 'SECONDARY',
          uptime: 17,
          optime: { ts: Timestamp({ t: 1712396989, i: 5 }), t: Long('1') },
          optimeDurable: { ts: Timestamp({ t: 1712396989, i: 5 }), t: Long('1') },
          optimeDate: ISODate('2024-04-06T09:49:49.000Z'),
          optimeDurableDate: ISODate('2024-04-06T09:49:49.000Z'),
          lastAppliedWallTime: ISODate('2024-04-06T09:49:49.570Z'),
          lastDurableWallTime: ISODate('2024-04-06T09:49:49.570Z'),
          lastHeartbeat: ISODate('2024-04-06T09:49:55.522Z'),
          lastHeartbeatRecv: ISODate('2024-04-06T09:49:57.028Z'),
          pingMs: Long('0'),
          lastHeartbeatMessage: '',
          syncSourceHost: 'shard3:27018',
          syncSourceId: 2,
          infoMessage: '',
          configVersion: 1,
          configTerm: 1
        },
        {
          _id: 1,
          name: 'shard2:27018',
          health: 1,
          state: 2,
          stateStr: 'SECONDARY',
          uptime: 17,
          optime: { ts: Timestamp({ t: 1712396989, i: 5 }), t: Long('1') },
          optimeDurable: { ts: Timestamp({ t: 1712396989, i: 5 }), t: Long('1') },
          optimeDate: ISODate('2024-04-06T09:49:49.000Z'),
          optimeDurableDate: ISODate('2024-04-06T09:49:49.000Z'),
          lastAppliedWallTime: ISODate('2024-04-06T09:49:49.570Z'),
          lastDurableWallTime: ISODate('2024-04-06T09:49:49.570Z'),
          lastHeartbeat: ISODate('2024-04-06T09:49:55.523Z'),
          lastHeartbeatRecv: ISODate('2024-04-06T09:49:57.028Z'),
          pingMs: Long('0'),
          lastHeartbeatMessage: '',
          syncSourceHost: 'shard3:27018',
          syncSourceId: 2,
          infoMessage: '',
          configVersion: 1,
          configTerm: 1
        },
        {
          _id: 2,
          name: 'shard3:27018',
          health: 1,
          state: 1,
          stateStr: 'PRIMARY',
          uptime: 1044,
          optime: { ts: Timestamp({ t: 1712396989, i: 5 }), t: Long('1') },
          optimeDate: ISODate('2024-04-06T09:49:49.000Z'),
          lastAppliedWallTime: ISODate('2024-04-06T09:49:49.570Z'),
          lastDurableWallTime: ISODate('2024-04-06T09:49:49.570Z'),
          syncSourceHost: '',
          syncSourceId: -1,
          infoMessage: 'Could not find member to sync from',
          electionTime: Timestamp({ t: 1712396989, i: 1 }),
          electionDate: ISODate('2024-04-06T09:49:49.000Z'),
          configVersion: 1,
          configTerm: 1,
          self: true,
          lastHeartbeatMessage: ''
        }
      ],
      ok: 1,
      '$clusterTime': {
        clusterTime: Timestamp({ t: 1712396989, i: 5 }),
        signature: {
          hash: Binary.createFromBase64('AAAAAAAAAAAAAAAAAAAAAAAAAAA=', 0),
          keyId: Long('0')
        }
      },
      operationTime: Timestamp({ t: 1712396989, i: 5 })
    }
    

部署mongos

  1. 配置文件

    sharding:
      configDB: config/mongoconfig1:27019,mongoconfig2:27019,mongoconfig3:27019
    net:
      bindIp: 0.0.0.0
    
  2. docker-compose.yaml

    version: "3.3"
    networks:
      mongodb:
        driver: bridge
        ipam:
          config:
          - subnet: 172.31.4.0/24
    services:
      mongoconfig1:
        networks:
          mongodb:
            ipv4_address: 172.31.4.11
            aliases:
            - config
        image: mongodb/mongodb-community-server:5.0-ubuntu2004
        volumes:
        - "./config/configsvr.yaml:/etc/configsvr.yaml"
        entrypoint: ["mongod","-f","/etc/configsvr.yaml"]  
    
      mongoconfig2:
        networks:
          mongodb:
            ipv4_address: 172.31.4.12
            aliases:
            - config
        image: mongodb/mongodb-community-server:5.0-ubuntu2004
        volumes:
        - "./config/configsvr.yaml:/etc/configsvr.yaml"
        entrypoint: ["mongod","-f","/etc/configsvr.yaml"]
    
      mongoconfig3:
        networks:
          mongodb:
            ipv4_address: 172.31.4.13
            aliases:
            - config
        image: mongodb/mongodb-community-server:5.0-ubuntu2004
        volumes:
        - "./config/configsvr.yaml:/etc/configsvr.yaml"
        entrypoint: ["mongod","-f","/etc/configsvr.yaml"]
        
      shard1:
        networks:
          mongodb:
            ipv4_address: 172.31.4.21
            aliases:
            - shard
        image: mongodb/mongodb-community-server:5.0-ubuntu2004
        volumes:
        - "./config/mongod.yaml:/etc/mongod.yaml"
        entrypoint: ["mongod","-f","/etc/mongod.yaml"]
        
      shard2:
        networks:
          mongodb:
            ipv4_address: 172.31.4.22
            aliases:
            - shard
        image: mongodb/mongodb-community-server:5.0-ubuntu2004
        volumes:
        - "./config/mongod.yaml:/etc/mongod.yaml"
        entrypoint: ["mongod","-f","/etc/mongod.yaml"] 
        
      shard3:
        networks:
          mongodb:
            ipv4_address: 172.31.4.23
            aliases:
            - shard
        image: mongodb/mongodb-community-server:5.0-ubuntu2004
        volumes:
        - "./config/mongod.yaml:/etc/mongod.yaml"
        entrypoint: ["mongod","-f","/etc/mongod.yaml"]
      mongos1:
        networks:
          mongodb:
            ipv4_address: 172.31.4.31
            aliases:
            - mongos
        image: mongodb/mongodb-community-server:5.0-ubuntu2004 
        volumes:
        - "./config/mongos.yaml:/etc/mongos.yaml"
        entrypoint: ["mongos","-f","/etc/mongos.yaml"]
    
  3. 添加分片信息

    [direct: mongos] monitor> sh.addShard( "shard1/shard1:27018,shard2:27018,shard3:27018")
    {
      shardAdded: 'shard1',
      ok: 1,
      '$clusterTime': {
        clusterTime: Timestamp({ t: 1712399671, i: 4 }),
        signature: {
          hash: Binary.createFromBase64('AAAAAAAAAAAAAAAAAAAAAAAAAAA=', 0),
          keyId: Long('0')
        }
      },
      operationTime: Timestamp({ t: 1712399671, i: 4 })
    }
    
  4. 为指定数据库启用分片

    sh.enableSharding("monitor")
    
  5. 对指定数据库中指定集合按照指定算法进行分片

    sh.shardCollection("monitor.alert", { name : "hashed" } )
    

mongodb 增加副本

rs.add()

路由节点 mongos

配置节点 config

复制集:

primary

secoundary

仲裁节点

sh.status()

config 是mongo的配置文件数据库

  • 服务端启动认证

    1. mongod -f mongod.conf --auth

    2. 方式2

      security:
        authorization: enabled
      
newdatabase> db.userinfo.count()
DeprecationWarning: Collection.count() is deprecated. Use countDocuments or estimatedDocumentCount.
4
newdatabase> db.userinfo.countDocuments()
4
newdatabase> db.userinfo.estimatedDcocumentCount()
TypeError: db.userinfo.estimatedDcocumentCount is not a function
newdatabase> db.userinfo.estimatedDocumentCount()
4
newda

安全认证

doc

  1. 拿到数据库如何确定数据库版本,架构 等基本信息

  2. 查看数据库文档大小,和索引大小

  3. 慢查询原因,如何判断查询是否走了索引

  4. 数据备份和恢复

  5. 数据迁移

posted @ 2022-02-20 00:10  mingtian是吧  阅读(13)  评论(0编辑  收藏  举报