[root@wanfei ~]# mkdir -p ~/i/apps/mongodb/data
[root@wanfei ~]# cd ~/i/apps/mongodb/
[root@wanfei mongodb]# ls
data
[root@wanfei mongodb]# cat < start.sh
#!/bin/bash
HOST_NAME=mongodb.wanfei.xyz
MONGODB_DIR=`pwd`
docker stop mongodb
docker rm mongodb
docker run -d \\
--hostname \${HOST_NAME} \\
--restart always \\
-p 27017:27017 \\
--name mongodb\\
-v \${MONGODB_DIR}/data:/data/db \\
registry.docker-cn.com/library/mongo \\
--auth
EOF
[root@wanfei mongodb]# ls
data start.sh
说明:
-d
: 后台运行容器;
--name
: 指定容器名;
-p
: 指定服务运行的端口;
-v
: 映射目录或文件;
--hostname
: 访问域名;
--auth
: 表示连接mongodb需要授权
[root@wanfei mongodb]# sh start.sh
Error response from daemon: No such container: mongodb
Error: No such container: mongodb
Unable to find image 'registry.docker-cn.com/library/mongo:latest' locally
latest: Pulling from library/mongo
7b722c1070cd: Pull complete
5fbf74db61f1: Pull complete
ed41cb72e5c9: Pull complete
7ea47a67709e: Pull complete
778aebe6fb26: Pull complete
3b4b1e0b80ed: Pull complete
844ccc42fe76: Pull complete
eab01fe8ebf8: Pull complete
e5758d5381b1: Pull complete
dc553720c5c3: Pull complete
67750c781aa2: Pull complete
b00b8942c827: Pull complete
32201bb8ca69: Pull complete
Digest: sha256:002fda672a0d196325a30736d4c80d04adf6f39dd28db41e6799f42844cab7b8
Status: Downloaded newer image for registry.docker-cn.com/library/mongo:latest
9ce4360786d3d003ec092203260a8d70544455f52146fa81f43e92a094253711
[root@wanfei mongodb]# docker exec -it mongodb mongo admin
MongoDB shell version v4.0.6
connecting to: mongodb://127.0.0.1:27017/admin?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("9ef68ffc-5ca0-4e86-92f0-68d581716a9b") }
MongoDB server version: 4.0.6
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
>
> db.createUser({ user: 'admin', pwd: 'xxxxxx', roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] });
Successfully added user: {
"user" : "wanfei",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
]
}
> exit
bye
MongoDB用户权限
内建的角色:
数据库用户角色:read、readWrite;
数据库管理角色:dbAdmin、dbOwner、userAdmin;
集群管理角色:clusterAdmin、clusterManager、clusterMonitor、hostManager;
备份恢复角色:backup、restore;
所有数据库角色:readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、dbAdminAnyDatabase
超级用户角色:root // 这里还有几个角色间接或直接提供了系统超级用户的访问(dbOwner 、userAdmin、userAdminAnyDatabase)
内部角色:__system
角色说明:
Read
:允许用户读取指定数据库
readWrite
:允许用户读写指定数据库
dbAdmin
:允许用户在指定数据库中执行管理函数,如索引创建、删除,查看统计或访问system.profile
userAdmin
:允许用户向system.users集合写入,可以找指定数据库里创建、删除和管理用户
clusterAdmin
:只在admin数据库中可用,赋予用户所有分片和复制集相关函数的管理权限。
readAnyDatabase
:只在admin数据库中可用,赋予用户所有数据库的读权限
readWriteAnyDatabase
:只在admin数据库中可用,赋予用户所有数据库的读写权限
userAdminAnyDatabase
:只在admin数据库中可用,赋予用户所有数据库的userAdmin权限
dbAdminAnyDatabase
:只在admin数据库中可用,赋予用户所有数据库的dbAdmin权限。
root
:只在admin数据库中可用。超级账号,超级权限
Robo 3T
下载地址 https://robomongo.org/download
save
保存,再Connect
连接root
角色的用户登录才能创建数据库
和添加数据库对应用户
(经测试添加失败,只能通过命令手动添加用户)# 进入admin数据库
[root@wanfei mongodb]# docker exec -it mongodb mongo admin
MongoDB shell version v4.0.6
connecting to: mongodb://127.0.0.1:27017/admin?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("3cb25388-ac56-4d1d-8f80-f2b67c9b3f5f") }
MongoDB server version: 4.0.6
# 输入账号密码认证,返回1说明认证成功
> db.auth("admin","xxxxxx")
1
# 查看所有数据库
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
# 创建新数据库
> use webflux
switched to db webflux
# 创建 和新创建的数据库 绑定的用户
> db.createUser({ user: 'wanfei', pwd: 'xxxxxx', roles: [ { role: "readWrite", db: "webflux" } ] });
Successfully added user: {
"user" : "wanfei",
"roles" : [
{
"role" : "readWrite",
"db" : "webflux"
}
]
}
# 重新认证新的用户
> db.auth("wanfei","xxxxxx")
1
# 随便添加一条信息才算创建成功
> db.webflux.insert({"name":"sss"});
WriteResult({ "nInserted" : 1 })
> show dbs
webflux 0.000GB
> use admin
switched to db admin
> db.auth("admin","xxxxxx")
1
# 创建一个root用户
> db.createUser({user: "root",pwd: "xxxxxx",roles: [ { role: "root", db: "admin" } ]})
> db.auth("root","xxxxxx")
> show users
{
"_id" : "admin.admin",
"user" : "admin",
"db" : "admin",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}
{
"_id" : "admin.root",
"user" : "root",
"db" : "admin",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}
# 删除单个用户
> db.system.users.remove({user:"admin"})
WriteResult({ "nRemoved" : 1 })
> show users
{
"_id" : "admin.root",
"user" : "root",
"db" : "admin",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}