MongoDB日常维护主要包括数据库的备份恢复、安全管理和数据库状态监控
在MongoDB中备份管理包括导入导出、备份与恢复、复制数据库和克隆集合等操作
可以使用mongoexport 和 mongoimport 命令来导出导入 MongoDB 的数据
for(var i=1;i<=1000;i++){
db.users.insert({
'id':i,'name':'maomao'});}
而后使用db.users.find(),会发现只会出现20行,这是因为数据库默认只会出现20行
mongoexport -d test -c 表格 -o 储存位置/表名.json
[root@mongodb bin]# mongoexport -d test -c users -o /home/mao.json
2021-04-19T10:01:49.406-0400 connected to: localhost
2021-04-19T10:01:49.416-0400 exported 1000 records
[root@mongodb bin]# ls /home
mao.json maomao
导入数据后模拟数据表格丢失
> db.users.drop()
true
> show tables
comment
system.indexes
zhu
恢复数据
mongoimport -d 库名 -c 表名 --file json文件路径
[root@mongodb bin]# mongoimport -d test -c users --file /home/mao.json
2021-04-19T10:05:33.264-0400 connected to: localhost
2021-04-19T10:05:33.273-0400 imported 1000 documents
> db.users.findOne()
{
"_id" : ObjectId("607d8cdd5c684f0ac406a9ce"),
"id" : 2,
"name" : "maomao"
}
导出特定条件的数据(id大于500)
[root@mongodb bin]# mongoexport -d test -c users -q '{"id":{"$gt":500}}' -o /home/mao500.json
2021-04-19T10:11:35.345-0400 connected to: localhost
2021-04-19T10:11:35.349-0400 exported 500 records
[root@mongodb bin]# head -10 mao500.json
{
"_id":{
"$oid":"607d8cdd5c684f0ac406abc1"},"id":501.0,"name":"maomao"}
{
"_id":{
"$oid":"607d8cdd5c684f0ac406abc2"},"id":502.0,"name":"maomao"}
{
"_id":{
"$oid":"607d8cdd5c684f0ac406abc4"},"id":504.0,"name":"maomao"}
{
"_id":{
"$oid":"607d8cdd5c684f0ac406abc5"},"id":505.0,"name":"maomao"}
{
"_id":{
"$oid":"607d8cdd5c684f0ac406abc6"},"id":506.0,"name":"maomao"}
{
"_id":{
"$oid":"607d8cdd5c684f0ac406abc7"},"id":507.0,"name":"maomao"}
{
"_id":{
"$oid":"607d8cdd5c684f0ac406abc8"},"id":508.0,"name":"maomao"}
{
"_id":{
"$oid":"607d8cdd5c684f0ac406abc9"},"id":509.0,"name":"maomao"}
{
"_id":{
"$oid":"607d8cdd5c684f0ac406abca"},"id":510.0,"name":"maomao"}
{
"_id":{
"$oid":"607d8cdd5c684f0ac406abcb"},"id":511.0,"name":"maomao"}
[root@mongodb bin]# mongoimport -d mao -c users --file /home/mao500.json
测试
> use mao
switched to db mao
> show tables
system.indexes
users
> db.users.findOne()
{
"_id" : ObjectId("607d8cdd5c684f0ac406abc1"),
"id" : 501,
"name" : "maomao"
}
MongoDB中可以使用mongodump 命令来备份MongoDB 的数据,该命令可以导出所有数据到指定目录中,mongodump 命令可以通过参数指定导出的数据量级转存的服务器
mongodump -h dbhost -d dbname -o db备份路径
[root@mongodb home]# mkdir /home/backup
[root@mongodb home]# mongodump -h 127.0.0.1:27017 -d mao -o /home/backup/
2021-04-19T10:22:52.442-0400 writing mao.users to
2021-04-19T10:22:52.454-0400 done dumping mao.users (500 documents)
[root@mongodb home]# ll /home/backup/
total 0
drwxr-xr-x 2 root root 51 Apr 19 10:22 mao
[root@mongodb home]# cd backup/mao/
[root@mongodb mao]# ls
users.bson users.metadata.json
使用mongorestore命令来恢复备份的数据
mongostore -h dbhost -d dbname --dir=/数据路径
[root@mongodb mao]# mongorestore -h127.0.0.1:27017 -d mao1 --dir=/home/backup/mao/
2021-04-19T10:26:42.560-0400 the --db and --collection args should only be used when restoring from a BSON file. Other use
s are deprecated and will not exist in the future; use --nsInclude instead2021-04-19T10:26:42.560-0400 building a list of collections to restore from /home/backup/mao dir
2021-04-19T10:26:42.561-0400 reading metadata for mao1.users from /home/backup/mao/users.metadata.json
2021-04-19T10:26:42.645-0400 restoring mao1.users from /home/backup/mao/users.bson
2021-04-19T10:26:42.688-0400 no indexes to restore
2021-04-19T10:26:42.688-0400 finished restoring mao1.users (500 documents)
2021-04-19T10:26:42.688-0400 done
> show dbs
admin 0.078GB
local 0.078GB
mao 0.078GB
mao1 0.078GB # 还原的库
test 0.078GB
在MongoDB中可以将数据库中的集合进行克隆,这里将mao 数据库中的users集合克隆到另外一个实例
启动另外一个实例(instance),一个实例就是内存空间。
mongod -f /usr/local/mongodb/bin/mongodb2.conf
netstat -lnt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:27018 0.0.0.0:* LISTEN
mongo --port 27018 # 加端口号
克隆数据
> db.runCommand({
"cloneCollection":"mao.users","from":"127.0.0.1:27017"})
{
"ok" : 1 }
> show dbs
local 0.078GB
mao 0.078GB
> use mao
switched to db mao
> show tables
system.indexes
users
> db.users.findOne()
{
"_id" : ObjectId("607d8cdd5c684f0ac406abc1"),
"id" : 501,
"name" : "maomao"
MongoDB安全管理主要包括MongoDB 的安全访问控制和用户权限分配
修改配置文件
vim mongodb1.conf
添加
bind_ip=192.168.188.101
port = 27017
然后停止服务
mongod -f mongodb1.conf --shutdown
开启监控
方法一:命令行开启,临时,下次重启失效
mongod -f /usr/local/mongodb/bin/mongodb1.conf --httpinterface
[root@mongodb ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 192.168.188.101:27017 *:*
方法二:修改配置文件,永久生效
在配置文件下面添加
httpinterface=true
绑定ip和端口之后 登录必须加上 --host
mongo --host 192.168.188.101:27017
查看当前正在运行的进程
db.currentOp()
杀死高消耗的进程
db.killOp(268)
”小驼峰” killOp
“大驼峰” StudentOp
可以配置授权用户来访问MongoDB,启动时必须指定auth=true,否则授权不起作用
创建管理用户
db.createUser(
{
user: "用户名",
pwd: "密码",
roles: [ {
role: "root", db: "管理库名" } ]
}
)
> db.createUser(
... {
... user:'root',
... pwd:'123',
... roles:[{
role:'root',db:'admin'}]
... }
... )
Successfully added user: {
"user" : "root",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]
}
创建普通用户
db.createUser({
user:"xiaotian",pwd:"123",roles:[{
role:"readWrite",db:"mao"}]})
验证
> db.auth('xiaotian','123')
1
返回值为1说明验证成功
开启用户验证
vim mongodb1.conf
添加
auth=true
验证用户 方法1
mongod -f /usr/local/mongodb/bin/mongodb1.conf --shutdown
mongod -f /usr/local/mongodb/bin/mongodb1.conf
mongo --host 192.168.188.101:27017
> show dbs
2021-04-19T10:57:37.163-0400 E QUERY [thread1] Error: listDatabases failed:{
"ok" : 0,
"errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }",
"code" : 13,
"codeName" : "Unauthorized"
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:62:1
shellHelper.show@src/mongo/shell/utils.js:814:19
shellHelper@src/mongo/shell/utils.js:704:15
@(shellhelp2):1:1
> db.auth("root","123")
1
> show dbs
admin 0.078GB
local 0.078GB
mao 0.078GB
mao1 0.078GB
test 0.078GB
> use mao
switched to db mao
> db.auth("xiaotian","123")
1
> show tables;
system.indexes
users
验证用户 方法2
mongo -uroot -proot 192.168.100.101:27017/admin
show dbs
删除账号xiaotian
db.dropUser("xiaotian");
false
> use mao
switched to db mao
> db.dropUser("xiaotian");
true