MongoDB和Docker通过docker

编程入门 行业动态 更新时间:2024-10-26 09:22:01
本文介绍了MongoDB和Docker通过docker-compose auth错误而不是初始化数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是Docker的新手,并且已阅读 Mongo Docker Docs 。 我需要一些帮助来设置一个简单的应用程序。

I am new to Docker and I have read the Mongo Docker Docs. I need some help setting up a simple application.

我想要一个本地mongodb来转储/使用名为 my_test_db的数据库并进行写操作到< project-dir> / mongo / my_test_db

I want a local mongodb that dumps / uses a database named "my_test_db" and writes to <project-dir>/mongo/my_test_db

这是我的简单项目:

# project structure <project-dir>/ -- .env -- docker-compose.yml -- mongo/ -- + -- my_test_db/

.env 文件:

# <project>/.env MONGO_INITDB_ROOT_USERNAME=test_user MONGO_INITDB_ROOT_PASSWORD=test_pwrd MONGO_INITDB_DATABASE=my_test_db MONGO_DATA_DIR=/mongo/my_test_db MONGO_LOG_FILE=/mongo/mongodb.log

我尝试 docker compose :

#docker-compose.yml version: '3' services: mongodb: image: mongo:4.0 # load env variables env_file: - .env # map data_dir to host <project>/<data_dir> volumes: - ".${MONGO_DATA_DIR}:${MONGO_DATA_DIR}" expose: - 27017 # set env vars with loaded vars (appears to be necessary for the user to be created, despite loading them with env_file) environment: MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME} MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_DATABASE} MONGO_INITDB_DATABASE: ${MONGO_INITDB_DATABASE} # map ports ports: - 27017:27017 command: "mongod --smallfiles" container_name: mongodb-test # --logpath=.${MONGO_LOG_FILE} # when added to command fails

然后将其放入两个外壳

#shell-1 <project-dir>$ docker-compose up # I see my user created # I see "MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=e19ea190b601" so my custom db isnt made on init...

#shell-2 # <project-dir>$ mongo 27017 --usernamne test_user --password test_pwrd # does fails to connect <project-dir>$ mongo 27017 # connects > db.getName() # 27017 # so naming it didnt work > show users # Error: command usersInfo requires authentication > use my_test_db > db.myColl.insert({'name': 'test'}) # WriteCommandError({..., "codeName": "Unauthorized"})

我目前正在macOS上进行测试(文档表明这可能是相关的)

I am currently testing on macOS (as docs state that this might be relevant)

我在做什么错?我了解docker的概念,但个人不使用docker的概念,因为这也使我很难使用...但是我正在尝试学习。

What am I doing wrong? I understand the concept of docker but personally don't use it as it is a pain to get used too... but I'm trying to learn.

推荐答案

  • 对于身份验证部分:您是否尝试添加-authenticationDatabase admin 以便能够登录?
  • 关于 MONGO_INITDB_DATABASE 除非您在 / docker-entrypoint-下有一个脚本,否则它不会生效initdb.d / 如 docker-entrypoiny.sh ,因此您可以在 docker-entrypoint-initdb.d /

    • For the authentication part: have you tried to add --authenticationDatabase admin in order to be able to login ?
    • Regarding MONGO_INITDB_DATABASE it wont be effective unless you have a script under /docker-entrypoint-initdb.d/ as shown in the docker-entrypoiny.sh that is used in the mongodb image so you can have a bash script in docker-entrypoint-initdb.d/ with that said.

      使用root用户作为应用程序数据库的所有者不是安全的方法,因为它将能够控制其中的所有内容。 mongodb实例。

      Using the root user as an owner for your application db is not a security wise as it will have the ability to control everything in the mongodb instance.

      恕我直言,更好的方法是添加额外的变量,例如以下内容:

      IMHO a better approach would be adding extra variables like the following:

      • MONGO_APPLICATION_DATABASE
      • MONGO_APPLICATION_USERNAME
      • MONGO_APPLICATION_PASSWORD
      • MONGO_APPLICATION_DATABASE
      • MONGO_APPLICATION_USERNAME
      • MONGO_APPLICATION_PASSWORD

      然后添加一个bash脚本,该脚本将创建用户并使用环境变量将其分配给数据库(您需要将其挂载到docker-entrypoint-initdb.d):

      Then add a bash script that will create the user and assign it to the database by using environment variables ( you need to mount it to docker-entrypoint-initdb.d):

      您可能需要根据需要更改用户角色。

      You might need to change the user role as you see fit.

      # initmongo/setup.sh mongo admin -u $MONGO_INITDB_ROOT_USERNAME -p $MONGO_INITDB_ROOT_PASSWORD --eval "db.getSiblingDB('$MONGO_APPLICATION_DATABASE').createUser({user: '$MONGO_APPLICATION_USERNAME', pwd: '$MONGO_APPLICATION_PASSWORD', roles: [{role: 'readWrite', db: '$MONGO_APPLICATION_DATABASE'}]});"

      在docker-compose.yml中将卷添加到当前卷列表中

      In the docker-compose.yml add a volume to your current list of volumes

      #docker-compose.yml volumes: - ".${MONGO_DATA_DIR}:${MONGO_DATA_DIR}" - "./initmongo/:/docker-entrypoint-initdb.d/"

      最后删除此变量 MONGO_INITDB_DATABASE ,因为不需要它

      And finally remove this variable MONGO_INITDB_DATABASE as there is no need for it

      要登录,请使用以下命令:

      In order to login use the following command:

      将变量替换为您的实际值

      replace the variables with your actual values

      docker exec -it container_name mongo -u "$MONGO_APPLICATION_USERNAME" -p "$MONGO_APPLICATION_PASSWORD" --authenticationDatabase "$MONGO_APPLICATION_DATABASE" "$MONGO_APPLICATION_DATABASE"

    • 对于 .env 文件,我不确定是什么引起了此问题。使用时是否有任何警告?还可以考虑检查此评论或此

    • For the .env file i am not sure what could cause this issue. do you have any warnings while using it ? Also Consider checking this comment or this

更多推荐

MongoDB和Docker通过docker

本文发布于:2023-10-22 08:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1517005.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:MongoDB   Docker   docker

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!