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 }
>
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所在服务器地址,例如:127.0.0.1,当然也可以指定端口号:127.0.0.1:27017,当然该目录需要提前创建,在备份完成后,系统自动在dump目录下建立一个beifeng目录,这个目录里面存放该数据库实例的备份数据。
mongodump -h localhost:27017 -d toto -o f:/beifeng
-h:用来指定要输出的数据库所在的ip地址和端口号
-d: 指定要备份的数据库
-o: 表示要备份到的文件目录
执行后的效果图:
7另外启动一个mongodb的客户端,目的是将数据库中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所在服务器地址,例如:127.0.0.1,当然也可以指定端口号:127.0.0.1:27017 -d:需要备份的数据库实例,例如toto -o:备份的数据存放位置,当然该目录需要提前建立,在备份完成后,系统自动在dump目录下建立一个test目录,这个目录里面存放数据库实例的备份数据。 |
C:\Users\to-to>mongorestore -h localhost:27017 -d toto -directoryperdb F:/beifeng/toto connected to: localhost:27017 2014-10-15T23:19:11.071+0800 F:/beifeng/toto\c3.bson 2014-10-15T23:19:11.071+0800 going into namespace [toto.c3] 2014-10-15T23:19:14.009+0800 Progress: 5740200/54000000 10% (bytes) 2014-10-15T23:19:17.010+0800 Progress: 10125000/54000000 18% (bytes) 2014-10-15T23:19:20.010+0800 Progress: 15660000/54000000 29% (bytes) 2014-10-15T23:19:23.011+0800 Progress: 22528800/54000000 41% (bytes) 2014-10-15T23:19:26.013+0800 Progress: 29586600/54000000 54% (bytes) 2014-10-15T23:19:29.013+0800 Progress: 36752400/54000000 68% (bytes) 2014-10-15T23:19:32.000+0800 Progress: 43372800/54000000 80% (bytes) 2014-10-15T23:19:35.001+0800 Progress: 50284800/54000000 93% (bytes) 1000000 objects found 2014-10-15T23:19:36.579+0800 Creating index: { key: { _id: 1 }, name: "_id_", ns: "toto.c3" } 2014-10-15T23:19:36.641+0800 Creating index: { unique: true, key: { age: 1 }, name: "age_1", ns: "toto.c3" } 2014-10-15T23:19:41.440+0800 F:/beifeng/toto\cap1.bson 2014-10-15T23:19:41.440+0800 going into namespace [toto.cap1] 2014-10-15T23:19:41.440+0800 Created collection toto.cap1 with options: { "create" : "cap1", "cap ped" : true, "size" : 4096, "max" : 100 } 53 objects found 2014-10-15T23:19:41.440+0800 Creating index: { key: { _id: 1 }, name: "_id_", ns: "toto.cap1" } 2014-10-15T23:19:41.440+0800 F:/beifeng/toto\cap2.bson 2014-10-15T23:19:41.440+0800 going into namespace [toto.cap2] 2014-10-15T23:19:41.440+0800 Created collection toto.cap2 with options: { "create" : "cap2", "cap ped" : true, "size" : 4096, "max" : 100 } file F:/beifeng/toto\cap2.bson empty, skipping 2014-10-15T23:19:41.456+0800 Creating index: { key: { _id: 1 }, name: "_id_", ns: "toto.cap2" } C:\Users\to-to> |
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 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.接下来的客户端连接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:创建集合,备份与导入导出, 数据还原,导入导出相关推荐
- 非关系型数据库NoSQL的崛起
非关系型数据库NoSQL的崛起 <连线>杂志网络版近日刊载文章,对NoSQL(非关系型数据库)的来源与历史进行了追溯.文章主要介绍了最古老的NoSQL数据库之一CouchDB,这种数据库的 ...
- 关系型数据库sql与非关系型数据库nosql
一.什么是非关系型数据库? 1.非关系型数据库就是区别于关系型数据库的数据库. 2.虽然说起来有点绕嘴,但是非关系型数据库就是因为,关系型数据库处理不了的一些读写问题而产生的数据库 3.我们常用的非关 ...
- Java面试题 非关系型数据库nosql
四:非关系型数据库(nosql) 1:Redis (1)Redis介绍 是一个高性能的(key/value)分布式内存数据库,基于内存运行并支持内存的NoSql数据库之一,数据结构服务器. (2)特点 ...
- 常见非关系型数据库(NoSQL)推荐介绍
2019独角兽企业重金招聘Python工程师标准>>> 随着互联网web2.0网站的兴起,非关系型的数据库现在成了一个极其热门的新领域, 非关系数据库产品的发展非常迅速.而传统的关系 ...
- 常见的几个非关系型数据库(NoSQL)、非关系型和关系型的区别
目录 非关系型数据库的特点 -------------------------------. 1.关系型数据库 ------------------------------------ 2. 关系型 ...
- 非关系型数据库-NoSQL(Redis)
文章目录 一.什么是 NoSQL? 二.NoSQL 的特性? 三.什么是 Redis? 四.Redis 安装部署 4.1 下载安装包 4.2 编译安装 4.3 修改配置文件 4.4 启动服务 4.5 ...
- 关系型数据库MySql与非关系型数据库NoSql
云计算背后的秘密:NoSQL诞生的原因和优缺点 我本来一直觉得NoSQL其实很容易理解的,我本身也已经对NoSQL有了非常深入的研究,但是在最近准备YunTable的Chart的时候,发现NoSQL不 ...
- 关系型数据库(Relational Database)与非关系型数据库(NoSQL)的区别:(MySQL,Redis,Memcache,MongoDB)
Table of Contents 关系型数据库(Relational Database) 什么是关系数据库 什么是SQL? 关系数据库的结构 关系模型 关系数据库的好处 数据一致性 隔离性和原子性 ...
- mysql同步大师_数据库大师成长日记:您最需要了解的NoSQL非关系型数据库
朋友们,我们平时使用的数据库,大多都是关系型数据库,包括MySQL.PostgreSQL.SQLServer等.关系型数据库的特点是数据的存储通过二维表格实现,将数据存储在相互独立的表格中. 近年来, ...
- MySQL数据库是非关系_MySQL(数据库)基础知识、关系型数据库yu非关系型数据库、连接认证...
什么是数据库? 数据库(Database):存储数据的仓库 高效地存储和处理数据的介质(介质主要是两种:磁盘和内存) 数据库系统:DBS(Database System):是一种虚拟系统,将多种内容关 ...
最新文章
- python代码写好了怎么运行视频-Python的初学者你现在可以自己“看”到代码的运行了!...
- CentOS上安装skype
- 深入理解XGBoost:分布式实现
- 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型...
- [渝粤教育] 西南科技大学 线性代数 在线考试复习资料
- sql 循环处理数据_图文介绍 SQL 的三种查询计划处理模型,Spark 用了其中两个
- u3d_shader_surface_shader_1
- 1.9编程基础之顺序查找02:输出最高分数的学生姓名
- opencv 一种灰度图像增强方式
- Bootstrap Bootstrap表格插件bootstrap-table配置与应用小结
- VC++的Unicode编程(经典之作,交流传薪)
- 河南双轨制直销系统开发推荐奖介绍
- 利用PPT要怎样制作文字弹幕特效
- Android四大组件之广播
- 调用企业微信接口注意事项
- JDK8 Windows系统中Java HotSpot虚拟机配置参数
- mysql左连接查询举例_mysql左右连接查询(有示例图)
- 为什么你在互联网上搞不到钱?
- iMX6UL lvgl开发备忘
- box-shadow四个边框设置阴影样式
热门文章
- MATLAB机器学习系列-7支持向量机原理及其代码实现
- VTK:创建字母频率的条形图用法实战
- boost::mp11::mp_transform_q相关用法的测试程序
- boost::mp11::mp_less相关用法的测试程序
- boost::filesystem模块打印文件状态的测试程序
- GDCM:gdcm::DefinedTerms的测试程序
- boost::core::typeinfo的用法实例
- boost::core模块default_allocator
- Boost:post process后期处理的测试程序
- ITK:将高斯噪声添加到特定图像