MongoDB - admin user not authorized

Easy Fix Guide: MongoDB - admin user not authorized 🐒
So you've been following the MongoDB documentation to add authorization to your MongoDB, but you're facing issues with the admin user not being authorized. Don't panic! We've got you covered with some easy solutions to get you back on track. 🚀
Understanding the Problem:
The error message you're seeing (not authorized on admin) indicates that the user you created (sa) does not have the necessary permissions to perform certain actions.
Solution:
To fix this issue, we need to grant the correct privileges to the admin user. Follow these steps:
Start by connecting to your MongoDB server using the
mongoshell:/usr/bin/mongo localhost:27017/adminAuthenticate as the admin user you created (
sa) using the following command:db.auth("sa", "pwd")Make sure to replace
pwdwith the actual password you set for the admin user. If the authentication is successful, you will see1as the output.Grant the necessary privileges to the admin user using the
grantRolesToUsercommand. Run the following command to grant therootrole to thesauser:db.grantRolesToUser("sa", [{ role: "root", db: "admin" }])Exit the
mongoshell by typingexit.Restart the MongoDB service to apply the changes:
sudo service mongod restartReconnect to the
mongoshell as thesauser and verify that the user has the required privileges:/usr/bin/mongo localhost:27017/admin -u sa -p pwdYou should now be able to perform administrative tasks and create other users with specific permissions. Try running the commands
show collectionsto test if the authorization is working as expected.
Call-to-Action: Share Your Experience! 💬
Now that you've successfully fixed the "admin user not authorized" issue, why not share your experience with the MongoDB community? Leave a comment below and let us know how this guide helped you. If you encountered any other MongoDB challenges, we're here to help! 🙌
Don't forget to hit the share button and spread the word to help other developers facing the same issue. Happy coding! 👩💻👨💻
Take Your Tech Career to the Next Level
Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.



