MongoDB Admin user creation script

## Copy this script to your mongo /bin folder #################################
ID=`id -u -n`
echo
echo " AdminUserCreation.sh script is executed by user : $ID"
echo
echo " Date and Time of Execution : `date`"
echo
read -p "Enter the port number for your mongos process: " port
echo
host="localhost"
uid=umg_menu
db=admin
roles=userAdminAnyDatabase\",\"clusterAdmin\",\"readWriteAnyDatabase
read -s -p "Enter the first half of the password for umg_menu user : " pwd1
sleep 1
echo -e "\n"
read -s -p "Enter the second half of the password for umg_menu user : " pwd2
pwd=`echo "$pwd1$pwd2"`
/apps/bin/mongo $host:$port/$db --eval "db.addUser({user:\"$uid\",pwd:\"$pwd1$pwd2\",roles:[\"$roles\"]})" >/dev/null 2>&1 &
mypid=$!
wait $mypid
op=$?
if [ $op -eq 0 ]; then
echo
echo -e "\nSUCCESS: Admin user (umg_menu) has been created with below details
--------------------------------------------------------------------------
User name : $uid
Database : $db 
Hostname : $host
Port : $port
Roles : $roles
User createdOn : `date` \n"
else
echo
echo -e "\nERROR: umg_menu User creation failed, please check & try again."
fi

  • Ask Question