NoSQL理论基础及安装、基本操作
30分钟开始 分布式系统理论: CAP: 一致性 可用性 分区容错性 MongoDB: 安装 crud 索引 副本集 分片 NoSQL:非关系型、分布式、不提供ACID功能 技术特点: 1、简单数据模型 2、元数据和应用数据分离(分不同服务器存储) 3、弱一致性 优势: 1、避免不必要的复杂性 2、高吞吐量 3、高水平扩展能力和低端硬件集群 4、不适用对象-关系映射 劣势: 1、不支持ACID特性 2、功能简单 3、没有统一的数据查询模型 分类: NoSQL: 键值存储 列式数据库 文档数据库 图式数据库 SQL: mysql pgsql 缓存数据库系统: memcache CAP理论:从CAP中挑出2个 BASE理论: 基本可用 软状态 最终一致性 C,A:SQL(保证一致性,可用性) C,P:悲观加锁机制(一致性,分区容错性) A,P:DNS 数据一致性模型:强一致性、弱一致性、最终一致性 数据一致性的实现技术: Quorum(法定票数)系统NRW策略(关注) N:副本数 R:完成读操作所需要读取的最少副本数 W:完成写操作所需要写入的最少副本数 要想保证强一致性:R+W>N 最多只能保证最终一致性:R+W<=N 两段式提交:2PC(two phase commit protocol)(关注) 有两类节点: 一类为协调者 一类为事务参与者 两段: 1、请求阶段:事务协调者通知事务参与者提交事务 2、提交阶段:事务参与者提交事务 时间戳策略 Paxos:根据协议进行协调 向量时钟 NoSQL的数据存储模型: 1、键值存储:k-w 优点:查找迅速 缺点:数据无结构、通常只被当做字符串或二进制数据 应用场景:内容缓存 实例:redis,dynamo 2、列式模型: 数据模型:数据按列存储,将同一列数据存在一起 优点:查找迅速,可扩展性强,易于实现分布式 缺点:功能相对sql有限 应用场景:分布式文件系统或分布式存储 实例:Bigtable(google),cassandra,HBase,Hypertable 3、文档模型 数据模型:与键值模型相似,value指向结构化数据 优点:数据格式要求不严格,无需事先定义结构 缺点:查询性能不高,缺乏统一查询语法 应用场景:web应用 实例:MongoDB,CouchDB 4、图式模型: 数据模型:图结构模型 优点:利用图结构相关算法提高性能,满足特殊场景应用需求 缺点:实现分布式较困难,功能有定向性 应用场景:社交网络、推荐系统、关系图谱 实例:Neo4j www.nosql-database.org Mongodb: collection:表 多个collection:database MongoDB的安装:是一个易于扩展的、高性能、开源、文档模式的no-sql数据库 存储:海量数据、文档数据库、不需要创建表结构、c++研发的,开源 是什么? 基于文档数据库(json格式) 无表结构 性能: c++ 支持各种索引 不支持事务 内存映射(延迟写操作) 扩展性: 复制 auto-sharding 商业支持 支持基于文档的查询:表达式为json 支持使用map/reduce 灵活的聚合操作 在分片的基础上并行处理 GridFS:网格文件系统,存储单个大文件或海量小文件的分布式文件系统 地理位置、空间索引 被生产环境验证过 特性: 动态查询 查询性能剖析 基于复制完成故障自动转移 rabbitmq的性能太差,使用HBase 适应场景: web网站 缓存 低价值、高存储量 高扩展性 实现对象、json存储的应用编程环境 不适合场景: 事务型 商业智能决策 使用sql接口的 MongoDB数据模型:面向集合的数据库 数据库:无需创建 表:集合(行):由文档组成,多个文档组成一个表,文档是json格式的(可以嵌套),集合无需事先定义 c/s: mongod服务器端 mongo 安装: 查看配置文件: [root@stu~]#cat/etc/mongod.conf #mongo.conf #wheretolog logpath=/var/log/mongo/mongod.log logappend=true #forkandruninbackground fork=true #port=27017 dbpath=/var/lib/mongo#运行mongod服务的用户也是mongod,所以保证这个目录的属主属组也为mongod, 为了数据使用,应该找一个合理的目录 #locationofpidfile pidfilepath=/var/run/mongodb/mongod.pid #Disableswrite-aheadjournaling #nojournal=true #EnablesperiodicloggingofCPUutilizationandI/Owait #cpu=true #Turnon/offsecurity.Offiscurrentlythedefault #noauth=true #auth=true #Verboseloggingoutput. #verbose=true #Inspectallclientdataforvalidityonreceipt(usefulfor #developingdrivers) #objcheck=true #Enabledbquotamanagement #quota=true #Setoplogginglevelwherenis #0=off(default) #1=W #2=R #3=both #7=W+somereads #diaglog=0 #Ignorequeryhints #nohints=true #DisabletheHTTPinterface(Defaultstolocalhost:27018). #nohttpinterface=true #Turnsoffserver-sidescripting.Thiswillresultingreatlylimited #functionality #noscripting=true #Turnsofftablescans.Anyquerythatwoulddoatablescanfails. #notablescan=true #Disabledatafilepreallocation. #noprealloc=true #Specify.nsfilesizefornewdatabases. #nssize=<size> #AccouttokenforMongomonitoringserver. #mms-token=<token> #ServernameforMongomonitoringserver. #mms-name=<server-name> #PingintervalforMongomonitoringserver. #mms-interval=<seconds> #ReplicationOptions #inreplicatedmongodatabases,specifyherewhetherthisisaslaveormaster #slave=true #source=master.example.com #Slaveonly:specifyasingledatabasetoreplicate #only=master.example.com #or #master=true #source=slave.example.com 创建目录,改属主属组 修改配置文件 启动服务 system数据库保存其他数据库的元数据(和myslq中的mysql数据库一样) 查看端口: 27017:服务端口 28017:管理端口 NoSQL数据库一般是在内网中使用的,不进行认证,直接连 数据库帮助: >db.help function(){ print("DBmethods:"); print("tdb.addUser(userDocument)"); print("tdb.adminCommand(nameOrDocument)-switchesto'admin'db,andrunscommand[justcallsdb.runCommand(...)]"); print("tdb.auth(username,password)"); print("tdb.cloneDatabase(fromhost)"); print("tdb.commandHelp(name)returnsthehelpforthecommand"); print("tdb.copyDatabase(fromdb,todb,fromhost)"); print("tdb.createCollection(name,{size:...,capped:...,max:...})"); print("tdb.currentOp()displayscurrentlyexecutingoperationsinthedb"); print("tdb.dropDatabase()"); print("tdb.eval(func,args)runcodeserver-side"); print("tdb.fsyncLock()flushdatatodiskandlockserverforbackups"); print("tdb.fsyncUnlock()unlocksserverfollowingadb.fsyncLock()"); print("tdb.getCollection(cname)sameasdb['cname']ordb.cname"); print("tdb.getCollectionNames()"); print("tdb.getLastError()-justreturnstheerrmsgstring"); print("tdb.getLastErrorObj()-returnfullstatusobject"); print("tdb.getMongo()gettheserverconnectionobject"); print("tdb.getMongo().setSlaveOk()allowqueriesonareplicationslaveserver"); print("tdb.getName()"); print("tdb.getPrevError()"); print("tdb.getProfilingLevel()-deprecated"); print("tdb.getProfilingStatus()-returnsifprofilingisonandslowthreshold"); print("tdb.getReplicationInfo()"); print("tdb.getSiblingDB(name)getthedbatthesameserverasthisone"); print("tdb.hostInfo()getdetailsabouttheserver'shost"); print("tdb.isMaster()checkreplicaprimarystatus"); print("tdb.killOp(opid)killsthecurrentoperationinthedb"); print("tdb.listCommands()listsallthedbcommands"); print("tdb.loadServerScripts()loadsallthescriptsindb.system.js"); print("tdb.logout()"); print("tdb.printCollectionStats()"); print("tdb.printReplicationInfo()"); print("tdb.printShardingStatus()"); print("tdb.printSlaveReplicationInfo()"); print("tdb.removeUser(username)"); print("tdb.repairDatabase()"); print("tdb.resetError()"); print("tdb.runCommand(cmdObj)runadatabasecommand.ifcmdObjisastring,turnsitinto{cmdObj:1}"); print("tdb.serverStatus()"); print("tdb.setProfilingLevel(level,<slowms>)0=off1=slow2=all"); print("tdb.setVerboseShell(flag)displayextrainformationinshelloutput"); print("tdb.shutdownServer()"); print("tdb.stats()"); print("tdb.version()currentversionoftheserver"); return__magicNoPrint; } 集合帮助: >db.mycoll.help() DBCollectionhelp db.mycoll.find().help()-showDBCursorhelp db.mycoll.count() db.mycoll.copyTo(newColl)-duplicatescollectionbycopyingalldocumentstonewColl;noindexesarecopied. db.mycoll.convertToCapped(maxBytes)-calls{convertToCapped:'mycoll',size:maxBytes}}command db.mycoll.dataSize() db.mycoll.distinct(key)-e.g.db.mycoll.distinct('x') db.mycoll.drop()dropthecollection db.mycoll.dropIndex(index)-e.g.db.mycoll.dropIndex("indexName")ordb.mycoll.dropIndex({"indexKey":1}) db.mycoll.dropIndexes() db.mycoll.ensureIndex(keypattern[,options])-optionsisanobjectwiththesepossiblefields:name,unique,dropDups db.mycoll.reIndex() db.mycoll.find([query],[fields])-queryisanoptionalqueryfilter.fieldsisoptionalsetoffieldstoreturn. e.g.db.mycoll.find({x:77},{name:1,x:1}) db.mycoll.find(...).count() db.mycoll.find(...).limit(n) db.mycoll.find(...).skip(n) db.mycoll.find(...).sort(...) db.mycoll.findOne([query]) db.mycoll.findAndModify({update:...,remove:bool[,query:{},sort:{},'new':false]}) db.mycoll.getDB()getDBobjectassociatedwithcollection db.mycoll.getIndexes() db.mycoll.group({key:...,initial:...,reduce:...[,cond:...]}) db.mycoll.insert(obj) db.mycoll.mapReduce(mapFunction,reduceFunction,<optionalparams>) db.mycoll.remove(query) db.mycoll.renameCollection(newName,<dropTarget>)renamesthecollection. db.mycoll.runCommand(name,<options>)runsadbcommandwiththegivennamewherethefirstparamisthecollectionname db.mycoll.save(obj) db.mycoll.stats() db.mycoll.storageSize()-includesfreespaceallocatedtothiscollection db.mycoll.totalIndexSize()-sizeinbytesofalltheindexes db.mycoll.totalSize()-storageallocatedforalldataandindexes db.mycoll.update(query,object[,upsert_bool,multi_bool])-insteadoftwoflags,youcanpassanobjectwithfields:upsert,multi db.mycoll.validate(<full>)-SLOW db.mycoll.getShardVersion()-onlyforusewithsharding db.mycoll.getShardDistribution()-printsstatisticsaboutdatadistributioninthecluster db.mycoll.getSplitKeysForChunks(<maxChunkSize>)-calculatessplitpointsoverallchunksandreturnssplitterfunction 简单使用: 使用数据库:(无需创建),collection也无需创建 db.collection.insert:插入 show collections:查询集合 db.collections.find():查询语句 db.collections.update():更新 db.collections.remove():移除 集合信息: 删除集合: 查看数据库文件: 基本操作: show dbs:查看所有数据库 show collections:查看集合 show users:查看用户 show profile: show logs:查看所有日志列表 show log [name]:查看具体的日志 远程连接: mongo --host ip crud操作: create,read,update,delete 虽然没有表结构,但还是应该对同类对象放到一个collection 查询: db.users.find({age:{$gt:18}}).sort({age:1}) 查询age大于18的用户,以age为升序进行排序 插入: db.users.insert( { name:'suse', age:26, status:'A', group:['news','sports'] } ) 更新: db.coll.update( {age:{$gt:18}}, {$set:{status:'A'}}, {multi:true} 不指定时只修改第一个符合条件 ) 删除: db.coll.delete( {status:'D'} ) 插入: 一批只显示20个,输入it继续 limit: 删除: 修改: find高级用法: db.collection.find(<添加>,<字段>) db.collection.count()返回条数 比较运算: $gt:大于{field:{$gt:value}} $gte:大于等于{field:{$gte:value}} $in:存在于{field:{$in:[value1,value2,...]}} $lt:小于{field:{$lt:value}} $lte:小于等于{field:{$lte:value}} $ne:不等于{field:{$ne:value}} $nin:不存在于{field:{$nin:[value1,value2...]}} 大于 显示需要的字段: 逻辑运算: $or:或运算,{$or:[{expression1},{expression2},...]} $and:或运算,{$and:[{expression1},...]} $not:或运算,{field:{$not:{operator-expression}}} $nor:反运算,即返回不符合所有指定条件的文档,{$nor:[{expression1},...]} 与运算: 元素查询: 如果要分居文档中是否存在某字段等条件来挑选文档,则需要用到元素运算 $exists:根据指定字段的存在性挑选文档,语法:{field:{$exists:<boolean>}},指定<boolean>的值为'true'则返回存在指定字段的文档,'false'则返回不存在指定字段的文档 $mod:将指定字段的值进行取模运算,并返回其余数作为指定值的文档,语法{field:{$mod:[divisor,remainder]}} $type:返回指定字段的值类型为指定类型的文档,语法:{field:{$type:<bson type>}} 重新插入一条数据: 查询: 更新: update专有操作符大致包含:field,array,bitwise field: $inc:增大指定字段的值,格式: db.collection.update({field:value},{$nic:{field1:amount}}),其中{field:value}用于指定挑选标准,{$inc:{field1:amount}}用于指定要提升其值的字段及提升大小amount $rename:更改字段名,格式为{$rename:{<old name1>:<new name1>,<old name2>:<new name2>,...}} $set:修改字段的值为新指定的值,格式db.collection.update({field:value1},{$set:{field2:value2}}) $unset:删除指定的字段,格式db.collection.update({field:value1},{$unset:{field1:""}}) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |