5.非关系数据库(Nosql)它mongodb:创建一个集合,导出和导入备份, 数据恢复,进出口

本文涉及的产品
云数据库 MongoDB,独享型 2核8GB
推荐场景:
构建全方位客户视图
简介:

固定集合

固定集合值得是事先创建并且大小固定的集合

固定集合的特征:固定集合非常像环形队列。假设空间不足,最早文档就会被删除,为新的文档腾出空间。一般来说。固定集合适用于不论什么想要自己主动淘汰过期属性的场景,没有太多的操作限制。

创建固定集合使用命令:

db.createCollection(“collectionName”,{capped:true,size:100000,max:100});

size:指定集合大小,单位为KB,max指定文档的数量

当指定文档数量上限时,必须同一时候指定大小。

淘汰机制仅仅有在容量还没有满时才会根据文档数量来工作。要是容量满了,淘汰机制根据容量来工作。

创建一个集合:

>db.createCollection("cap1",{capped:true,size:1000,max:100});

{ "ok" : 1 }

插入数据

> for(var i=1;i<=100;i++){

...db.cap1.insert({name:"dongxue",age:i});

... }

WriteResult({ "nInserted" : 1 })

> db.cap1.find().count();

53   (大小之所以是53是由于大小超过了1000

固定集合的应用场景:聊天记录。日志信息

      淘汰机制:当满足size指定集合大小。不能再继续往固定集合中加数据。

      固定集合的容量优先

当文档达到100条时,再加入的时候会替换先前的

 

备份与导入导出。

MongoDB提供了备份和回复的功能,各自是MongoDB下载文件夹下的mongodump.exemongorestore.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:  表示要备份到的文件文件夹

运行后的效果图:

另外启动一个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 }

数据还原

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> 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>

 

 

导入导出:

用到的应用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数据库中的用户被视为超级用户(即:管理员)

在认证之后,管理员能够读写全部数据库,运行特定的管理员命令。运行listDatabaseshutdown.

在开启安全检查之前。一定要至少一个管理员账号。

  1. 最少得保证有一个管理员账号(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

 

版权声明:本文博客原创文章,博客,未经同意,不得转载。












本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/4668741.html,如需转载请自行联系原作者


相关文章
|
2月前
|
NoSQL MongoDB 数据库
数据库数据恢复—MongoDB数据库数据恢复案例
MongoDB数据库数据恢复环境: 一台操作系统为Windows Server的虚拟机上部署MongoDB数据库。 MongoDB数据库故障: 工作人员在MongoDB服务仍然开启的情况下将MongoDB数据库文件拷贝到其他分区,数据复制完成后将MongoDB数据库原先所在的分区进行了格式化操作。 结果发现拷贝过去的数据无法使用。管理员又将数据拷贝回原始分区,MongoDB服务仍然无法使用,报错“Windows无法启动MongoDB服务(位于 本地计算机 上)错误1067:进程意外终止。”
|
2月前
|
缓存 NoSQL Linux
在CentOS 7系统中彻底移除MongoDB数据库的步骤
以上步骤完成后,MongoDB应该会从您的CentOS 7系统中被彻底移除。在执行上述操作前,请确保已经备份好所有重要数据以防丢失。这些步骤操作需要一些基本的Linux系统管理知识,若您对某一步骤不是非常清楚,请先进行必要的学习或咨询专业人士。在执行系统级操作时,推荐在实施前创建系统快照或备份,以便在出现问题时能够恢复到原先的状态。
257 79
|
2月前
|
存储 NoSQL MongoDB
MongoDB数据库详解-针对大型分布式项目采用的原因以及基础原理和发展-卓伊凡|贝贝|莉莉
MongoDB数据库详解-针对大型分布式项目采用的原因以及基础原理和发展-卓伊凡|贝贝|莉莉
149 8
MongoDB数据库详解-针对大型分布式项目采用的原因以及基础原理和发展-卓伊凡|贝贝|莉莉
|
1月前
|
运维 NoSQL 容灾
告别运维噩梦:手把手教你将自建 MongoDB 平滑迁移至云数据库
程序员为何逃离自建MongoDB?扩容困难、运维复杂、高可用性差成痛点。阿里云MongoDB提供分钟级扩容、自动诊断与高可用保障,助力企业高效运维、降本增效,实现数据库“无感运维”。
|
5月前
|
存储 关系型数据库 MySQL
利用Cron表达式实现MySQL数据库的定时备份
以上就是如何使用Cron表达式和mysqldump命令实现MySQL数据库的定时备份。这种方法的优点是简单易用,而且可以根据需要定制备份的时间和频率。但是,它也有一些限制,例如,它不能备份MySQL服务器的配置文件和用户账户信息,也不能实现增量备份。如果需要更复杂的备份策略,可能需要使用专门的备份工具或服务。
129 15
|
5月前
|
NoSQL MongoDB 数据库
数据库数据恢复——MongoDB数据库服务无法启动的数据恢复案例
MongoDB数据库数据恢复环境: 一台Windows Server操作系统虚拟机上部署MongoDB数据库。 MongoDB数据库故障: 管理员在未关闭MongoDB服务的情况下拷贝数据库文件。将MongoDB数据库文件拷贝到其他分区后,对MongoDB数据库所在原分区进行了格式化操作。格式化完成后将数据库文件拷回原分区,并重新启动MongoDB服务。发现服务无法启动并报错。
|
6月前
|
存储 NoSQL MongoDB
微服务——MongoDB常用命令1——数据库操作
本节介绍了 MongoDB 中数据库的选择、创建与删除操作。使用 `use 数据库名称` 可选择或创建数据库,若数据库不存在则自动创建。通过 `show dbs` 或 `show databases` 查看所有可访问的数据库,用 `db` 命令查看当前数据库。注意,集合仅在插入数据后才会真正创建。数据库命名需遵循 UTF-8 格式,避免特殊字符,长度不超过 64 字节,且部分名称如 `admin`、`local` 和 `config` 为系统保留。删除数据库可通过 `db.dropDatabase()` 实现,主要用于移除已持久化的数据库。
413 0
|
6月前
|
存储 NoSQL MongoDB
从 MongoDB 到 时序数据库 TDengine,沃太能源实现 18 倍写入性能提升
沃太能源是国内领先储能设备生产厂商,数十万储能终端遍布世界各地。此前使用 MongoDB 存储时序数据,但随着设备测点增加,MongoDB 在存储效率、写入性能、查询性能等方面暴露出短板。经过对比,沃太能源选择了专业时序数据库 TDengine,生产效能显著提升:整体上,数据压缩率超 10 倍、写入性能提升 18 倍,查询在特定场景上也实现了数倍的提升。同时减少了技术架构复杂度,实现了零代码数据接入。本文将对 TDengine 在沃太能源的应用情况进行详解。
311 0
|
7月前
|
存储 NoSQL MongoDB
数据库数据恢复—MongoDB数据库迁移过程中丢失文件的数据恢复案例
某单位一台MongoDB数据库由于业务需求进行了数据迁移,数据库迁移后提示:“Windows无法启动MongoDB服务(位于 本地计算机 上)错误1067:进程意外终止。”
|
9月前
|
存储 JSON NoSQL
学习 MongoDB:打开强大的数据库技术大门
MongoDB 是一个基于分布式文件存储的文档数据库,由 C++ 编写,旨在为 Web 应用提供可扩展的高性能数据存储解决方案。它与 MySQL 类似,但使用文档结构而非表结构。核心概念包括:数据库(Database)、集合(Collection)、文档(Document)和字段(Field)。MongoDB 使用 BSON 格式存储数据,支持多种数据类型,如字符串、整数、数组等,并通过二进制编码实现高效存储和传输。BSON 文档结构类似 JSON,但更紧凑,适合网络传输。
249 15

推荐镜像

更多