mongoDB的基本操作以及数据的导入导出,备份和恢复

来源:互联网 发布:mac百度搜索历史 编辑:程序博客网 时间:2024/06/16 04:45

mongoDB关于数据库的操作

> db.help();  ------DB methods      


User相关的基本操作:

> show users                        ------查看所有用户

> db.system.users.find()              ------查看用户列表

> db.addUser('name','pwd')                           ------增加或修改用户密码

> db.addUser("userName", "pwd123", true)      ------添加用户、设置密码、是否只读

> db.auth("userName", "123123")                  ------数据库认证、安全模式

> db.removeUser('name')                               ------根据用户名删除用户

Database相关的基本操作:

> show dbs                                                                           ------查看所有数据库

> use [db-name]                                                                  ------当创建一个集合(table)的时候会自动创建当前数据库,这个指令相当于mysql的use [database-name]

> db.dropDatabase()                                                          ------删除当前的数据库

> db.repairDatabase()                                                        ------修复数据库

> db.copyDatabase('mail_addr','mail_addr_tmp')         -----拷贝数据库

> db.copyDatabase("mydb", "temp", "127.0.0.1")          ------将本机的mydb的数据复制到temp数据库中

> db.cloneDatabase(“127.0.0.1”)                                     ------将指定机器上的数据库的数据克隆到当前数据库

Collection相关的基本操作:

> show collections                                                             ------查看所有的集合 

> db.printReplicationInfo()                                             ------查看主从复制状态

> db.mail_addr.drop()                                                     ------删除collection(mail_addr 是collections的名字)

> db.createCollection(“collectionName”, {size: 20, capped: 5, max: 100})            ------创建一个聚集集合

> db.getCollection("account")                                        ------得到指定名称的聚集集合

> db.getCollectionNames()                                             ------得到当前db的所有聚集集合

> db.printCollectionStats()                                              ------查看各collection的状态

其它

> db.getPrevError()                                                          ------查询之前的错误信息
> db.resetError()                                                               ------清除错误记录

> db.user.help();             -----DBCollection help   (此处的user表示的是我的collection的名字)


查看聚集集合的基本信息(下面出现的test表示collection的名字)

> db.test.count();                  ------查询当前集合的数据条数
> db.test.dataSIze();                                                   ------查询数据空间大小
> db.test.getDB();                                                       ------得到当前聚集集合所在的database
> db.test.stats();                                                          ------得到当前聚集的状态
> db.test.totalSize();                                                ------得到聚集集合总大小
> db.test.storageSize();                                             ------得到聚集集合储存空间大小
> db.test.getShardVersion();                                    ------Shard版本信息
> db.test.renameCollection("users");                     ------将集合test重名为users
> db.test.drop();                                                          ------删除当前聚集集合

mongoDB数据的导入和导出

  Mongodb中的mongoexport工具可以把一个collection导出成JSON格式或CSV格式的文件。可以通过参数指定导出的数据项,也可以根据指定的条件导出数据。
  导出命令选项说明: 

  • -h:指明数据库宿主机的IP
  • -u:指明数据库的用户名
  • -p:指明数据库的密码
  • -d:指明数据库的名字
  • -c:指明collection的名字
  • -f:指明要导出那些列
  • -o:指明到要导出的文件名
  • -q:指明导出数据的过滤条件
user@user-xubuntu:/usr/lib/mongodb/bin$ sudo ./mongoexport -d wx_connect -c template -o template.txt[sudo] password for user: connected to: 127.0.0.1exported 28 records
     在导出数据时没有显示指定导出样式 ,默认导出了JSON格式的数据。如果我们需要导出CSV格式的数据,则需要使用--csv参数。导出的位置在/usr/lib/mongodb/bin下面,可以自己指定路径
user@user-xubuntu:/usr/lib/mongodb/bin$ sudo ./mongoexport -d wx_connect -c template --csv -o template_csv.datconnected to: 127.0.0.1csv mode requires a field list                     ------第一次没有指明要导出的列,所以只是实现一个空的文件user@user-xubuntu:/usr/lib/mongodb/bin$ sudo ./mongoexport -d wx_connect -c template --csv -f msgId,templateId,status,toUser -o template_csv_new.datconnected to: 127.0.0.1exported 28 records                           ------导出成功
     Mongodb中的mongoimport工具可以把一个特定格式文件中的内容导入到指定的collection中。该工具可以导入JSON格式数据,也可以导入CSV格式数据
     导入命令选项说明:  
  • -h:指明数据库宿主机的IP
  • -u:指明数据库的用户名
  • -p:指明数据库的密码
  • -d:指明数据库的名字
  • -c:指明collection的名字
  • -f:指明要导入那些列
  删除数据库中的数据(删除的时候可能会报remove needs a query at src/mongo/shell/collection.js:299这个错误,查阅相关资料没有找到答案。解决的方法:直接删除collections,然后在导数据,或者是安装mongoDB时指定数据data的路径,登陆mongo服务器端的时候也指定路径,这样应该不会出现删数据的时候需要去查询。第二种方法有待考证!)
> db.template.remove();
> db.template.drop();true                      ------删除聚集集合collection
      导入数据(导入数据的时候会隐式创建表结构)
user@user-xubuntu:/usr/lib/mongodb/bin$ sudo ./mongoimport -d wx_connect -c template  -o template.txtconnected to: 127.0.0.1imported 28 records 
   上面演示的是导入JSON格式的文件中的内容,如果要导入CSV格式文件中的内容,则需要通过--type参数指定导入格式(CSV 格式良好,主流数据库都支持导出为CSV 的格式,所以这种格式非常利于异构数据迁移)
user@user-xubuntu:/usr/lib/mongodb/bin$ sudo ./mongoexport -d wx_connect -c template --type csv --headerline --file template_csv_new.datconnected to: 127.0.0.1exported 28 records

  参数说明:

 -type:指明要导入的文件格式

 -headerline:指明第一行是列名,不需要导入

 -file:指明要导入的文件

MongoDB的数据备份和恢复

   用mongodump 来做MongoDB 的库或表级别的备份

      备份选项说明(sudo ./mongodump  --help):

  • -h:指明数据库宿主机的IP
  • -u:指明数据库的用户名
  • -p:指明数据库的密码
  • -d:指明数据库的名字
  • -c:指明collection的名字
  • -o:输出目录
  • -q:json query(json查询)

user@user-xubuntu:/usr/lib/mongodb/bin$ sudo ./mongodump -d wx_connect -c templateconnected to: 127.0.0.12014-11-07T11:05:45.473+0800 DATABASE: wx_connect to dump/wx_connect2014-11-07T11:05:45.474+0800 wx_connect.template to dump/wx_connect/template.bson2014-11-07T11:05:45.475+0800  28 documents2014-11-07T11:05:45.475+0800 Metadata for wx_connect.template to dump/wx_connect/template.metadata.json
        这个备份操作备份的是wx_connect数据库下面的一个collections,这个collection的名字是template,如果不写-c template,那么备份的就是整个数据库wx_connect. 默认的备份路径就是dump

user@user-xubuntu:/usr/lib/mongodb/bin$ cd dumpuser@user-xubuntu:/usr/lib/mongodb/bin/dump$ lswx_connectuser@user-xubuntu:/usr/lib/mongodb/bin/dump$ cd wx_connect/user@user-xubuntu:/usr/lib/mongodb/bin/dump/wx_connect$ lstemplate.bson  template.metadata.json                     ------备份之后会生成两个文件
  用mongorestore 来做MongoDB 的库或表级别的数据恢复

    恢复选项说明(sudo ./mongorestore  --help):

  • -u:指明数据库的用户名
  • -p:指明数据库的密码
  • -d:指明数据库的名字
  • -c:指明collection的名字        

user@user-xubuntu:/usr/lib/mongodb/bin$ sudo ./mongorestore -d wx_connect dump/wx_connect   //wx_connect是数据库的名字,dump/wx_connect 用来做恢复的文件路径 [sudo] password for user: connected to: 127.0.0.12014-11-07T12:04:55.412+0800 dump/wx_connect/template.bson2014-11-07T12:04:55.413+0800 going into namespace [wx_connect.template]28 objects found2014-11-07T12:04:55.416+0800 Creating index: { key: { _id: 1 }, name: "_id_", ns: "wx_connect.template" }
       经验证数据已经恢复。

0 0
原创粉丝点击