Last active
February 14, 2021 02:31
-
-
Save kennwhite/3b0361aa841a49f4f1ce38f1aad3567f to your computer and use it in GitHub Desktop.
Enable Authentication for new install of MongoDB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
sudo sed -i-`date -u|tr ' ' '_'`.bak 's/^#security:*/security:\n authorization: enabled/' /etc/mongod.conf | |
_mpwd=$(head -c 32 /dev/urandom 2>&1 | sha1sum | head -c 14 ) | |
echo "db.dropUser('dba')"|mongo "mongodb://localhost/admin" | |
echo "db.createUser({user:'dba',pwd:'$_mpwd',roles:['root']})"|mongo "mongodb://localhost/admin" | |
sudo service mongod restart && sleep 5 | |
echo show dbs | mongo "mongodb://dba:$_mpwd@localhost/?authSource=admin" | |
echo -e "\nTo login as user 'dba': mongo 'mongodb://dba:$_mpwd@localhost/?authSource=admin' (or YourIpAddress:27107)\n\n" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Expanded version: