设为首页 收藏本站
查看: 997|回复: 0

[经验分享] mongodb学习(-)

[复制链接]

尚未签到

发表于 2018-10-25 10:23:53 | 显示全部楼层 |阅读模式
MongoDB 是一个基于分布式文件存储的数据库。由 C++ 语言编写。  
旨在为 WEB 应用提供可扩展的高性能数据存储解决方案。
  
MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。
  

  
什么是NoSQL?
  
NoSQL,指的是非关系型的数据库。NoSQL有时也称作Not Only SQL的缩写,
  
是对不同于传统的关系型数据库的数据库管理系统的统称。
  
NoSQL用于超大规模数据的存储。(例如谷歌或Facebook每天为他们的用户收集万亿比特的数据)。
  
这些类型的数据存储不需要固定的模式,无需多余操作就可以横向扩展。
  

  

  
mongodb复制集配置
  
因为机器比较卡,所以就搭建在一台虚拟机里了,配置复制集,至少需要3个实例
  
master    192.168.1.156:27017
  
slave    192.168.1.156:37017
  
arbiter    192.168.1.156:47017
  

  

  

  
[root@Warfare_all_192.168.1.156 ~]# egrep -v '^#|^$' /etc/mongodb.conf
  
bind_ip = 192.168.1.156
  
port = 27017
  
fork = true
  
pidfilepath = /var/run/mongodb/mongodb.pid
  
logpath = /var/log/mongodb/mongodb.log
  
dbpath =/var/lib/mongodb
  
journal = true
  
replSet = shard1
  
nohttpinterface = true
  

  
[root@Warfare_all_192.168.1.156 ~]# egrep -v '^#|^$' /etc/mongodb_37017.conf
  
bind_ip = 192.168.1.156
  
port = 37017
  
fork = true
  
pidfilepath = /var/run/mongodb_37017/mongodb.pid
  
logpath = /var/log/mongodb_37017/mongodb.log
  
dbpath =/var/lib/mongodb_37017
  
journal = true
  
replSet = shard1
  
nohttpinterface = true
  

  
[root@Warfare_all_192.168.1.156 ~]# egrep -v '^#|^$' /etc/mongodb_47017.conf
  
bind_ip = 192.168.1.156
  
port = 47017
  
fork = true
  
pidfilepath = /var/run/mongodb_47017/mongodb.pid
  
logpath = /var/log/mongodb_47017/mongodb.log
  
dbpath =/var/lib/mongodb_47017
  
journal = true
  
replSet = shard1
  
nohttpinterface = true
  

  

  
ps aux |grep mongo
  
root      62095  0.6  3.1 3304700 60460 ?       Sl   20:07   0:24 mongod --quiet -f /etc/mongodb_57017.conf
  
root      62387  0.6  2.0 2944248 39176 ?       Sl   20:08   0:24 mongod --quiet -f /etc/mongodb_47017.conf
  
root      66145  0.6  3.5 5419352 67156 ?       Sl   20:20   0:18 mongod --quiet -f /etc/mongodb_37017.conf
  
root      68910  0.6  3.1 5264784 60024 ?       Sl   20:25   0:16 mongod --quiet -f /etc/mongodb.conf
  
[root@Warfare_all_192.168.1.156 ~]# mongo --host=192.168.1.156
  
MongoDB shell version: 2.4.14
  
connecting to: 192.168.1.156:27017/test
  
shard1:PRIMARY> show dbs;
  
local2.0771484375GB
  
test0.203125GB
  
wabao0.203125GB
  
shard1:PRIMARY>rs.conf()
  
{
  "_id" : "shard1",
  "version" : 15,
  "members" : [
  {
  "_id" : 0,
  "host" : "192.168.1.156:27017",
  "priority" : 10
  },
  {
  "_id" : 1,
  "host" : "192.168.1.156:37017",
  "priority" : 5
  },
  {
  "_id" : 2,
  "host" : "192.168.1.156:57017",
  "priority" : 3
  }
  ]
  
}
  

  
shard1:PRIMARY>rs.conf()
  

  

  

  
shard1:PRIMARY> help
  db.help()                    help on db methods
  db.mycoll.help()             help on collection methods
  sh.help()                    sharding helpers
  rs.help()                    replica set helpers
  help admin                   administrative help
  help connect                 connecting to a db help
  help keys                    key shortcuts
  help misc                    misc things to know
  help mr                      mapreduce
  

  show dbs                     show database names
  show collections             show collections in current database
  show users                   show users in current database
  show profile                 show most recent system.profile entries with time >= 1ms
  show logs                    show the accessible logger names
  show log [name]              prints out the last segment of log in memory, 'global' is default
  use                 set current database
  db.foo.find()                list objects in collection foo
  db.foo.find( { a : 1 } )     list objects in foo where a == 1
  it                           result of the last line evaluated; use to further iterate
  DBQuery.shellBatchSize = x   set default number of items to display on shell
  exit                         quit the mongo shell
  
shard1:PRIMARY> db.help()
  
DB methods:
  db.addUser(userDocument)
  db.adminCommand(nameOrDocument) - switches to 'admin' db, and runs command [ just calls db.runCommand(...) ]
  db.auth(username, password)
  db.cloneDatabase(fromhost)
  db.commandHelp(name) returns the help for the command
  db.copyDatabase(fromdb, todb, fromhost)
  db.createCollection(name, { size : ..., capped : ..., max : ... } )
  db.currentOp() displays currently executing operations in the db
  db.dropDatabase()
  db.eval(func, args) run code server-side
  db.fsyncLock() flush data to disk and lock server for backups
  db.fsyncUnlock() unlocks server following a db.fsyncLock()
  db.getCollection(cname) same as db['cname'] or db.cname
  db.getCollectionNames()
  db.getLastError() - just returns the err msg string
  db.getLastErrorObj() - return full status object
  db.getMongo() get the server connection object
  db.getMongo().setSlaveOk() allow queries on a replication slave server
  db.getName()
  db.getPrevError()
  db.getProfilingLevel() - deprecated
  db.getProfilingStatus() - returns if profiling is on and slow threshold
  db.getReplicationInfo()
  db.getSiblingDB(name) get the db at the same server as this one
  db.hostInfo() get details about the server's host
  db.isMaster() check replica primary status
  db.killOp(opid) kills the current operation in the db
  db.listCommands() lists all the db commands
  db.loadServerScripts() loads all the scripts in db.system.js
  db.logout()
  db.printCollectionStats()
  db.printReplicationInfo()
  db.printShardingStatus()
  db.printSlaveReplicationInfo()
  db.removeUser(username)
  db.repairDatabase()
  db.resetError()
  db.runCommand(cmdObj) run a database command.  if cmdObj is a string, turns it into { cmdObj : 1 }
  db.serverStatus()
  db.setProfilingLevel(level,) 0=off 1=slow 2=all
  db.setVerboseShell(flag) display extra information in shell output
  db.shutdownServer()
  db.stats()
  db.version() current version of the server
  
shard1:PRIMARY> rs.help()
  rs.status()                     { replSetGetStatus : 1 } checks repl set status
  rs.initiate()                   { replSetInitiate : null } initiates set with default settings
  rs.initiate(cfg)                { replSetInitiate : cfg } initiates set with configuration cfg
  rs.conf()                       get the current configuration object from local.system.replset
  rs.reconfig(cfg)                updates the configuration of a running replica set with cfg (disconnects)
  rs.add(hostportstr)             add a new member to the set with default attributes (disconnects)
  rs.add(membercfgobj)            add a new member to the set with extra attributes (disconnects)
  rs.addArb(hostportstr)          add a new member which is arbiterOnly:true (disconnects)
  rs.stepDown([secs])             step down as primary (momentarily) (disconnects)
  rs.syncFrom(hostportstr)        make a secondary to sync from the given member
  rs.freeze(secs)                 make a node ineligible to become primary for the time specified
  rs.remove(hostportstr)          remove a host from the replica set (disconnects)
  rs.slaveOk()                    shorthand for db.getMongo().setSlaveOk()
  

  db.isMaster()                   check who is primary
  db.printReplicationInfo()       check oplog size and time range
  

  reconfiguration helpers disconnect from the database so the shell will display
  an error, even if the command succeeds.
  see also http://:28017/_replSet for additional diagnostic info
  
shard1:PRIMARY> db.collection.find().help()
  
find() modifiers
  .sort( {...} )
  .limit( n )
  .skip( n )
  .count() - total # of objects matching query, ignores skip,limit
  .size() - total # of objects cursor would return, honors skip,limit
  .explain([verbose])
  .hint(...)
  .addOption(n) - adds op_query options -- see wire protocol
  ._addSpecial(name, value) - http://dochub.mongodb.org/core/advancedqueries#AdvancedQueries-Metaqueryoperators
  .batchSize(n) - sets the number of docs to return per getMore
  .showDiskLoc() - adds a $diskLoc field to each returned object
  .min(idxDoc)
  .max(idxDoc)
  

  
Cursor methods
  .toArray() - iterates through docs and returns an array of the results
  .forEach( func )
  .map( func )
  .hasNext()
  .next()
  .objsLeftInBatch() - returns count of docs left in current batch (when exhausted, a new getMore will be issued)
  .count(applySkipLimit) - runs command at server
  .itcount() - iterates through documents and counts them
  
shard1:PRIMARY> sh.help()
  sh.addShard( host )                       server:port OR setname/server:port
  sh.enableSharding(dbname)                 enables sharding on the database dbname
  sh.shardCollection(fullName,key,unique)   shards the collection
  sh.splitFind(fullName,find)               splits the chunk that find is in at the median
  sh.splitAt(fullName,middle)               splits the chunk that middle is in at middle
  sh.moveChunk(fullName,find,to)            move the chunk where 'find' is to 'to' (name of shard)
  sh.setBalancerState(  )   turns the balancer on or off true=on, false=off
  sh.getBalancerState()                     return true if enabled
  sh.isBalancerRunning()                    return true if the balancer has work in progress on any mongos
  sh.addShardTag(shard,tag)                 adds the tag to the shard
  sh.removeShardTag(shard,tag)              removes the tag from the shard
  sh.addTagRange(fullName,min,max,tag)      tags the specified range of the given collection
  sh.status()



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-626234-1-1.html 上篇帖子: MongoDB常用配置及维护 下篇帖子: 在centos6.5上安装mongodb数据库
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表