我使用的是mongo泊坞窗图片。当我尝试为新数据库创建用户和密码时,我收到错误。
这是我的Dockerfile:
FROM mongo
ENV AUTH yes
ENV JOURNALING yes
ADD run.sh /usr/bin/run.sh
ADD set_mongodb_password.sh /usr/bin/set_mongodb_password.sh
RUN chmod +x /usr/bin/run.sh && chmod +x /usr/bin/set_mongodb_password.sh
EXPOSE 27017 28017
CMD ["/usr/bin/run.sh"]
好的,现在我的运行脚本:
#!/bin/bash
cmd="mongod"
if [ "$AUTH" == "yes" ]; then
cmd="$cmd --auth"
fi
if [ "$JOURNALING" == "no" ]; then
cmd="$cmd --nojournal"
fi
$cmd &
if [ ! -f /data/db/.mongodb_password_set ]; then
set_mongodb_password
fi
fg
和我的set_mongodb_password脚本:
#!/bin/bash
USER=${MONGO_USER:-"mongo"}
PASS=${MONGO_PASSWORD:-"mongo"}
DB=${MONGO_DB:-"testdb"}
echo "=> Creating database < ${DB} > with an user < ${USER} > and password < ${PASS} > in MongoDB"
echo "=> Creating database"
mongo $DB --eval "db.getSiblingDB('${DB}');"
echo "=> Creating user and password"
mongo $DB --eval "db.createUser({user: '${USER}', pwd: '${PASS}', roles:[ { role:'readWrite',db: '${DB}'} ] } ) ;"
#mongo admin --eval "db.createUser({user: 'mongo', pwd: '${PASS}', roles:[{role:'readWrite',db:'$DB'}]});"
echo "=> Done!"
touch /data/db/.mongodb_password_set
echo "========================================================================"
echo "You can now connect to this MongoDB server using:"
echo ""
echo " mongo ${DB} -u ${USER} -p ${PASS} --host <host> --port <port>"
echo ""
echo "Please remember to change the above password as soon as possible!"
echo "========================================================================"
我构建并创建了我的mongo映像,所以当我尝试连接用户和密码时,它会返回一个身份验证错误。然后我使用docker exec
命令进入我的mongo容器并手动运行set_mongodb_password脚本,我收到了以下错误:
2015-08-05T06:08:50.550+0000 E QUERY Error: couldn't add user: not authorized on pepedb to execute command { createUser: "pepe", pwd: "xxx", roles: [ { role: "readWrite", db: "pepedb" } ], digestPassword: false, writeConcern: { w: "majority", wtimeout: 30000.0 } }
at Error (<anonymous>)
at DB.createUser (src/mongo/shell/db.js:1101:11)
at (shell eval):1:4 at src/mongo/shell/db.js:1101
所以,我不知道如何解决这个错误。当我使用简单的mongo图像并运行它。我可以进入容器并按步骤运行我的set_mongodb_password脚本步骤的内容,一切正常。所以我不明白问题出在哪里。也许我在配置或某些mongo参数中遗漏了一些东西。有什么帮助吗?
答案 0 :(得分:1)
run.sh和set_mongodb_password.sh中有一些错误。
在脚本run.sh中,你需要在#!bash行后面添加“set -m”,因为在最后一行你使用了fg。阅读帖子quamash repo以了解“set -m”和“fg”。
在同一个脚本中,您需要调用set_mongodb_password,例如/usr/bin/set_mongodb_password.sh而不是set_mongodb_password
set_mongodb_password.sh的问题是它在run.sh之前运行,所以你需要在“#!bash”之后添加以下行来等待mongo上升:
/** This method compares 2 BigDecimal objects for equality. It takes care of null object and that was the necessity of having it.
* To use this function most efficiently pass the possibly null object before the not null object.
* @param pNumber1
* @param pNumber2
* @return boolean
*/
public static boolean isEqual(BigDecimal pNumber1, BigDecimal pNumber2)
{
if ( pNumber1 == null )
{
if ( pNumber2 == null)
return true;
return false;
}
if ( pNumber2 == null)
return false;
return pNumber1.compareTo(pNumber2)==0;
}
执行此操作后,您将能够执行mongo命令并创建所需的用户。