美文网首页
MongoDB docker 添加用户名 密码

MongoDB docker 添加用户名 密码

作者: ouchaochao | 来源:发表于2019-01-18 10:19 被阅读0次

    1.docker ps

    ➜  ~ 查看docker
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                      NAMES
    88159454e823        mongo               "docker-entrypoint.s…"   17 hours ago        Up 17 hours         0.0.0.0:27017->27017/tcp   docker_mongodb
    
    

    2.进入容器内部

    ➜  ~ docker exec -it  88159454e823  /bin/bash
    root@88159454e823:/#
    

    3.mongo命令

    root@88159454e823:/# mongo
    MongoDB shell version v4.0.5
    connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
    Implicit session: session { "id" : UUID("325c2088-11f1-47a5-80f0-d129ad8e33a1") }
    MongoDB server version: 4.0.5
    Server has startup warnings: 
    2019-01-17T09:12:42.654+0000 I STORAGE  [initandlisten] 
    2019-01-17T09:12:42.654+0000 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
    2019-01-17T09:12:42.654+0000 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
    2019-01-17T09:12:43.269+0000 I CONTROL  [initandlisten] 
    2019-01-17T09:12:43.269+0000 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
    2019-01-17T09:12:43.269+0000 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
    2019-01-17T09:12:43.269+0000 I CONTROL  [initandlisten] 
    ---
    Enable MongoDB's free cloud-based monitoring service, which will then receive and display
    metrics about your deployment (disk utilization, CPU, operation statistics, etc).
    
    The monitoring data will be available on a MongoDB website with a unique URL accessible to you
    and anyone you share the URL with. MongoDB may use this information to make product
    improvements and to suggest MongoDB products and deployment options to you.
    
    To enable free monitoring, run the following command: db.enableFreeMonitoring()
    To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
    ---
    
    > 
    

    4.显示数据库

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

    5.选择使用admin库

    > use admin
    switched to db 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
    

    相关文章

      网友评论

          本文标题:MongoDB docker 添加用户名 密码

          本文链接:https://www.haomeiwen.com/subject/qlbgdqtx.html