一、mongodb的冷备
mongodb的冷备就是:复制库的相关文件。因此在冷备前,要关闭服务器,本全中使用平滑关闭server的命令。
>use admin
>db.shutdownServer()
或者可以通过fsync方式使MongoDB将数据写入缓存中,然后再复制备份
>use admin
>db.runCommand({"fsync":1,"lock":1})
锁库后执行插入数据命令,发现无任何反应。备份完后,要解锁(防止这个时候停电或其它原因,导致未缓存中的数据丢失)。
>use admin
>db.$cmd.sys.unlock.findOne()
>db.currentOp()
如果currentOp 只返回{"inprog":[]}结果,说明解锁成功。 解锁后,数据才会成功写入数据库。
解锁也可以使用以下命令:
>use admin
>db.fsyncUnlock()
二、mongodb的热备
可以在不停止服务的情况下,使用MongoDB提供的两个工具来实现备份和恢复。这个两个工具在MongoDB的bin目录下可以看到:mongodump/mongorestor
1、常用命令格
mongodump -h IP --port PORT -u UserName -p Password -d DB_name -c Collection_Name -o BackupPath
如果不指定用户,可以去掉-u和-p;省略-h,默认导出本机的数据库;省略- -port默认端口27017;省略-d默认导出所有数据库。
2、导出所有数据库
[[email protected] mongodb]# mongodump -h 127.0.0.1 -o /home/user01/backup/
3、导出指定数据库
[[email protected] mongodb]# mongodump -h 192.168.1.108 -d tank -o /home/user01/backup
三、mongorestore还原数据库
1、常用命令格式
mongorestore -h IP --port PORT -u UserName -p Password -d DB_Name -c Collection_Name --drop BackupPath_Or_FileName
--drop的意思是,先删除所有的记录,然后恢复。
2、恢复所有数据库到mongodb中
[[email protected] mongodb]# mongorestore /home/user01/backup/ #这里的路径是所有库的备份路径
3、还原指定的数据库
[[email protected] mongodb]# mongorestore -d tank /home/user01/backup/tank/ #tank这个数据库的备份路径
[[email protected] mongodb]# mongorestore -d tank_new /home/user01/backup/tank/ #将tank还有tank_new数据库中
这二个命令,可以实现数据库的备份与还原,文件格式是json和bson的。无法指写到表备份或者还原。
四、mongoexport导出表,或者表中部分字段
1、常用命令格式
mongoexport -h IP --port 端口 -u 用户名 -p 密码 -d 数据库 -c 表名 -f 字段 -q 条件导出 --csv -o 文件名
上面的参数好理解,重点说一下:
-f 导出指字段,以字号分割,-f name,email,age导出name,email,age这三个字段
-q 可以根查询条件导出,-q ‘{ "uid" : "100" }‘ 导出uid为100的数据
--csv 表示导出的文件格式为csv的,这个比较有用,因为大部分的关系型数据库都是支持csv,在这里有共同点
2、导出整张表
[[email protected] mongodb]# mongoexport -d tank -c users -o /home/user01/backup/tank/users.dat
3、导出表中部分字段
[[email protected] mongodb]# mongoexport -d tank -c users --csv -f uid,name,sex -o tank/users.csv
4、根据条件敢出数据
[[email protected] mongodb]# mongoexport -d tank -c users -q ‘{uid:{$gt:1}}‘ -o tank/users.json
五、mongoimport导入表,或者表中部分字段
1、常用命令格式
1),还原整表导出的非csv文件
mongoimport -h IP --port 端口 -u 用户名 -p 密码 -d 数据库 -c 表名 --upsert --drop 文件名
重点说一下--upsert,其他参数上面的命令已有提到,--upsert 插入或者更新现有数据
2),还原部分字段的导出文件
mongoimport -h IP --port 端口 -u 用户名 -p 密码 -d 数据库 -c 表名 --upsertFields 字段 --drop 文件名
--upsertFields根--upsert一样
3),还原导出的csv文件
mongoimport -h IP --port 端口 -u 用户名 -p 密码 -d 数据库 -c 表名 --type 类型 --headerline --upsert --drop 文件名
上面三种情况,还可以有其他排列组合的。
2、还原导出的表数据
[[email protected] mongodb]# mongoimport -d tank -c users --upsert tank/users.dat
3、部分字段的表数据导入
[[email protected] mongodb]# mongoimport -d tank -c users --upsertFields uid,name,sex tank/users.dat
4、还原csv文件
[[email protected] mongodb]# mongoimport -d tank -c users --type csv --headerline --file tank/users.csv
六、实例:
[[email protected] ~]# service mongod start
Starting mongod: [确定]
[[email protected] ~]# mongo localhost:27027
MongoDB shell version: 3.2.8
connecting to: localhost:27027/test
Server has startup warnings:
> use admin
switched to db admin
> db.createUser(
... {user:"admin",pwd:"123456",
... roles:[{role:"root",db:"admin"}]
... })
Successfully added user: {
"user" : "admin",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]
}
> use person
switched to db person
> db.p1.insert({name:"thompson",gender:"male",age:"24"})
WriteResult({ "nInserted" : 1 })
> db.p1.find()
{ "_id" : ObjectId("57a2a28aa6d4803a1c952529"), "name" : "thompson", "gender" : "male", "age" : "24" }
> exit
bye
[[email protected] ~]# mongo localhost:27027
MongoDB shell version: 3.2.8
connecting to: localhost:27027/test
> show dbs;
admin 0.000GB
local 0.000GB
person 0.000GB
> exit
bye
[[email protected] ~]# vim /etc/mongod.conf
[[email protected] ~]# sed -n ‘32,33p‘ /etc/mongod.conf 需要开启认证功能
security:
authorization: enabled
[[email protected] ~]# service mongod restart 设备完配置文件后必须重新启动才能生效
Stopping mongod: [确定]
Starting mongod: [确定]
[[email protected] ~]# mongo localhost:27027
MongoDB shell version: 3.2.8
connecting to: localhost:27027/test
> show dbs 如果未认证,系统提示错误
2016-08-04T10:06:08.491+0800 E QUERY [thread1] Error: listDatabases failed:{
"ok" : 0,
"errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }",
"code" : 13
} :
[email protected]/mongo/shell/utils.js:25:13
[email protected]/mongo/shell/mongo.js:62:1
[email protected]/mongo/shell/utils.js:761:19
[email protected]/mongo/shell/utils.js:651:15
@(shellhelp2):1:1
> use admin
switched to db admin
> db.auth("admin","123456") 认证
1
> use person
switched to db person
> db.createUser( 创建新用户
... {user:"person",pwd:"123",
... roles:[{role:"readWrite",db:"person"}]
... })
Successfully added user: {
"user" : "person",
"roles" : [
{
"role" : "readWrite",
"db" : "person"
}
]
}
> use admin
switched to db admin
> db.system.users.find()
{ "_id" : "admin.admin", "user" : "admin", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "KFiaKAkrDqCJ/H8uIIhwzA==", "storedKey" : "faWxuPj1hZ4jV3VhL9Z0zylBL0Y=", "serverKey" : "qYSi5BRZY/GPTuBeF60KCvB5dqg=" } }, "roles" : [ { "role" : "root", "db" : "admin" } ] }
{ "_id" : "person.person", "user" : "person", "db" : "person", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "0tRiioYKdcx+On3uXgR/Sw==", "storedKey" : "8M69xFSgqniSeU7uvLqpzaclECs=", "serverKey" : "Znu2x5fAzMgrMKlxpj2I//1lcWc=" } }, "roles" : [ { "role" : "readWrite", "db" : "person" } ] }
> exit
bye
[[email protected] ~]# mkdir backup
[[email protected] ~]# mongodump -h localhost --port 27027 -d person -o backup/ -u person -p 123
2016-08-04T10:09:36.701+0800 writing person.p1 to
2016-08-04T10:09:36.701+0800 done dumping person.p1 (1 document)
[[email protected] ~]# ls backup/person/
p1.bson p1.metadata.json
[[email protected] ~]#
[[email protected] ~]# mongo localhost:27027/admin -u admin -p
MongoDB shell version: 3.2.8
Enter password:
connecting to: localhost:27027/admin
> use person
switched to db person
> show collections
p1
> db.p1.drop()
true
> show collections
> exit
bye
[[email protected] ~]# mongorestore -h localhost --port 27027 -d person backup/person/ -u person -p 123
2016-08-04T10:11:42.234+0800 building a list of collections to restore from backup/person dir
2016-08-04T10:11:42.235+0800 reading metadata for person.p1 from backup/person/p1.metadata.json
2016-08-04T10:11:42.256+0800 restoring person.p1 from backup/person/p1.bson
2016-08-04T10:11:42.268+0800 restoring indexes for collection person.p1 from metadata
2016-08-04T10:11:42.268+0800 finished restoring person.p1 (1 document)
2016-08-04T10:11:42.268+0800 done
[[email protected] ~]#
[[email protected] ~]# mongo localhost:27027/person -u person -p 123
MongoDB shell version: 3.2.8
connecting to: localhost:27027/person
> show collections
p1
> db.p1.find()
{ "_id" : ObjectId("57a2a28aa6d4803a1c952529"), "name" : "thompson", "gender" : "male", "age" : "24" }
>
> use person
switched to db person
> db.grantRolesToUser("person",[{role:"dbAdmin",db:"person"}]) 为用户附加其它角色
> use admin
switched to db admin
> db.system.users.find()
{ "_id" : "admin.admin", "user" : "admin", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "KFiaKAkrDqCJ/H8uIIhwzA==", "storedKey" : "faWxuPj1hZ4jV3VhL9Z0zylBL0Y=", "serverKey" : "qYSi5BRZY/GPTuBeF60KCvB5dqg=" } }, "roles" : [ { "role" : "root", "db" : "admin" } ] }
{ "_id" : "person.person", "user" : "person", "db" : "person", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "0tRiioYKdcx+On3uXgR/Sw==", "storedKey" : "8M69xFSgqniSeU7uvLqpzaclECs=", "serverKey" : "Znu2x5fAzMgrMKlxpj2I//1lcWc=" } }, "roles" : [ { "role" : "dbAdmin", "db" : "person" }, { "role" : "readWrite", "db" : "person" } ] }
>
> use person
switched to db person
> db.revokeRolesFromUser("person",[{role:"dbAdmin",db:"person"}]) 用户角色回收
> use admin
switched to db admin
> db.system.users.find()
{ "_id" : "admin.admin", "user" : "admin", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "KFiaKAkrDqCJ/H8uIIhwzA==", "storedKey" : "faWxuPj1hZ4jV3VhL9Z0zylBL0Y=", "serverKey" : "qYSi5BRZY/GPTuBeF60KCvB5dqg=" } }, "roles" : [ { "role" : "root", "db" : "admin" } ] }
{ "_id" : "person.person", "user" : "person", "db" : "person", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "0tRiioYKdcx+On3uXgR/Sw==", "storedKey" : "8M69xFSgqniSeU7uvLqpzaclECs=", "serverKey" : "Znu2x5fAzMgrMKlxpj2I//1lcWc=" } }, "roles" : [ { "role" : "readWrite", "db" : "person" } ] }
> exit
bye
[[email protected] ~]# rm backup/* -rf
[[email protected] ~]# mongoexport -h localhost --port 27027 -u person -p 123 -d person -c p1 -o backup/person.p1.dat
2016-08-04T10:22:06.773+0800 connected to: localhost:27027
2016-08-04T10:22:06.773+0800 exported 1 record
[[email protected] ~]# mongoimport -h localhost --port 27027 -u person -p 123 -d person -c p2 --upsert backup/person.p1.dat
2016-08-04T10:25:16.414+0800 connected to: localhost:27027
2016-08-04T10:25:16.434+0800 imported 1 document
[[email protected] ~]# mongo localhost:27027/person -u person -p 123
MongoDB shell version: 3.2.8
connecting to: localhost:27027/person
> show collections
p1
p2
> db.p2.find()
{ "_id" : ObjectId("57a2a28aa6d4803a1c952529"), "name" : "thompson", "gender" : "male", "age" : "24" }
> db.p1.find()
{ "_id" : ObjectId("57a2a28aa6d4803a1c952529"), "name" : "thompson", "gender" : "male", "age" : "24" }
{ "_id" : ObjectId("57a2a9c43f2b617cfdd64c63"), "name" : "eric", "gender" : "female", "age" : 18 }
{ "_id" : ObjectId("57a2bfe38381eac036252b7c"), "name" : "test1", "gender" : "male", "age" : 20 }
> exit
bye
[[email protected] ~]# rm -rf backup/* ; ls backup/
[[email protected] ~]# mongodump -h localhost --port 27027 -d person -o backup/ -u person -p 123 备份时备份整个库中的所有表
2016-08-04T12:36:36.321+0800 writing person.p1 to
2016-08-04T12:36:36.321+0800 writing person.p2 to
2016-08-04T12:36:36.322+0800 done dumping person.p1 (3 documents)
2016-08-04T12:36:36.322+0800 done dumping person.p2 (1 document)
[[email protected] ~]# ls backup/
person
[[email protected] ~]# ls backup/person/
p1.bson p1.metadata.json p2.bson p2.metadata.json
[[email protected] ~]# mongo localhost:27027/person -u person -p 123
MongoDB shell version: 3.2.8
connecting to: localhost:27027/person
> db.p1.drop()
true
> db.p2.drop()
true
> show collections
> exit
bye
[[email protected] ~]# mongorestore -h localhost --port 27027 -d person -c p1 backup/person/p1.bson -u person -p 123
注:可以单独恢复指定的表
2016-08-04T12:38:55.541+0800 checking for collection data in backup/person/p1.bson
2016-08-04T12:38:55.541+0800 reading metadata for person.p1 from backup/person/p1.metadata.json
2016-08-04T12:38:55.560+0800 restoring person.p1 from backup/person/p1.bson
2016-08-04T12:38:55.639+0800 restoring indexes for collection person.p1 from metadata
2016-08-04T12:38:55.640+0800 finished restoring person.p1 (3 documents)
2016-08-04T12:38:55.641+0800 done
[[email protected] ~]# mongo localhost:27027/person -u person -p 123
MongoDB shell version: 3.2.8
connecting to: localhost:27027/person
> show tables
p1
> db.p1.find()
{ "_id" : ObjectId("57a2a9c43f2b617cfdd64c63"), "name" : "eric", "gender" : "female", "age" : 18 }
{ "_id" : ObjectId("57a2bfe38381eac036252b7c"), "name" : "test1", "gender" : "male", "age" : 20 }
{ "_id" : ObjectId("57a2a28aa6d4803a1c952529"), "name" : "thompson", "gender" : "male", "age" : "24" }
> exit
bye
[[email protected] ~]