RET=1 while [[ RET -ne 0 ]]; do echo "=> Waiting for confirmation of MongoDB service startup" sleep 5 mongo admin --eval "help" >/dev/null 2>&1 RET=$? done
#通过docker logs + id可以看到下面的输出 echo "=> Creating an admin user with a ${_word} password in MongoDB" mongo admin --eval "db.addUser({user: 'admin', pwd: '$PASS', roles: [ 'userAdminAnyDatabase', 'dbAdminAnyDatabase' ]});" mongo admin --eval "db.shutdownServer();"
echo "=> Done!" touch /.mongodb_password_set
echo "========================================================================" echo "You can now connect to this MongoDB server using:" echo "" echo " mongo admin -u admin -p $PASS --host <host> --port <port>" echo "" echo "Please remember to change the above password as soon as possible!" echo "========================================================================"
④ run.sh脚本是主要的启动脚本,内容为: [root@docker1 Mongodb]# cat run.sh #!/bin/bash if [ ! -f /.mongodb_password_set ]; then /set_mongodb_password.sh fi
if [ "$AUTH" == "yes" ]; then export mongodb='/usr/local/mongodb/bin/mongod --nojournal --auth --httpinterface --rest' else export mongodb='/usr/local/mondodb/bin/mongod --nojournal --httpinterface --rest' fi
if [ ! -f /data/db/mongod.lock ]; then eval $mongodb else export mongodb=$mongodb' --dbpath /data/db' rm /data/db/mongod.lock mongod --dbpath /data/db --repair && eval $mongodb fi