### 备份方式和恢复方式
备份全部数据库
mkdir /bak
mongodump
备份指定数据库
mkidr /bak
mongodump -d admin
备份一个数据库中的某个集合
mkdir /bak
mongodump -d admin -c student
恢复全部数据库
mongorestore --drop # --drop是为了防止数据重复
恢复某个数据库
mongorestore -d dbname --drop
恢复某个数据库的某个集合
mongorestore -d dbname -c student --drop
异机数据恢复
mongorestore -h host -d dbname /path/sourcefile # 注意这里要使用dump出来的文件的源文件
mongodump
-h 导出源
-d 要导出的数据库名称
-o 数据库要导出的位置
mongorestore
-d 使用的数据库名称
-c 恢复一个表
### 数据的导出
mongodb中的mongoexport工具可以把一个collection导出成json格式或csv格式的文件。可以通过参数指定导出的数据项,也可以根据指定的条件导出数据。
mongoexport
参数说明:
-h:指明数据库宿主机的ip
-u:指明数据库的用户名
-p:指明数据库的密码
-d:指明数据库的名字
-c:指明collection的名字
-f:指明要导出那些列
-o:指明到要导出的文件名
-q:指明导出数据的过滤条件
### 数据的导入
mongoimport
-type 指明要导入的文件格式
-headerline 指明不导入第一行
-file 指明要导入的文件路径
参数说明:
-h:指明数据库宿主机的ip
-u:指明数据库的用户名
-p:指明数据库的密码
-d:指明数据库的名字
-c:指明collection的名字
-f:指明要导入那些列
csv 格式良好,主流数据库都支持导出为csv 的格式,所以这种格式非常利于异构数据迁移
### 演示备份数据库
mkdir /bakcd /bak[root@redis bak]# mongodump -h 192.168.58.30 --port 27017 -u root -pconnected to: 192.168.58.30:27017enter password: thu jul 17 08:15:42.996 all dbsthu jul 17 08:15:43.006 database: admin to dump/adminthu jul 17 08:15:43.015 admin.system.indexes to dump/admin/system.indexes.bsonthu jul 17 08:15:43.076 2 objectsthu jul 17 08:15:43.077 admin.system.users to dump/admin/system.users.bsonthu jul 17 08:15:43.077 1 objectsthu jul 17 08:15:43.077 metadata for admin.system.users to dump/admin/system.users.metadata.jsonthu jul 17 08:15:43.078 database: guest to dump/guestthu jul 17 08:15:43.082 guest.system.indexes to dump/guest/system.indexes.bsonthu jul 17 08:15:43.088 3 objectsthu jul 17 08:15:43.088 guest.system.users to dump/guest/system.users.bsonthu jul 17 08:15:43.097 1 objectsthu jul 17 08:15:43.097 metadata for guest.system.users to dump/guest/system.users.metadata.jsonthu jul 17 08:15:43.098 guest.student to dump/guest/student.bsonthu jul 17 08:15:43.103 26 objectsthu jul 17 08:15:43.103 metadata for guest.student to dump/guest/student.metadata.json[root@redis bak]# lltotal 4drwxr-xr-x 4 root root 4096 jul 17 08:15 dump[root@redis bak]# cd dump/[root@redis dump]# lsadmin guest[root@redis dump]#
###恢复数据库
由于刚开始的时候指定了备份目录下面的所有文件,所以会报错
[root@localhost ~]# [root@localhost ~]# mongorestore -d admin admin/*error: too many positional optionsimport bson files into mongodb.后来从新换了一个,再次报错
[root@localhost ~]# mongorestore -d admin admin/ couldn't connect to [127.0.0.1] couldn't connect to server 127.0.0.1:27017上面报错是因为我配置文件中写的是本机ip,更换后
[root@localhost ~]# mongorestore -d admin admin/ -h 192.168.58.10 connected to: 192.168.58.10thu jul 17 09:08:33.873 admin/system.users.bsonthu jul 17 09:08:33.873 going into namespace [admin.system.users]1 objects foundthu jul 17 09:08:33.902 creating index: { key: { _id: 1 }, ns: admin.system.users, name: _id_ }thu jul 17 09:08:34.043 creating index: { key: { user: 1, usersource: 1 }, unique: true, ns: admin.system.users, name: user_1_usersource_1 }[root@localhost ~]#
### 查看是否正确导入
[root@localhost ~]# mongo 192.168.58.10mongodb shell version: 2.4.6connecting to: 192.168.58.10/testwelcome to the mongodb shell.for interactive help, type help.for more comprehensive documentation, seequestions? try the support group> show dbslthu jul 17 09:12:34.806 don't know how to show [dbsl] at src/mongo/shell/utils.js:847> show dbs;admin0.203125gbguest0.203125gblocal0.078125gb> use guestswitched to db guest> show collections;studentsystem.indexessystem.users> db.student.find(){ _id : objectid(53c769621d184866a15043ec), name : zhuima }{ _id : objectid(53c769861d184866a15043ed), x : 1 }{ _id : objectid(53c769861d184866a15043ee), x : 2 }{ _id : objectid(53c769861d184866a15043ef), x : 3 }{ _id : objectid(53c769861d184866a15043f0), x : 4 }{ _id : objectid(53c769861d184866a15043f1), x : 5 }{ _id : objectid(53c769861d184866a15043f2), x : 6 }{ _id : objectid(53c769861d184866a15043f3), x : 7 }{ _id : objectid(53c769861d184866a15043f4), x : 8 }{ _id : objectid(53c769861d184866a15043f5), x : 9 }{ _id : objectid(53c769861d184866a15043f6), x : 10 }{ _id : objectid(53c769861d184866a15043f7), x : 11 }{ _id : objectid(53c769861d184866a15043f8), x : 12 }{ _id : objectid(53c769861d184866a15043f9), x : 13 }{ _id : objectid(53c769861d184866a15043fa), x : 14 }{ _id : objectid(53c769861d184866a15043fb), x : 15 }{ _id : objectid(53c769861d184866a15043fc), x : 16 }{ _id : objectid(53c769861d184866a15043fd), x : 17 }{ _id : objectid(53c769861d184866a15043fe), x : 18 }{ _id : objectid(53c769861d184866a15043ff), x : 19 }type it for more>
### 用户管理
添加超级用户
use admin
db.adduser('name','password')
添加只读用户
use dbname
db.adduser('name','password',true)
添加普通用户
use dbname
db.adduser('name','password') 其实也不能说是普通用户,,这个只是针对于某一个数据库有权限
删除用户
use dbname
db.system.users.remove('name','password')
更换用户密码
use dbname
db.adduser('name','password')
切换用户(必须要先到对对应的数据库,root账户除外)
use dbname
db.auth('name','password')
查找用户
use dbname
db.system.users.find()
### 监控mongodb
