创建管理账户
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