0
点赞
收藏
分享

微信扫一扫

Mongodb 数据库的简单使用(二)

一、 Mongodb 的认证

一、启用认证

# 默认情况下,mongodb是没有使用认证功能的,在配置文件中添加 auth=true 开启认证功能,开启后要重启下 Mongodb

[root@test220 ~]# /usr/local/mongodb/bin/mongod --shutdown -f /usr/local/mongodb/config/mongodb.conf

[root@test220 ~]# echo "auth=true" >> /usr/local/mongodb/config/mongodb.conf

[root@test220 ~]# numactl --interleave=all /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/config/mongodb.conf

二、配置账号进行验证

# 设置两个用户账号 admin 、 testuser 用来测试

[root@test220 ~]# mongo 10.6.2.220:27017

> 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 test
switched to db test

> db.createUser({user:"testuser",pwd:"123456",roles:[{ role:"readWrite", db:"test" }
] })
Successfully added user: {
"user" : "testuser",
"roles" : [
{
"role" : "readWrite",
"db" : "test"
}
]
}

三、进行验证登录

# 登录两种验证方法,第一种是登录时验证,第二种是在mongodb的shell控制台验证

1、第一种方法

# 语法格式: mongo -u 用户名 -p 用户密码 数据库服务器地址:端口号/数据库名

[root@test220 ~]# mongo -u admin -p 123456 10.6.2.220:27017/admin
[root@test220 ~]# mongo -u testuser -p 123456 10.6.2.220:27017/test

2、第二种方法

[root@test220 ~]# mongo 10.6.2.220:27017 ## 貌似登入进去了,实则啥也干不了
> use admin
switched to db admin
> show dbs # 此时还没认证,就算是 admin 用户也无法查看任何信息
> db.auth("admin","123456")
1 # 返回为 1 就是验证通过
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB

> use test
switched to db test
> show dbs # 这里报错是提示没有权限查看,上面定义的是 test 库读写权限
E QUERY [js] uncaught exception: Error: Failed to acquire database information from privileges

> db.auth("testuser","123111") # 验证错误的情况,下面返回值为 0
Error: Authentication failed.
0

> db.auth("testuser","123456")
1

> db.class.insert({"name":"zhangsan"}) # 在test库内 可以写入数据、查询数据
WriteResult({ "nInserted" : 1 })

> db.class.find()
{ "_id" : ObjectId("61dd25b187f250afc6d4f3e2"), "name" : "zhangsan" }

四、修改用户登录口令

# 两种修改方法

1、第一种方法: db.changeUserPassword("用户名","新密码")

终端 A :
[root@test220 ~]# mongo 10.6.2.220:27017
> use admin
switched to db admin
> db.auth("rose","123123") # 使用原账号口令验证通过
1

终端 B :
> use admin
switched to db admin
> db.changeUserPassword("rose","abc123") # 更改原账号口令

终端 A 验证:
> use admin
switched to db admin
> db.auth("rose","123123") # 原账号口令登录验证不通过
Error: Authentication failed.
0
> db.auth("rose","abc123") # 新的账号口令登录验证通过
1

2、第二种方法: db.updateUser("用户名",{pwd:"新密码"})

终端 B :
> use admin
switched to db admin
> db.updateUser("rose",{pwd:"123456"})

终端 A :
[root@test220 ~]# mongo 10.6.2.220:27017
> use admin
switched to db admin

> db.auth("rose","123123")
Erro: Authentication failed.
0

> db.auth("rose","123456")
1

五、删除用户

> use admin
switched to db admin

> db.dropUser("rose")
true

二、Mongodb 的权限


一、mongodb 角色和权限

# role:
数据库用户角色:read、readwrite
数据库管理角色:dbAdmin、dbOwner、userAdmin
集群管理角色:clusterAdmin、clusterManager、clusterMonitor、hostManager
备份恢复角色:backup、restore
跨数据库角色:readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、dbAdminAnyDatabase
超级用户角色:root
内部角色:__system

# 常用权限
read: 允许用户读取指定数据库
readwrite: 允许用户读写指定数据库
userAdmin: 允许用户向system.users集合写入,可以在指定数据库里创建、删除和管理用户
dbAdmin: 允许用户在指定数据库中执行管理函数,如索引创建、删除,查看统计或访问

readAnyDatabase: 只在admin数据库中可用 赋予用户所有数据库的读权限
readWriteAnyDatabase: 只在admin数据库中可用 赋予用户所有数据库的读写权限
userAdminAnyDatabase: 只在admin数据库中可用 赋予用户所有数据库的userAdmin权限
dbAdminAnyDatabase: 只在admin数据库中可用 赋予用户所有数据库的dbAdmin权限

root: 只在admin数据库中可用 超级账号,超级权限
clusterAdmin: 只在admin数据库中可用 赋予用户所有分片和复制集相关函数的管理权限

二、关于权限的理解

1、mongodb是没有默认管理员账号,所以要先添加管理员账号,在开启权限认证。
2、切换到admin数据库,添加的账号才是管理员账号。
3、用户只能在用户所在数据库登录,包括管理员账号。
4、管理员可以管理所有数据库,但是不能直接管理其他数据库,要先在admin数据库认证后才可以

三、配置用户权限
操作步骤:
1、切换到换到相关数据库下
2、添加账号(账号、密码、角色)

配置说明: 1、添加管理员用户账号,切换到admin 数据库下操作
2、添加普通用户账号,切换到相应的数据库下操作
3、roles 里面可以指定多个权限和多个数据库对象
4、customData 存放一些用户相关的自定义数据库,是可选项,其他的都是必须的选项

添加用户语法:
db.createUser({user:"xxx",pwd:"xxx",customData: { <any information> },roles:
[{role:"xxx",db:"xxx"}]})
{ 添加的用户名 用户密码 用户角色:{具体那个角色,授权的数据库库名 }

示例:
[root@test220 ~]# mongo 10.6.2.220:27017

> use test
switched to db test

> db.createUser({user:"testuser",pwd:"123456",roles:[{ role:"readWrite", db:"test" }
] })
Successfully added user: {
"user" : "testuser",
"roles" : [
{
"role" : "readWrite",
"db" : "test"
}
]
}

四、登录验证
建议另外开启一个终端进行测试

[root@test220 ~]# mongo 10.6.2.220:27017 # 貌似登入进去了,实则啥也干不了

> use admin
switched to db admin
> show dbs # 此时还没认证,就算是 admin 用户也无法查看
> db.auth("admin","123456")
1 # 返回为 1 就是验证通过
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB

> use test
switched to db test
> show dbs # 这里报错是提示没有权限查看,上面定义的是 test 库读写权限
E QUERY [js] uncaught exception: Error: Failed to acquire database information from privileges

> db.auth("testuser","123111") # 验证错误的情况,下面返回值为 0
Error: Authentication failed.
0

> db.auth("testuser","123456")
1

> show users # 没有show 权限
E QUERY [js] uncaught exception: Error: logical sessions can not have multiple authenticated users

> db.class.insert({"name":"zhangsan"}) # 在test库内 可以写入数据、查询数据
WriteResult({ "nInserted" : 1 })
> db.class.find()
{ "_id" : ObjectId("61dd25b187f250afc6d4f3e2"), "name" : "zhangsan" }

五、更新权限

# 更新用户涉及到 ---- 新角色权限、删除用户

1、添加权限示例

1) 创建一个用户 rose ,设置 test 库的 readWrite 权限

# 打开一个终端,这里且称为 --- 终端 A
> use admin
switched to db admin
> db
admin

> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
test 0.000GB
testmore 0.000GB

> db.createUser({user:"rose",pwd:"123456",roles:[{ role:"readWrite", db:"test" }] })
Successfully added user: {
"user" : "rose",
"roles" : [
{
"role" : "readWrite",
"db" : "test"
}
]
}

2)查看验证下上面设置的权限
# 另开一个终端查看 这里称之为--- 终端 B

> use admin
switched to db admin
> db.auth("admin","123456")
1

> show users
{
"_id" : "admin.rose",
"userId" : UUID("339082ab-10f8-44ca-a759-b218eab64614"),
"user" : "rose",
"db" : "admin",
"roles" : [
{
"role" : "readWrite",
"db" : "test"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}

2、更改权限示例
1)更改权限

# 更改权限分为两种情况:
一种是在以前权限上增加其他的权限
另外一种是将现在的权限更改为另外一种权限

# 增加权限使用命令: db.grantRolesToUser
# 更改权限使用命令: db.updateUser

2) 增加权限示例:
终端 A:
> db.grantRolesToUser("rose",[{role:"readWrite",db:"testmore"}])

终端 B:
> show users
{
"_id" : "admin.rose",
"userId" : UUID("339082ab-10f8-44ca-a759-b218eab64614"),
"user" : "rose",
"db" : "admin",
"roles" : [
{
"role" : "readWrite",
"db" : "test"
},
{
"role" : "readWrite",
"db" : "testmore"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}
# 这里可以看到用户 rose 现在的权限是 对两个库都有读写权限了

3) 更改上面的 rose 权限

终端 A:
> updateUser("rose",{pwd:'123456',roles:[{role:"read",db:"test"}]})

终端 B:
> show users
{
"_id" : "admin.rose",
"userId" : UUID("339082ab-10f8-44ca-a759-b218eab64614"),
"user" : "rose",
"db" : "admin",
"roles" : [
{
"role" : "read",
"db" : "test"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}
# 发现 rose 对 test 库的权限变了,并且原来对 testmore 库的权限没有了,说明 update是权限取代

# 假如上述更改权限命令换成
# db.updateUser("rose",{pwd:'123123',roles:[{role:"read",db:"test"}]})
开启第三终端登录验证下,会发现此时 rose 的密码也随之修改了,这也算是 updateUser 的另一功能吧

3、删除权限示例:
删除 rose 对 test 库的权限

终端 A :
> db.revokeRolesFromUser("rose",[{role:"readWrite",db:"test"}])

终端 B:
> show users
{
"_id" : "admin.rose",
"userId" : UUID("339082ab-10f8-44ca-a759-b218eab64614"),
"user" : "rose",
"db" : "admin",
"roles" : [
{
"role" : "readWrite",
"db" : "testmore"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}

三、Mongodb 的备份与恢复


一、备份恢复工具介绍

1、mongodb自带的有两款备份恢复工具,分别是
1)mongoexport/mongoimport:导入/导出的是JSON格式或者CSV格式
2)mongodump/mongorestore :导入/导出的是BSON格式。

二者区别:
1)JSON的优劣:
JSON可读性强但体积较大,BSON则是二进制文件,体积小但对人类几乎没有可读性
JSON虽然具有较好的跨版本通用性,但其只保留了数据部分,不保留索引,账户等其他基础信息

2)BSON的优劣:
不同版本之间用 mongodump/mongorestore可能不会成功
当无法使用BSON进行跨版本的数据迁移的时候,可使用JSON格式

2、第三方连接工具自带的备份和恢复功能
具体就看每个工具具体的使用方法了,当然也不是所有工具都有这样的动能,具体情况再看了

二、数据的备份

1、 备份前数据查看

[root@test220 ~]# mongo -u admin -p 123456 10.6.2.220:27017/admin

> use admin
switched to db admin
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
test 0.000GB
testmore 0.000GB

> use test
switched to db test
> show collections
class
fruit
game
info

> db.fruit.find()
{ "_id" : ObjectId("61de7780f2cb355cb7a46a1c"), "name" : "apple", "price" : 8.5 }
{ "_id" : ObjectId("61de77a1f2cb355cb7a46a1d"), "name" : "banana", "price" : 3.5 }
{ "_id" : 80, "name" : "orange", "price" : 6.5 }
{ "_id" : 70, "name" : "pear", "price" : 6.8 }
{ "_id" : ObjectId("61de786ef2cb355cb7a46a1f"), "name" : "peach", "price" : 11.5 }
{ "_id" : ObjectId("61de78d9f2cb355cb7a46a20"), "name" : "grapes", "price" : 11.5 }
{ "_id" : ObjectId("61de7931f2cb355cb7a46a22"), "name" : "plum", "price" : 5.5 }

> db.class.find()
{ "_id" : ObjectId("61dd25b187f250afc6d4f3e2"), "name" : "zhangsan" }
{ "_id" : ObjectId("61de53da57216ed19d04e4b1"), "name" : "kite", "age" : 20 }
{ "_id" : ObjectId("61de543e57216ed19d04e4b3"), "name" : "tim", "age" : 10 }
{ "_id" : ObjectId("61de543e57216ed19d04e4b4"), "name" : "jack", "age" : 10 }
{ "_id" : ObjectId("61de54dc57216ed19d04e4b6"), "name" : "tim", "age" : 55 }

> db.info.find()
{ "_id" : ObjectId("61de6c004f8fa3f11da6cdce"), "name" : "rose", "age" : 60 }
{ "_id" : ObjectId("61e10f3d0ab2cab372251a11"), "name" : "tim" }
{ "_id" : ObjectId("61e10f620ab2cab372251a12"), "name" : "tim", "age" : 30 }

2、 进行备份操作

1) 使用 mongodump 进行备份
[root@test220 ~]# mkdir -p /backup/backup{01,02.03,04}

# 全部数据库备份
[root@test220 ~]# mongodump -uadmin -p123456 --port 27017 --authenticationDatabase admin -o /backup/backup01

# 备份指定的数据库
[root@test220 ~]# mongodump -uadmin -p123456 --port 27017 --authenticationDatabase admin -d test -o /backup/backup02

# 备份指定数据库的指定集合
[root@test220 ~]# mongodump -uadmin -p123456 --port 27017 --authenticationDatabase admin -d test -c class -o /backup/backup03

# 备份后对文件进行压缩
[root@test220 ~]# mongodump -uadmin -p123456 --port 27017 --authenticationDatabase admin -d test -c class -o /backup/backup04 --gzip

2)使用 mongoexport 进行备份

[root@test220 ~]# mkdir -p /backup/backup{11,22}

备份为 JSON 格式
[root@test220 ~]# mongoexport -uadmin -p123456 --port 27017 --authenticationDatabase admin -d test -c class -o /backup/backup11/class.json

备份为 csv 格式
[root@test220 ~]# mongoexport -uadmin -p123456 --port 27017 --authenticationDatabase admin -d test -c class --type=csv -f name,age -o /backup/backup22/class.csv

3、备份后查看备份文件

[root@test220 ~]# ll /backup/backup01
drwxr-xr-x 2 root root 128 Jan 14 14:00 admin
drwxr-xr-x 2 root root 182 Jan 14 14:00 test
drwxr-xr-x 2 root root 145 Jan 14 14:00 testmore

[root@test220 ~]# ll /backup/backup02
drwxr-xr-x 2 root root 182 Jan 14 14:01 test

[root@test220 ~]# ll /backup/backup03/test/
-rw-r--r-- 1 root root 685 Jan 14 14:13 class.bson
-rw-r--r-- 1 root root 158 Jan 14 14:13 class.metadata.json

[root@test220 ~]# ll /backup/backup04/test/
-rw-r--r-- 1 root root 284 Jan 14 14:02 class.bson.gz
-rw-r--r-- 1 root root 146 Jan 14 14:02 class.metadata.json.gz

[root@test220 ~]# ll /backup/backup11
-rw-r--r-- 1 root root 956 Jan 14 14:24 class.json

[root@test220 ~]# ll /backup/backup22/
-rw-r--r-- 1 root root 120 Jan 14 14:25 class.csv

# 其实可以发现,这种方式备份好的文件是可以直接看懂的,不论是 JSON 或者 CSV
[root@test220 ~]# cat /backup/backup11/class.json
{"_id":{"$oid":"61dd25b187f250afc6d4f3e2"},"name":"zhangsan"}
{"_id":{"$oid":"61de53da57216ed19d04e4b1"},"name":"kite","age":20.0}
{"_id":{"$oid":"61de543e57216ed19d04e4b3"},"name":"tim","age":10.0}
{"_id":{"$oid":"61de543e57216ed19d04e4b4"},"name":"jack","age":10.0}
{"_id":{"$oid":"61de54dc57216ed19d04e4b6"},"name":"tim","age":55.0}

[root@test220 ~]# cat /backup/backup22/class.csv
name,age
zhangsan,
kite,20
tim,10
jack,10
tim,55

# 反观 BSON 格式的,则是到乱码类的密文,无法读出来,安全性上比用 mongoexport 要好
[root@test220 ~]# cat /backup/backup03/test/class.bson
)_idaۥ±ఆճname zhangsan2_idaݓٗ!nѝ䯂namekiteage4@1_idaݔ>W!nѝ䱂nametimage$@2_idaݔ>W!nѝ䲂 namejackage$@1_idaݔۗ!nѝ䴂nametimageK@2_idaݔۗ!nѝ䵂 namejackageN@3_idaݧO£�3namemlikeage$@1_idaݨ6O£�Bnamebobage>@1_idaݨ¿O£�_namecccageI@1_idaݩ¢O£�onameabc

4、将上面的备份文件进行恢复测试

1) mongodump 备份的恢复工具是 mongorestore

单库恢复,恢复到指定一个新库
[root@test220 ~]# mongorestore -uadmin -p123456 --port 27017 --authenticationDatabase admin -d test01 /backup/backup02/test

单库恢复,恢复之前把原来库的集合 drop 掉
[root@test220 ~]# mongorestore -uadmin -p123456 --port 27017 --authenticationDatabase admin -d test --drop /backup/backup02/test

单表恢复,恢复到指定一个新表
[root@test220 ~]# mongorestore -uadmin -p123456 --port 27017 --authenticationDatabase admin -d test01 -c class02 /backup/backup02/test/class.bson

2) mongoexport 备份的恢复工具是 mongoimport

[root@test220 ~]# mongoimport -uadmin -p123456 --port 27017 --authenticationDatabase admin -d test -c class11 /backup/backup11/class.json

[root@test220 ~]# mongoimport -uadmin -p123456 --port 27017 --authenticationDatabase admin -d test -c class22 --type=csv --headerline --file /backup/backup22/class.csv

3) 去数据库里面查看验证

[root@test220 ~]# mongo -u admin -p 123456 10.6.2.220:27017/admin

> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
test 0.000GB
test01 0.000GB
testmore 0.000GB

> use test01
switched to db test01
> show collections
class
class02
fruit
game
info

> db.class.find()
{ "_id" : ObjectId("61dd25b187f250afc6d4f3e2"), "name" : "zhangsan" }
{ "_id" : ObjectId("61de53da57216ed19d04e4b1"), "name" : "kite", "age" : 20 }
{ "_id" : ObjectId("61de543e57216ed19d04e4b3"), "name" : "tim", "age" : 10 }
{ "_id" : ObjectId("61de543e57216ed19d04e4b4"), "name" : "jack", "age" : 10 }
{ "_id" : ObjectId("61de54dc57216ed19d04e4b6"), "name" : "tim", "age" : 55 }

db.class02.find()
{ "_id" : ObjectId("61de53da57216ed19d04e4b1"), "name" : "kite", "age" : 20 }
{ "_id" : ObjectId("61de543e57216ed19d04e4b3"), "name" : "tim", "age" : 10 }
{ "_id" : ObjectId("61de543e57216ed19d04e4b4"), "name" : "jack", "age" : 10 }
{ "_id" : ObjectId("61de54dc57216ed19d04e4b6"), "name" : "tim", "age" : 55 }
{ "_id" : ObjectId("61dd25b187f250afc6d4f3e2"), "name" : "zhangsan" }

> use test
switched to db test
> show collections
class
class11
class22
fruit
game
info

> db.class11.find()
{ "_id" : ObjectId("61dd25b187f250afc6d4f3e2"), "name" : "zhangsan" }
{ "_id" : ObjectId("61de53da57216ed19d04e4b1"), "name" : "kite", "age" : 20 }
{ "_id" : ObjectId("61de543e57216ed19d04e4b3"), "name" : "tim", "age" : 10 }
{ "_id" : ObjectId("61de54dc57216ed19d04e4b6"), "name" : "tim", "age" : 55 }
{ "_id" : ObjectId("61de543e57216ed19d04e4b4"), "name" : "jack", "age" : 10 }

> db.class22.find()
{ "_id" : ObjectId("61e118b48f280e2260ed80f2"), "name" : "zhangsan", "age" : "" }
{ "_id" : ObjectId("61e118b48f280e2260ed80f3"), "name" : "kite", "age" : 20 }
{ "_id" : ObjectId("61e118b48f280e2260ed80f4"), "name" : "tim", "age" : 10 }
{ "_id" : ObjectId("61e118b48f280e2260ed80f5"), "name" : "tim", "age" : 55 }

# 总结: 通过恢复后的对比喝上面种种发现
1、 mongoexport 备份的 CSV 格式,恢复时可能存在问题
{ "_id" : ObjectId("61e118b48f280e2260ed80f2"), "name" : "zhangsan", "age" : "" }
无故多出来 age 字段,原来的文档里面实则没有

2、 mongoexport和mongoimport 只能够针对集合(表)操作
而mongodump和mongorestore则对数据库(全库和单库)、集合都可以操作

3、 安全性上, mongodump和mongorestore 也要安全很多
而且,mongodump和mongorestore支持备份时进行压缩操作,提高了速度
举报

相关推荐

0 条评论