creating first user in MongoDB

207 views
Skip to first unread message

Venkatram Athota

unread,
Dec 15, 2016, 4:00:20 PM12/15/16
to mongodb-user
Team,


I have installed MongoDB on Oneops cloud through script and I also want to create a initial user through script. when I try to do that, it is giving me an error saying "  could not add user .. is not authorized on Admin database "  i am running the script on CentOS system and mongodb version installed is 3.2.8. 

here is the command i am using in the script to connect to shell and try to add initial user..

mongo admin <<'EOF'
use admin
var mongoadmin= {user:"mongoAdmin",pwd:"xxxxx",roles:[{role:"clusterAdmin",db:"admin"},{role:"userAdminAnyDatabase",db:"admin"},{role:"backup",db:"admin"},{role:"userAdmin",db:"tax"}]}
db.createUser(mongoadmin)
exit
EOF

it is connecting to mongo shell but could not add user.

can you please help me to add initial user in mongodb? 

R Kumar

unread,
Dec 16, 2016, 3:35:42 PM12/16/16
to mongodb-user
Are you connecting via the localhost interface? After enabling authentication, if no users exist on the system, the only way to connect and create users is to connect via localhost. So you would ssh to the server hosting the MongoDB instance and run the script from there.

If you are already doing that and you still can't get in, I would guess that there might be an existing user already on the database. In that case, the localhost exception closes. In this case, you would need to disable auth, create userAdmin@admin, and re-enable auth.

Worst case scenario you can disable auth, create userAdmin@admin, and re-enable auth. But I would try to execute the script on the machine hosting the MongoDB server first, and if that fails, do the disable-enable route. If you do have to disable auth, I'd use something like db.getUsers() for each DB to see if maybe there is a user hanging around that messed everything up.
Reply all
Reply to author
Forward
0 new messages