mongoDB, manual

Positioning

As a non-relational database, it is
often compared with mysql

advantage

  • Easy to expand, no concept of table structure, add fields at will (can be at will but not at will)
  • Single collection transaction performance + non-transaction addition, deletion and modification performance are better than mysql
  • Naturally support horizontal expansion sharding (sharding)

Disadvantages

  • Cross-collection transaction performance is low. The official recommendation is to use nested documents instead of related design in relational databases. However, you should be careful not to design data with larger growth space as nested documents. First, the design may be unreasonable. Second, for nested documents with larger growth space , optimization and statistical queries are more troublesome.

Build and configure

Referring to build the official document
production using Notes:

  • Preferential use of cloud services
  • For self-built, the formal environment needs to create a copy set to avoid single node crash
  • mogno has no password by default, do not bind to the external network ip when authorization is not configured
  1. Replica set

    1. If a few members fail, the replica set can still perform both write and read operations.
      However, if the data center with the majority of members fails, the replica set will become read-only.
  2. Fragmentation

    Sharding requires at least two shards to distribute sharded data. If you plan to enable sharding in the near future, but it is not needed during deployment, a single sharded cluster may be useful.

    1. Configuration
      1. The configuration service is deployed as three members of the replica set
      2. Deploy each shard as a 3-member replica set
      3. Deploy one or more mongos routers
    2. Boot sequence
      1. configsvr must be a replica set configuration service metadata
      2. shardsvr must be a replica set
      3. mongos gateway

Precautions

  1. index
    1. mongo is not just mongo, the query must hit the index
    2. Note that to effectively use the index, while hitting the index, you must control the index hit range. If the range is too large, there is no index.
    3. When creating an index, you must specify background: true to avoid the large amount of table data jamming the database
      # 查看user_mobile_info表索引创建 进度
      db.currentOp({"command.createIndexes":"user_mobile_info"}) 
      
    4. Mongo's query will not automatically do type conversion like mysql, you must use the corresponding type (character | number) when querying
    5. A field can have both a character type and a number type, and special attention should be paid to changes when using it
    6. Reasonably use the index's expireAfterSeconds parameter
    7. All indexes are created through spring-data annotations, which is more intuitive to view during programming
  2. thing
    1. After version 4.2, transaction support is relatively complete, but cross-collection transactions have performance problems
  3. Disk free
    1. remove will not free up disk space (continue to occupy will be reused)
    2. drop will release some disk space
    3. repairDatabase will rebuild the space and index however!
      1. Will lock the entire library
      2. 50% + additional space required
      3. Longer time
      4. Other similar operations will also lock the library or table

    When the memory consumption of the library table is relatively large, the space release will be very troublesome, so you must make a copy set when creating the database!
    Copying the repairDatabase one by one is no problem

Troubleshoot mongodDB

  1. mongodDB crash
    1. Check the log file for specific problems (the file path is specified at startup)
    2. Restart, pay attention to the specified configuration file
    3. Prioritize machine memory issues
  2. mognoDB stuck
    1. Prioritize indexing issues, as well as network issues
    2. It can start the whole issue is written or read, use mongostat similar to UNIX / Linux file system utility vmstat, located in the installation directory / bin directory
    3. View specific issues through slow logs, view slow logs
      1. Common commands
      # 查看慢日志是否开启,以及级别和时间(毫秒) 
      PRIMARY> db.getProfilingStatus()
      { "was" : 1, "slowms" : 200 }
      #查看级别 
      0 –  不开启 
      1 –  记录慢命令 (默认为>100ms)  
      2 –  记录所有命令  
      PRIMARY> db.getProfilingLevel()
      1
      # 设置级别和时间
      PRIMARY> db.setProfilingLevel(1,200)
      { "was" : 2, "slowms" : 100, "ok" : 1 }
      # 案例(排除了某些表的慢日志结果,并按耗时大小排序(毫秒))
      db.system.profile.find( { millis : { $gt : 100 }, ns:{$nin:["imoney.accountCheck","imoney.accountPay"]} } ).sort({millis:-1})
      
    4. Check the mongo log for exceptions
    5. Check whether the system network cpu disk memory is abnormal
Published 17 original articles · won 24 · views 280,000 +

Guess you like

Origin blog.csdn.net/qq_22956867/article/details/102706762