1.进入liunx

docker ps

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                      NAMES
f0c71c17e606        mongo:3.2           "docker-entrypoint.sh"   7 days ago          Up 7 days           0.0.0.0:27017->27017/tcp   serene_minsky
 

2.进入容器内部

docker exec -it  f0c71c17e606  /bin/bash

3.mongo命令(root 后变为容器id)

root@f0c71c17e606:/# mongo
MongoDB shell version: 3.2.19
connecting to: test
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
Server has startup warnings:
2018-04-17T07:18:29.956+0000 I CONTROL  [initandlisten]
2018-04-17T07:18:29.956+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-04-17T07:18:29.956+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-04-17T07:18:29.956+0000 I CONTROL  [initandlisten]
2018-04-17T07:18:29.956+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-04-17T07:18:29.956+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-04-17T07:18:29.956+0000 I CONTROL  [initandlisten] 

4.show dbs  显示数据库

show dbs

5.use admin 选择使用admin库

use admin

6.创建用户

db.createUser( {

 user: "root",
 pwd: "root",

 roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]})

成功后显示信息

Successfully added user: {
        "user" : "root",
        "roles" : [
                {
                        "role" : "userAdminAnyDatabase",
                        "db" : "admin"
                }
        ]
}
>

mongodb中的用户是基于身份role的,该管理员账户的 role是 userAdminAnyDatabase。 ‘userAdmin’代表用户管理身份,’AnyDatabase’ 代表可以管理任何数据库。

7.第二次进入

> use admin
switched to db admin
> db
admin
>  db.auth("root", "root")
1
>  db.auth("root", "roo1t")
Error: Authentication failed.
0







Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐