MongoDB数据库的导出和导入(Linux环境)

一.前言

MongoDB自带了mongodump和mongorestore这两个工具来实现对数据的备份和恢复。

mongodump能够在MongoDB运行时进行备份,它的工作原理是对运行的MongoDB做查询,然后将所有查到的文档写入磁盘。但是存在的问题时使用mongodump产生的备份不一定是数据库的实时快照,如果我们在备份时对数据库进行了写入操作,则备份出来的文件可能不完全和MongoDB实时数据相等。另外在备份时可能会对其它客户端性能产生不利的影响。

二.MongoDB数据库备份

1.语法

mongodump -h dbhost -d dbname -o dbdirectory

参数说明:
-h: MongoDB所在服务器地址,例如:127.0.0.1,可以指定端口号:127.0.0.1:3717
-d: 需要备份的数据库实例,例如:test
-o: 备份的数据存放位置,例如:/data/wwwroot/mongodump/,该目录需要提前建立,这个目录里面存放该数据库实例的备份数据。

帮助文档:

[root@localhost mongodb]# mongodump --help  
Export MongoDB data to BSON files.  
  
options:  
  --help                   produce help message  
  -v [ --verbose ]         be more verbose (include multiple times for more   
                           verbosity e.g. -vvvvv)  
  --version                print the program's version and exit  
  -h [ --host ] arg        mongo host to connect to ( <set name>/s1,s2 for   
                           sets)  
  --port arg               server port. Can also use --host hostname:port  
  --ipv6                   enable IPv6 support (disabled by default)  
  -u [ --username ] arg    username  
  -p [ --password ] arg    password  
  --dbpath arg             directly access mongod database files in the given   
                           path, instead of connecting to a mongod  server -   
                           needs to lock the data directory, so cannot be used   
                           if a mongod is currently accessing the same path  
  --directoryperdb         if dbpath specified, each db is in a separate   
                           directory  
  --journal                enable journaling  
  -d [ --db ] arg          database to use  
  -c [ --collection ] arg  collection to use (some commands)  
  -o [ --out ] arg (=dump) output directory or "-" for stdout  
  -q [ --query ] arg       json query  
  --oplog                  Use oplog for point-in-time snapshotting  
  --repair                 try to recover a crashed database  
  --forceTableScan         force a table scan (do not use $snapshot) 

详细参数说明:
-h:指明数据库宿主机的IP
-u:指明数据库的用户名
-p:指明数据库的密码
-d:指明数据库的名字
-c:指明collection的名字
-o:指明到要导出的文件名
-q:指明导出数据的过滤条件

导出的备份数据为一个以库名命名的文件夹,目录下面为各表的json数据文件

将备份数据打包,上传到需要导入数据的服务器

tar -zcvf filename.tar.gz  dirname/

2.实例

a.数据备份

mongodump -h 192.168.0.xxx:3717 -d rbac -o /data/wwwroot/mongodump/

导出的备份数据为一个以库名命名的文件夹,目录下面为各表的json数据文件
得到如图的数据备份文件
库名文件夹
库里的表数据
b.压缩数据

cd /data/wwwroot/mongodump/
tar -zcvf rbac.tar.gz  rbac/

c.将压缩文件传到目标恢复服务器

三.MongoDB数据库恢复

1.语法

mongorestore -h dbhost -d dbname --dir dbdirectory

参数说明:
-h: MongoDB所在服务器地址
-d: 需要恢复的数据库实例,例如:test,当然这个名称也可以和备份时候的不一样,比如test2
–dir: 备份数据所在位置,例如:/home/mongodump/itcast/
–drop: 恢复的时候,先删除当前数据,然后恢复备份的数据。就是说,恢复后,备份后添加修改的数据都会被删除,慎用!

帮助文档:

[root@localhost mongodb]# ./bin/mongorestore --help  
 usage: ./bin/mongorestore [options] [directory or filename to restore from]  
 options:  
   --help                  produce help message  
   -v [ --verbose ]        be more verbose (include multiple times for more   
                           verbosity e.g. -vvvvv)  
   --version               print the program s version and exit  
   -h [ --host ] arg       mongo host to connect to ( <set name>/s1,s2 for sets)  
   --port arg              server port. Can also use --host hostname:port  
   --ipv6                  enable IPv6 support (disabled by default)  
   -u [ --username ] arg   username  
   -p [ --password ] arg   password  
   --dbpath arg            directly access mongod database files in the given   
                           path, instead of connecting to a mongod  server -   
                           needs to lock the data directory, so cannot be used   
                           if a mongod is currently accessing the same path  
   --directoryperdb        if dbpath specified, each db is in a separate   
                           directory  
   --journal               enable journaling  
   -d [ --db ] arg         database to use  
   -c [ --collection ] arg collection to use (some commands)  
   --objcheck              validate object before inserting  
   --filter arg            filter to apply before inserting  
   --drop                  drop each collection before import  
   --oplogReplay           replay oplog for point-in-time restore  
   --keepIndexVersion      don't upgrade indexes to newest version  

详细参数说明:
-h:指明数据库宿主机的IP
-u:指明数据库的用户名
-p:指明数据库的密码
-d:指明数据库的名字
-c:指明collection的名字
-o:指明到要备份的文件名
-q:指明备份数据的过滤条件

将上传的数据压缩文件,解压缩

tar -zxvf filename.tar.gz

2.遇到问题

不指定管理员,会出现权限报错

Failed: rbac.action_log: error reading database: not authorized on rbac to execute command { listCollections: 1, cursor: { batchSize: 0 } }

查看管理员用户信息,roles中root具有最高权限

use admin
show users
{
  "_id": "admin.test1",
  "user": "test1",
  "db": "admin",
  "credentials": {
    "SCRAM-SHA-1": {
      "iterationCount": 10000,
      "salt": "abc==",
      "storedKey": "def=",
      "serverKey": "ghj="
    }
  },
  "roles": [
    {
      "role": "root",
      "db": "admin"
    }
  ]
}

3.实例

a.解压缩数据文件

cd /data/wwwroot/mongodump/
tar -zxvf rbac.tar.gz

b.数据恢复导入

mongorestore -h 192.168.175.xxo:3717 -u test1 -p 123456 --authenticationDatabase admin -d rbac --dir /data/wwwroot/mongodump/rbac

四.其他命令

1.连接MongoDB

mongo --host 192.168.0.xxx--port 3717 admin --username test1  --password 123456 --authenticationDatabase admin

2.查看所有数据库

show dbs;

3.从指定主机上克隆数据库

 db.copyDatabase(“127.0.0.1”); 

将指定机器上的数据库的数据复制到当前数据库

4.从指定的机器上复制指定数据库数据到某个数据库

 db.copyDatabase("product_test_db", "test_db", "172.20.65.xxx");

将远程172.20.65.xxx服务器中product_test_db数据库的数据复制到本机test_db数据库中

参考链接: https://www.cnblogs.com/meiya/p/7659531.html.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值