mongo3.2.7环境的搭建和全库备份、恢复
来源:互联网 发布:mac os 10.10 u盘安装 编辑:程序博客网 时间:2024/05/04 22:14
创建管理账户
use admin
db.createUser(
{
user: "admin",
pwd: "admin",
roles:
[ { role: "dbAdminAnyDatabase",db: "admin" },
{ role: "userAdminAnyDatabase",db: "admin" }
]
}
)
创建业务库和业务用户
use videoau
db.createUser(
{
user: "vduser",
pwd: "vduser",
roles: [ { role: "dbOwner", db: "videoau" } ]
}
)
use videoau
for (var i = 0; i < 10; i++) db.hosts.save({host : "host" + i, age : i})
for (var i = 0; i < 10; i++) db.names.save({user : "host" + i, age : i})
> db.hosts.find()
{ "_id" : ObjectId("57d6697446af58a09fd19b8d"), "host" : "host0", "age" : 0 }
{ "_id" : ObjectId("57d6697446af58a09fd19b8e"), "host" : "host1", "age" : 1 }
{ "_id" : ObjectId("57d6697446af58a09fd19b8f"), "host" : "host2", "age" : 2 }
{ "_id" : ObjectId("57d6697446af58a09fd19b90"), "host" : "host3", "age" : 3 }
{ "_id" : ObjectId("57d6697446af58a09fd19b91"), "host" : "host4", "age" : 4 }
{ "_id" : ObjectId("57d6697446af58a09fd19b92"), "host" : "host5", "age" : 5 }
{ "_id" : ObjectId("57d6697446af58a09fd19b93"), "host" : "host6", "age" : 6 }
{ "_id" : ObjectId("57d6697446af58a09fd19b94"), "host" : "host7", "age" : 7 }
{ "_id" : ObjectId("57d6697446af58a09fd19b95"), "host" : "host8", "age" : 8 }
{ "_id" : ObjectId("57d6697446af58a09fd19b96"), "host" : "host9", "age" : 9 }
启动Mongodb
不启用认证
mongod -f /etc/mongod.conf
启用认证
mongod -f /etc/mongod.conf --auth
关闭mongodb
mongod --shutdown -f /etc/mongod.conf
登陆mongodb
不启用认证
mongo --host 192.168.80.68
–rest # 开启简单的rest API
启用认证
mongo --host 192.168.80.68 admin -u admin -p
mongo --host 192.168.80.68 videoau -u vduser -p
全库备份
mkdir -p /mogoback
chown -R mongod:mongod /mogoback
mongodump --host 192.168.80.68 --port 27017 -u admin -p admin --out /mogoback
备份的时候报错
[root@instance ~]# mongodump --host 192.168.80.68 --port 27017 -u admin -p admin --out /mogoback
2016-09-12T16:42:26.319+0800 Failed: error counting videoau.names: not authorized on videoau to execute command { count: "names", query: {} }
如果不使用auth启动
[root@instance ~]# mongod -f /etc/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 3716
child process started successfully, parent exiting
[root@instance ~]# mongodump --host 192.168.80.68 --port 27017 -u admin -p admin --out /mogoback
2016-09-12T16:43:07.133+0800 writing admin.system.users to
2016-09-12T16:43:07.134+0800 done dumping admin.system.users (2 documents)
2016-09-12T16:43:07.134+0800 writing admin.system.version to
2016-09-12T16:43:07.135+0800 done dumping admin.system.version (1 document)
2016-09-12T16:43:07.135+0800 writing videoau.names to
2016-09-12T16:43:07.135+0800 writing videoau.hosts to
2016-09-12T16:43:07.136+0800 done dumping videoau.hosts (10 documents)
2016-09-12T16:43:07.136+0800 done dumping videoau.names (10 documents)
备份成功
开启认证备份需要增加参数
加参数:
--authenticationDatabase
mongodump --host 192.168.80.68 --port 27017 -u admin -p admin --authenticationDatabase videoau --out /mogoback
mongodump --host 192.168.80.68 -d videoau -o /mogoback/all_bak_20150912.bak -u admin -p=admin --authenticationDatabase admin
2016-09-12T16:55:07.200+0800 Failed: error counting videoau.names: not authorized on videoau to execute command { count: "names", query: {} }
mongodump --host 192.168.80.68 -d videoau -o /mogoback/all_bak_20150912.bak -u vduser -p=vduser --authenticationDatabase admin
2016-09-12T16:55:35.900+0800 Failed: error connecting to db server: server returned error on SASL authentication step: Authentication failed.
原来admin账户没有backup和restore的权限
{role: 'backup',db: 'admin'}
#授予角色:db.grantRolesToUser( "userName" , [ { role: "<role>", db: "<database>" } ])
#取消角色:db.grantRolesToUser( "userName" , [ { role: "<role>", db: "<database>" } ])
db.grantRolesToUser( "admin" , [ { role: "backup", db: "admin" } ])
db.grantRolesToUser( "admin" , [ { role: "restore", db: "admin" } ])
再次备份
[root@instance videoau]# mongodump --host 192.168.80.68 -d videoau -o /mogoback/all_bak_20150912.bak -u admin -p=admin --authenticationDatabase admin
2016-09-12T17:04:44.029+0800 writing videoau.names to
2016-09-12T17:04:44.031+0800 writing videoau.hosts to
2016-09-12T17:04:44.031+0800 done dumping videoau.names (10 documents)
2016-09-12T17:04:44.032+0800 done dumping videoau.hosts (10 documents)
[root@instance videoau]#
[root@instance videoau]# mongodump --host 192.168.80.68 -o /mogoback/all_bak_20150912.bak -u admin -p=admin --authenticationDatabase admin
2016-09-12T17:05:01.891+0800 writing admin.system.users to
2016-09-12T17:05:01.892+0800 done dumping admin.system.users (2 documents)
2016-09-12T17:05:01.892+0800 writing admin.system.version to
2016-09-12T17:05:01.893+0800 done dumping admin.system.version (1 document)
2016-09-12T17:05:01.893+0800 writing videoau.names to
2016-09-12T17:05:01.894+0800 writing videoau.hosts to
2016-09-12T17:05:01.895+0800 done dumping videoau.hosts (10 documents)
2016-09-12T17:05:01.895+0800 done dumping videoau.names (10 documents)
恢复测试
删除数据库
dropDatabase() 方法
>use videoau
switched to db videoau
>db.dropDatabase()
>{ "dropped" : "videoau", "ok" : 1 }
>
不加authenticationDatabase参数会报错
mongorestore -d videoau --drop /mogoback/all_bak_20150912.bak/
[root@instance all_bak_20150912.bak]# mongorestore -d videoau --drop /mogoback/all_bak_20150912.bak/
2016-09-12T17:12:27.210+0800 building a list of collections to restore from /mogoback/all_bak_20150912.bak dir
2016-09-12T17:12:27.210+0800 don't know what to do with subdirectory "all_bak_20150912.bak/admin", skipping...
2016-09-12T17:12:27.210+0800 don't know what to do with subdirectory "all_bak_20150912.bak/videoau", skipping...
2016-09-12T17:12:27.210+0800 done
增加authenticationDatabase参数
mongorestore -d videoau --drop /mogoback/all_bak_20150912.bak/videoau -u admin -p=admin --authenticationDatabase admin
[root@instance all_bak_20150912.bak]# mongorestore -d videoau --drop /mogoback/all_bak_20150912.bak/videoau -u admin -p=admin --authenticationDatabase admin
2016-09-12T17:23:42.786+0800 building a list of collections to restore from /mogoback/all_bak_20150912.bak/videoau dir
2016-09-12T17:23:42.787+0800 reading metadata for videoau.names from /mogoback/all_bak_20150912.bak/videoau/names.metadata.json
2016-09-12T17:23:42.809+0800 restoring videoau.names from /mogoback/all_bak_20150912.bak/videoau/names.bson
2016-09-12T17:23:42.815+0800 reading metadata for videoau.hosts from /mogoback/all_bak_20150912.bak/videoau/hosts.metadata.json
2016-09-12T17:23:42.824+0800 restoring indexes for collection videoau.names from metadata
2016-09-12T17:23:42.826+0800 restoring videoau.hosts from /mogoback/all_bak_20150912.bak/videoau/hosts.bson
2016-09-12T17:23:42.826+0800 finished restoring videoau.names (10 documents)
2016-09-12T17:23:42.828+0800 restoring indexes for collection videoau.hosts from metadata
2016-09-12T17:23:42.829+0800 finished restoring videoau.hosts (10 documents)
use admin
db.createUser(
{
user: "admin",
pwd: "admin",
roles:
[ { role: "dbAdminAnyDatabase",db: "admin" },
{ role: "userAdminAnyDatabase",db: "admin" }
]
}
)
创建业务库和业务用户
use videoau
db.createUser(
{
user: "vduser",
pwd: "vduser",
roles: [ { role: "dbOwner", db: "videoau" } ]
}
)
use videoau
for (var i = 0; i < 10; i++) db.hosts.save({host : "host" + i, age : i})
for (var i = 0; i < 10; i++) db.names.save({user : "host" + i, age : i})
> db.hosts.find()
{ "_id" : ObjectId("57d6697446af58a09fd19b8d"), "host" : "host0", "age" : 0 }
{ "_id" : ObjectId("57d6697446af58a09fd19b8e"), "host" : "host1", "age" : 1 }
{ "_id" : ObjectId("57d6697446af58a09fd19b8f"), "host" : "host2", "age" : 2 }
{ "_id" : ObjectId("57d6697446af58a09fd19b90"), "host" : "host3", "age" : 3 }
{ "_id" : ObjectId("57d6697446af58a09fd19b91"), "host" : "host4", "age" : 4 }
{ "_id" : ObjectId("57d6697446af58a09fd19b92"), "host" : "host5", "age" : 5 }
{ "_id" : ObjectId("57d6697446af58a09fd19b93"), "host" : "host6", "age" : 6 }
{ "_id" : ObjectId("57d6697446af58a09fd19b94"), "host" : "host7", "age" : 7 }
{ "_id" : ObjectId("57d6697446af58a09fd19b95"), "host" : "host8", "age" : 8 }
{ "_id" : ObjectId("57d6697446af58a09fd19b96"), "host" : "host9", "age" : 9 }
启动Mongodb
不启用认证
mongod -f /etc/mongod.conf
启用认证
mongod -f /etc/mongod.conf --auth
关闭mongodb
mongod --shutdown -f /etc/mongod.conf
登陆mongodb
不启用认证
mongo --host 192.168.80.68
–rest # 开启简单的rest API
启用认证
mongo --host 192.168.80.68 admin -u admin -p
mongo --host 192.168.80.68 videoau -u vduser -p
全库备份
mkdir -p /mogoback
chown -R mongod:mongod /mogoback
mongodump --host 192.168.80.68 --port 27017 -u admin -p admin --out /mogoback
备份的时候报错
[root@instance ~]# mongodump --host 192.168.80.68 --port 27017 -u admin -p admin --out /mogoback
2016-09-12T16:42:26.319+0800 Failed: error counting videoau.names: not authorized on videoau to execute command { count: "names", query: {} }
如果不使用auth启动
[root@instance ~]# mongod -f /etc/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 3716
child process started successfully, parent exiting
[root@instance ~]# mongodump --host 192.168.80.68 --port 27017 -u admin -p admin --out /mogoback
2016-09-12T16:43:07.133+0800 writing admin.system.users to
2016-09-12T16:43:07.134+0800 done dumping admin.system.users (2 documents)
2016-09-12T16:43:07.134+0800 writing admin.system.version to
2016-09-12T16:43:07.135+0800 done dumping admin.system.version (1 document)
2016-09-12T16:43:07.135+0800 writing videoau.names to
2016-09-12T16:43:07.135+0800 writing videoau.hosts to
2016-09-12T16:43:07.136+0800 done dumping videoau.hosts (10 documents)
2016-09-12T16:43:07.136+0800 done dumping videoau.names (10 documents)
备份成功
开启认证备份需要增加参数
加参数:
--authenticationDatabase
mongodump --host 192.168.80.68 --port 27017 -u admin -p admin --authenticationDatabase videoau --out /mogoback
mongodump --host 192.168.80.68 -d videoau -o /mogoback/all_bak_20150912.bak -u admin -p=admin --authenticationDatabase admin
2016-09-12T16:55:07.200+0800 Failed: error counting videoau.names: not authorized on videoau to execute command { count: "names", query: {} }
mongodump --host 192.168.80.68 -d videoau -o /mogoback/all_bak_20150912.bak -u vduser -p=vduser --authenticationDatabase admin
2016-09-12T16:55:35.900+0800 Failed: error connecting to db server: server returned error on SASL authentication step: Authentication failed.
原来admin账户没有backup和restore的权限
{role: 'backup',db: 'admin'}
#授予角色:db.grantRolesToUser( "userName" , [ { role: "<role>", db: "<database>" } ])
#取消角色:db.grantRolesToUser( "userName" , [ { role: "<role>", db: "<database>" } ])
db.grantRolesToUser( "admin" , [ { role: "backup", db: "admin" } ])
db.grantRolesToUser( "admin" , [ { role: "restore", db: "admin" } ])
再次备份
[root@instance videoau]# mongodump --host 192.168.80.68 -d videoau -o /mogoback/all_bak_20150912.bak -u admin -p=admin --authenticationDatabase admin
2016-09-12T17:04:44.029+0800 writing videoau.names to
2016-09-12T17:04:44.031+0800 writing videoau.hosts to
2016-09-12T17:04:44.031+0800 done dumping videoau.names (10 documents)
2016-09-12T17:04:44.032+0800 done dumping videoau.hosts (10 documents)
[root@instance videoau]#
[root@instance videoau]# mongodump --host 192.168.80.68 -o /mogoback/all_bak_20150912.bak -u admin -p=admin --authenticationDatabase admin
2016-09-12T17:05:01.891+0800 writing admin.system.users to
2016-09-12T17:05:01.892+0800 done dumping admin.system.users (2 documents)
2016-09-12T17:05:01.892+0800 writing admin.system.version to
2016-09-12T17:05:01.893+0800 done dumping admin.system.version (1 document)
2016-09-12T17:05:01.893+0800 writing videoau.names to
2016-09-12T17:05:01.894+0800 writing videoau.hosts to
2016-09-12T17:05:01.895+0800 done dumping videoau.hosts (10 documents)
2016-09-12T17:05:01.895+0800 done dumping videoau.names (10 documents)
恢复测试
删除数据库
dropDatabase() 方法
>use videoau
switched to db videoau
>db.dropDatabase()
>{ "dropped" : "videoau", "ok" : 1 }
>
不加authenticationDatabase参数会报错
mongorestore -d videoau --drop /mogoback/all_bak_20150912.bak/
[root@instance all_bak_20150912.bak]# mongorestore -d videoau --drop /mogoback/all_bak_20150912.bak/
2016-09-12T17:12:27.210+0800 building a list of collections to restore from /mogoback/all_bak_20150912.bak dir
2016-09-12T17:12:27.210+0800 don't know what to do with subdirectory "all_bak_20150912.bak/admin", skipping...
2016-09-12T17:12:27.210+0800 don't know what to do with subdirectory "all_bak_20150912.bak/videoau", skipping...
2016-09-12T17:12:27.210+0800 done
增加authenticationDatabase参数
mongorestore -d videoau --drop /mogoback/all_bak_20150912.bak/videoau -u admin -p=admin --authenticationDatabase admin
[root@instance all_bak_20150912.bak]# mongorestore -d videoau --drop /mogoback/all_bak_20150912.bak/videoau -u admin -p=admin --authenticationDatabase admin
2016-09-12T17:23:42.786+0800 building a list of collections to restore from /mogoback/all_bak_20150912.bak/videoau dir
2016-09-12T17:23:42.787+0800 reading metadata for videoau.names from /mogoback/all_bak_20150912.bak/videoau/names.metadata.json
2016-09-12T17:23:42.809+0800 restoring videoau.names from /mogoback/all_bak_20150912.bak/videoau/names.bson
2016-09-12T17:23:42.815+0800 reading metadata for videoau.hosts from /mogoback/all_bak_20150912.bak/videoau/hosts.metadata.json
2016-09-12T17:23:42.824+0800 restoring indexes for collection videoau.names from metadata
2016-09-12T17:23:42.826+0800 restoring videoau.hosts from /mogoback/all_bak_20150912.bak/videoau/hosts.bson
2016-09-12T17:23:42.826+0800 finished restoring videoau.names (10 documents)
2016-09-12T17:23:42.828+0800 restoring indexes for collection videoau.hosts from metadata
2016-09-12T17:23:42.829+0800 finished restoring videoau.hosts (10 documents)
2016-09-12T17:23:42.829+0800 done
mongostat --host 192.168.80.68 --username admin --password admin --authenticationDatabase admin
0 0
- mongo3.2.7环境的搭建和全库备份、恢复
- mysql 完成的全库备份和恢复
- RMAN的备份与恢复-全备份
- mongodb的导出导入备份和恢复(全)
- oracle11g简单的物理全备份和恢复
- Oracle 数据库全量备份恢复和部分备份恢复
- Oracle 数据库全量备份恢复和部分备份恢复
- Linux 系统全备份和恢复
- rman管理的全库备份与恢复&用户管理的全库的备份与恢复
- 备份和恢复IBM_WAS_4.0.x 的运行环境。
- rman全库备份,恢复过程
- innobackupex全库增量备份与恢复
- mongodb全库备份与恢复
- RMAN的备份与恢复(1)-全备份
- Oracle备份/恢复案例03--全库恢复
- oracle备份之rman_全库备份恢复
- MySQL的备份和恢复
- oracle的备份和恢复
- 支持向量机
- Redis 跨域登录 ------- 待更~~~!!!
- 哈夫曼编码(Add All,uva 10954)
- C#与单片机通信中遇到的数据类型转换
- java基础(五)Hashcode的作用
- mongo3.2.7环境的搭建和全库备份、恢复
- PHP 设计模式
- Java集合中基于hash的容器
- HTML5表单的创建以及和php交互
- android 拖拽裁剪
- swift学习笔记 --- Closures
- ButterKnife失效的解决办法
- 10 种机器学习算法的要点(附 Python 和 R 代码)
- MySQL添加字段和修改字段的方法