5.非关系数据库(Nosql)它mongodb:创建一个集合,导出和导入备份, 数据恢复,进出口
1
固定集合
固定集合值得是事先创建并且大小固定的集合
2
固定集合的特征:固定集合非常像环形队列。假设空间不足,最早文档就会被删除,为新的文档腾出空间。一般来说。固定集合适用于不论什么想要自己主动淘汰过期属性的场景,没有太多的操作限制。
3
创建固定集合使用命令:
db.createCollection(“collectionName”,{capped:true,size:100000,max:100});
size:指定集合大小,单位为KB,max指定文档的数量
当指定文档数量上限时,必须同一时候指定大小。
淘汰机制仅仅有在容量还没有满时才会根据文档数量来工作。要是容量满了,淘汰机制根据容量来工作。
4
创建一个集合:
>db.createCollection("cap1",{capped:true,size:1000,max:100});
{ "ok" : 1 }
>
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG90b3R1enVvcXVhbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">
5
插入数据
> for(var i=1;i<=100;i++){
...db.cap1.insert({name:"dongxue",age:i});
... }
WriteResult({ "nInserted" : 1 })
> db.cap1.find().count();
53 (大小之所以是53是由于大小超过了1000)
6
固定集合的应用场景:聊天记录。日志信息
淘汰机制:当满足size指定集合大小。不能再继续往固定集合中加数据。
固定集合的容量优先
当文档达到100条时,再加入的时候会替换先前的
7
备份与导入导出。
MongoDB提供了备份和回复的功能,各自是MongoDB下载文件夹下的mongodump.exe和mongorestore.exe文件。
备份数据使用以下的命令:
mongodump –h dbhost –d dbname –o dbdirectory
-h:MonDB所在server地址,比如:127.0.0.1,当然也能够指定port号:127.0.0.1:27017,当然该文件夹须要提前创建。在备份完毕后,系统自己主动在dump文件夹下建立一个test文件夹,这个文件夹里面存放该数据库实例的备份数据。
mongodump -h localhost:27017 -d toto -of:/beifeng
-h:用来指定要输出的数据库所在的ip地址和port号
-d:
指定要备份的数据库
-o:
表示要备份到的文件文件夹
运行后的效果图:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG90b3R1enVvcXVhbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">
7
另外启动一个mongodb的client,目的是将数据库中toto数据库删掉
|
C:\Users\to-to>mongo MongoDB shell version: 2.6.4 connecting to: test > use toto; switched to db toto > db.help(); DB methods: db.adminCommand(nameOrDocument) - switches to 'admin' db, and runs command [ just calls db.r unCommand(...) ] 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.createUser(userDocument) 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.getWriteConcern() - returns the write concern used for any operations on this db, inherit ed from server object if set 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.dropUser(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,<slowms>) 0=off 1=slow 2=all db.setWriteConcern( <write concern doc> ) - sets the write concern for writes to the db db.unsetWriteConcern( <write concern doc> ) - unsets the write concern for writes to the db db.setVerboseShell(flag) display extra information in shell output db.shutdownServer() db.stats() db.version() current version of the server > db.dropDatabase(); { "dropped" : "toto", "ok" : 1 } > |
8
数据还原
|
mongorestore -h localhost:27017 -d toto -directoryperdb F:/beifeng/toto -h:MongoDB所在server地址,比如:127.0.0.1,当然也能够指定port号:127.0.0.1:27017 -d:须要备份的数据库实例,比如test -o:备份的数据存放位置,当然该文件夹须要提前建立,在备份完毕后,系统自己主动在dump文件夹下建立一个test文件夹,这个文件夹里面存放数据库实例的备份数据。 |
|
C:\Users\to-to> connected to: localhost:27017 2014-10-15T23:19:11.071+0800 F:/beifeng/toto\c3.bson 2014-10-15T23:19:11.071+0800 2014-10-15T23:19:14.009+0800 2014-10-15T23:19:17.010+0800 2014-10-15T23:19:20.010+0800 2014-10-15T23:19:23.011+0800 2014-10-15T23:19:26.013+0800 2014-10-15T23:19:29.013+0800 2014-10-15T23:19:32.000+0800 2014-10-15T23:19:35.001+0800 1000000 objects found 2014-10-15T23:19:36.579+0800 2014-10-15T23:19:36.641+0800 "toto.c3" } 2014-10-15T23:19:41.440+0800 F:/beifeng/toto\cap1.bson 2014-10-15T23:19:41.440+0800 2014-10-15T23:19:41.440+0800 ped" : true, "size" : 4096, "max" : 100 } 53 objects found 2014-10-15T23:19:41.440+0800 2014-10-15T23:19:41.440+0800 F:/beifeng/toto\cap2.bson 2014-10-15T23:19:41.440+0800 2014-10-15T23:19:41.440+0800 ped" : true, "size" : 4096, "max" : 100 } file F:/beifeng/toto\cap2.bson empty, skipping 2014-10-15T23:19:41.456+0800 C:\Users\to-to>
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG90b3R1enVvcXVhbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt=""> |
9
导入导出:
用到的应用mongoexport,mongoimport
|
mongoexport –h dhost –d dbname –c collectionName –o output 參数说明: -h -d -c -o dname:表示要导出的数据库 collectionName:表示导出哪个集合 output:表示导出到的位置。 |
|
C:\Users\to-to>mongoexport -h localhost:27017 -d toto -c c3 -o f:/beifen/c3.txt connected to: localhost:27017 exported 1000000 records C:\Users\to-to> 相同能够数据导出到doc中 |
|
数据导入: mongoimport -h localhost:27017 -d toto -c ccc f:/beifen/c3.txt
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG90b3R1enVvcXVhbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt=""> C:\Users\to-to>mongo MongoDB shell version: 2.6.4 connecting to: test > use toto switched to db toto > show tables; c3 cap1 cap2 ccc system.indexes > db.ccc.find(); { "_id" : ObjectId("543e7473256769913d467e75"), "name" : "zhangsan", "age" : 1 } { "_id" : ObjectId("543e7473256769913d467e76"), "name" : "zhangsan", "age" : 2 } { "_id" : ObjectId("543e7473256769913d467e77"), "name" : "zhangsan", "age" : 3 } { "_id" : ObjectId("543e7473256769913d467e78"), "name" : "zhangsan", "age" : 4 } { "_id" : ObjectId("543e7473256769913d467e79"), "name" : "zhangsan", "age" : 5 } { "_id" : ObjectId("543e7473256769913d467e7a"), "name" : "zhangsan", "age" : 6 } { "_id" : ObjectId("543e7473256769913d467e7b"), "name" : "zhangsan", "age" : 7 } { "_id" : ObjectId("543e7473256769913d467e7c"), "name" : "zhangsan", "age" : 8 } { "_id" : ObjectId("543e7473256769913d467e7d"), "name" : "zhangsan", "age" : 9 } { "_id" : ObjectId("543e7473256769913d467e7e"), "name" : "zhangsan", "age" : 10 } { "_id" : ObjectId("543e7473256769913d467e7f"), "name" : "zhangsan", "age" : 11 } { "_id" : ObjectId("543e7473256769913d467e80"), "name" : "zhangsan", "age" : 12 } { "_id" : ObjectId("543e7473256769913d467e81"), "name" : "zhangsan", "age" : 13 } { "_id" : ObjectId("543e7473256769913d467e82"), "name" : "zhangsan", "age" : 14 } { "_id" : ObjectId("543e7473256769913d467e83"), "name" : "zhangsan", "age" : 15 } { "_id" : ObjectId("543e7473256769913d467e84"), "name" : "zhangsan", "age" : 16 } { "_id" : ObjectId("543e7473256769913d467e85"), "name" : "zhangsan", "age" : 17 } { "_id" : ObjectId("543e7473256769913d467e86"), "name" : "zhangsan", "age" : 18 } { "_id" : ObjectId("543e7473256769913d467e87"), "name" : "zhangsan", "age" : 19 } { "_id" : ObjectId("543e7473256769913d467e88"), "name" : "zhangsan", "age" : 20 } Type "it" for more > 上面自己主动隐式创建了一个ccc集合。 |
9 mongodb安全认证
每一个mongodb实例中的数据库都能够有很多用户,假设开启了安全性检查,仅仅有数据库认证用户才干运行读或者写操作。在认证的上下文中。MongoDB会将普通的数据作为admin
数据库处理。Admin数据库中的用户被视为超级用户(即:管理员)
在认证之后,管理员能够读写全部数据库,运行特定的管理员命令。运行listDatabase和shutdown.
在开启安全检查之前。一定要至少一个管理员账号。
最少得保证有一个管理员账号(admin
数据库其中的用户都是管理员)use admin
db.addUser(“username”,”password”);
2.有了管理员账号。就能够为其他的数据库分配用户。
2.1 首先要跳转到被分配的数据库
3.须要又一次启动mongodb服务,开启安全检查
4.接下来的client连接mongodb,须要登录才干运行对应的操作。
|
C:\Users\to-to>mongo localhost:27017/admin MongoDB shell version: 2.6.4 connecting to: localhost:27017/admin > db admin |
版权声明:本文博客原创文章,博客,未经同意,不得转载。
5.非关系数据库(Nosql)它mongodb:创建一个集合,导出和导入备份, 数据恢复,进出口的更多相关文章
- PCB MongoDB 数据库 Collection集合导出与导入
由于一直以来用微软可视化图形界面习惯了,而MongoDB是命令式操作,而用系统自带CMD操作不方便, 这里介绍一款CMD的替代品,大小100多M. Cmder工具下载 https://github. ...
- 为MongoDB创建一个Windows服务
一:选型,根据机器的操作系统类型来选择合适的版本,使用下面的命令行查询机器的操作系统版本 wmic os get osarchitecture 二:下载并安装 附上下载链接 点击安装包,我这里是把文件 ...
- 模板模式创建一个poi导出功能
之前的导出都很乱,直接写在代码中,等到下回还使用导出功能时又不知如何下手,今天用模板模式重写了一个导出功能,方便以后使用: package com.sf.addrCheck.util.export.p ...
- 使用 puppeteer 创建一个自动化导出 PDF 的服务
最近在基于 RAP2 做内网的一个 API 管理平台,涉及到与外部人员进行协议交换,需要提供 PDF 文档. 在设置完成 CSS 后已经可以使用浏览器的打印功能实现导出 PDF,但全手动,总是觉得不爽 ...
- 非默认安装目录下mysql数据的导出与导入
系统:Centos 6.5 1.首先确定msyql是否安装以及安装目录: [root@localhost ~]# service mysqld status mysqld (pid ) 正在运行... ...
- MongoDB一个基于分布式文件存储的数据库(介于关系数据库和非关系数据库之间的数据库)
1:MongoDB的官方网址:https://www.mongodb.com MongoDB的中文社区:http://www.mongoing.com/(老外也很看重中国市场啊,知道大家英语不好, ...
- NoSql非关系型数据库之MongoDB应用(三):MongoDB在项目中的初步应用
业精于勤,荒于嬉:行成于思,毁于随. 我们可以结合相关的IDE做一个简单的增删改查了,实现MongoDB在项目中的初步应用. 前提是安装了MongoDB服务和MongoDB可视化工具,没有安装的可以点 ...
- MongoDB 表(集合) 创建删除、数据增删改查
MongoDB 表(集合) 创建删除和增删改查数据 创建一个集合(emp) 在创建集合之前先使用use xxx,选择数据库,如果没有会创建(并不是真正的创建,只有在数据库里面保存集合数据之后才能够真正 ...
- MongoDB创建数据库和集合命令db.createCollection详解(转)
切换/创建数据库 use yourDB; 当创建一个集合(table)的时候会自动创建当前数据库 完整的命令如下:db.createCollection(name, {capped: <Boo ...
随机推荐
- u-boot: Error: Can't overwrite "ethaddr"
When try to execute following command, It reports error as following: --->8--- U-Boot> setenv ...
- 使用C++名单在文档处理和学生成绩管理系统相结合
对于学生成绩管理系统,我并不陌生,几乎学习C人的语言.做项目会想到学生成绩管理系统,我也不例外.在研究中的一段时间C语言之后,还用C语言到学生管理系统,然后做几个链接.计数,这个系统是以前的系统上的改 ...
- 《HBase权威指南》读书笔记----简介
工作中要使用HBase,刚刚开始接触HBase,理解不深,只是记录一下 . HBase基于google的bigtable论文实现,属于nosql. 几个概念: (1)列(column):最基本单位为列 ...
- ffplay for mfc 代码备忘录
在上传一个开源播放器项目ffplay for mfc.它会ffmpeg工程ffplay媒体播放器(ffplay.c)移植到VC环境,而使用MFC做一套接口.它可以完成一个播放器播放的基本流程的视频:解 ...
- Linux Kernel(Android) 加密算法汇总(四)-应用程序调用OpenSSL加密演算法
Linux Kernel(Android) 加密算法总结(三)-应用程序调用内核加密算法接口 讲到了怎样调用内核中的接口的方法. 本节主要是介绍怎样Android C/C++应用程序调用Openssl ...
- Android含文档server结束(client UI接口异步请求的一部分)三
在本文中,AsyncTask为了实现异步请求,详细代码如下所示的: public class downloadActivity extends Activity { private TextView ...
- 3g自己主动更新网卡驱动web完架构文档
几年前写. 看它是否是用得上 1 简单介绍 本文档具体描写叙述了基于ASP.NET平台和IIS服务的T-Mobile自己主动更新系统的实现框架. 本文档主要从技术架构和业务架构两个方面来着手来描写叙 ...
- iOS开展-clang: error: unknown argument: '-websockets'解决方案
问题: 昨天莫名其妙Xcode自己主动升级,那么今天之前执行project什么时候,不知怎的,他们都获得了. 错误内容: clang: error: unknown argument: '-webso ...
- 深入理解Javascript闭包概念
一.变量的作用域 要理解闭包,首先必须理解Javascript特殊的变量作用域. 变量的作用域无非就是两种:全局变量和局部变量. Javascript语言的特殊之处,就在于函数内部能够直接读取全局变量 ...
- Robotium调用getActivity()导致程序挂起的方法
1. 问题背景的叙述性说明 需要直接用在工作中没有项目的源代码robotium测试目标android平台launcher,该平台的基础上,当前日期的版本号android 4.4.2.之前我用来验证的可 ...