简体   繁体   English

Docker-使用MongoDB创建第一个用户

[英]Docker - Creating First User with MongoDB

I am new to docker, and am trying to set up my first mongo db user on my docker container. 我是Docker的新手,正在尝试在Docker容器上设置我的第一个mongo db用户。 I followed this get mongo for docker tutorial to get started, which provided this .yml file: 我遵循了此get mongo入门指南 ,该教程提供了.yml文件:

# Use root/example as user/password credentials

version: '3.1'
services:

  mongo:
    image: mongo
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example
  mongo-express:
    image: mongo-express
    restart: always
    ports:
      - 8081:8081
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: root
      ME_CONFIG_MONGODB_ADMINPASSWORD: example

I then used docker-compose up to create my container instance. 然后,我使用docker-compose up创建了我的容器实例。 Once this was done, I ssh'ed into the container and tried to do a simple show dbs query. 完成此操作后,我将其切入容器并尝试执行一个简单的show dbs查询。 Whenever I run this, or any other command, I get the following authorisation message: 每当我运行此命令或任何其他命令时,都会收到以下授权消息:

> use admin
switched to admin
> show dbs
2018-06-09T17:20:48.644+0000 E QUERY    [thread1] Error: listDatabases failed:{
    "ok" : 0,
    "errmsg" : "not authorized on admin to execute command { 
         listDatabases: 1.0, $db: \"admin\" }",
    "code" : 13,
    "codeName" : "Unauthorized"
}

I read that when you use environment variables like MONGO_INITDB_ROOT_USERNAME and MONGO_INITDB_ROOT_PASSWORD , that you didn't have to use --auth as this covers me, so I could do things like creating users? 我读到,当您使用诸如MONGO_INITDB_ROOT_USERNAMEMONGO_INITDB_ROOT_PASSWORD类的环境变量时,您不必使用--auth,因为这涵盖了我,因此我可以执行创建用户之类的操作? https://stackoverflow.com/a/42973849/4819186 https://stackoverflow.com/a/42973849/4819186

Would someone be able to point me in the right direction on how to set the user up correctly? 有人能为我指出正确设置用户的正确方向吗? Thanks. 谢谢。

you need to authenticate yourself when connecting to the mongod (mongo server). 连接到mongod(mongo服务器)时,您需要进行身份验证。 You need to use something like: 您需要使用类似:

mongo --host my_host -u user -p password --authenticationDatabase admin

with this you're basically authenticating yorurself against admin database, now you should have the right privileges 这样,您基本上就可以对管理数据库进行身份验证,现在您应该具有正确的权限

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM