如何为init-mongo.sh:ro编写bash脚本以添加管理员用户并运行初始化脚本 [英] How to write bash script for init-mongo.sh:ro to add admin user and run an initialisation script

查看:114
本文介绍了如何为init-mongo.sh:ro编写bash脚本以添加管理员用户并运行初始化脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我添加docker-entrypoint-initdb.d/init-mongo.sh:ro文件时,我的docker mongo组合无法通过身份验证

My docker compose of mongo fails to authenticate when I add a docker-entrypoint-initdb.d/init-mongo.sh:ro file

docker-compose

version: "3.1"
services:
  mongo1:
    container_name: mongo1
    image: mongo
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: ${MONGO_USER}
      MONGO_INITDB_ROOT_PASSWORD: ${MONGO_PASS}
      MONGO_INITDB_DATABASE: ${MONGO_DB_NAME}
    ports:
      - 27017:27017
      - 28017:28017
    env_file:
      - .env
    volumes:
      - volume-mongo:/data/db
      - ./mongo/init-mongo-js.sh:/docker-entrypoint-initdb.d/init-mongo.sh:ro
    command: ['--auth', '--wiredTigerCacheSizeGB=1']
    networks:
      - mongo-network
  # mongo-express:
  #   container_name: mongo-express
  #   image: mongo-express
  #   restart: always
  #   ports:
  #     - '8081:8081'
  #   env_file:
  #     - .env
  #   environment:
  #     - ME_CONFIG_OPTIONS_EDITORTHEME=ambiance
  #     - ME_CONFIG_BASICAUTH_USERNAME=${MONGO_USER}
  #     - ME_CONFIG_BASICAUTH_PASSWORD=${MONGO_PASS}
  #     - ME_CONFIG_MONGODB_ENABLE_ADMIN=true
  #     - ME_CONFIG_MONGODB_AUTH_DATABASE=admin
  #     - ME_CONFIG_MONGODB_SERVER=mongo1
  #     - ME_CONFIG_MONGODB_ADMINUSERNAME=${MONGO_USER}
  #     - ME_CONFIG_MONGODB_ADMINPASSWORD=${MONGO_PASS}
  #   depends_on:
  #     - mongo1
  #   networks:
  #     - mongo-network
volumes:
  volume-mongo:
    driver: local

networks:
  mongo-network:
    driver: bridge

.env

MONGO_DB_NAME=DBX
MONGO_USER=MYNEWUSERNAME
MONGO_PASS=somesecret
# Mongo host

尝试登录失败

docker exec -it mongo1 mongo admin -u MYNEWUSERNAME -p somesecret

当我删除对init-mongo.sh:ro-文件的引用时,它可以工作,但随后我无法运行初始化脚本.

When I remove the reference to the init-mongo.sh:ro- file then it works but then I don't get to run my initialisation script.

我被引荐给这篇文章: https://github.com/docker-library/mongo/issues/174

I have been referred to this article: https://github.com/docker-library/mongo/issues/174

我假设它失败的原因是因为我的bash文件-init-mongo.sh:ro正在覆盖此文件:

I'm assuming the reason it fails is because my bash file - init-mongo.sh:ro is overwriting this file:


if [ "$MONGO_INITDB_ROOT_USERNAME" ] && [ "$MONGO_INITDB_ROOT_PASSWORD" ]; then 
  rootAuthDatabase='admin' 

  "${mongo[@]}" "$rootAuthDatabase" <<-EOJS 
    db.createUser({ 
      user: $(_js_escape "$MONGO_INITDB_ROOT_USERNAME"), 
      pwd: $(_js_escape "$MONGO_INITDB_ROOT_PASSWORD"), 
      roles: [ { role: 'root', db: $(_js_escape "$rootAuthDatabase") } ] 
    }) 
  EOJS 
fi 

如果该理论是正确的,那么将其包含在我的脚本中以及进行其他更改似乎是一个很好的解决方案.唯一的问题是在我的vscode中,它具有shell格式扩展,这种代码组合的颜色编码看起来不正确,所以我怀疑它是错误的.

If that theory is correct then including it into my script with my additional changes seems like a good solution. The only problem is in my vscode, with shell-format extension, the colour coding of this code combination doesn't look right so I suspect its wrong.

init-mongo.sh:ro 尝试合并以下内容:

#!/bin/bash
set -e
# set -e causes the whole script to exit when a command fails, so the script can't silently fail and startup mongo.

if [ "$MONGO_INITDB_ROOT_USERNAME" ] && [ "$MONGO_INITDB_ROOT_PASSWORD" ]; then 
  rootAuthDatabase='admin' 

  "${mongo[@]}" "$rootAuthDatabase" <<-EOJS 
    db.createUser({ 
      user: $(_js_escape "$MONGO_INITDB_ROOT_USERNAME"), 
      pwd: $(_js_escape "$MONGO_INITDB_ROOT_PASSWORD"), 
      roles: [ { role: 'root', db: $(_js_escape "$rootAuthDatabase") } ] 
    }) 
  EOJS 
fi 

mongo <<EOF
use ${MONGO_DB_NAME}
db.createCollection("users")
db.users.insert({"name": "mike"})
EOF

感谢您对修复此bash脚本的任何帮助.

Any help in fixing this bash script appreciated.

谢谢

推荐答案

我已经尝试并删除了docker-compose文件中的引用

I have experimented and removed the reference in the docker-compose file

    environment:
      MONGO_INITDB_ROOT_USERNAME: ${MONGO_USER}
      MONGO_INITDB_ROOT_PASSWORD: ${MONGO_PASS}
      MONGO_INITDB_DATABASE: ${MONGO_DB_NAME}

,而是直接在init文件中创建管理员用户.

and instead create the admin user directly in the init file.

#!/bin/bash
set -e

mongo <<EOF
use admin 
db.createUser(
  {
    user: "${MONGO_USER}",
    pwd: "${MONGO_PASS}",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
  }
)

use ${MONGO_DB_NAME}
db.createCollection("users")
db.users.insert({"name": "john"})
EOF

这篇关于如何为init-mongo.sh:ro编写bash脚本以添加管理员用户并运行初始化脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆