Search code examples
mongodbopenshiftopenshift-nextgen

How to interact with MongoDB via shell in OpenShift Online 3 Dev Preview?


I've got multiple issues with an application which I suspect are related to permissions on the database.

Everything seems locked down however and I can't complete basic commands such as show dbs in order to troubleshoot the problems further and "see" what I'm working with. I've been stuck on this for two days now and it's really frustrating.

I've tried this both from the online console and on local terminal, both with and without user credentials supplied at login:

Online Console

Console > MongoDB Service > Deployment > Pod > mongodb-1-vs19d > Terminal:

sh-4.2$ mongo                                                                   
MongoDB shell version: 2.6.9                                                    
connecting to: test                                                             
> show dbs                                                                      
2016-07-13T04:33:10.809-0400 listDatabases failed:{                             
        "ok" : 0,                                                               
        "errmsg" : "not authorized on admin to execute command { listDatabases: 
1.0 }",                                                                         
        "code" : 13                                                             
} at src/mongo/shell/mongo.js:47

Local Terminal

me@my-computer:~$ oc rsh mongodb-1-vs19d
sh-4.2$ mongo
MongoDB shell version: 2.6.9
connecting to: test
> show dbs
2016-07-13T04:35:06.449-0400 listDatabases failed:{
    "ok" : 0,
    "errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }",
    "code" : 13
} at src/mongo/shell/mongo.js:47

Local Terminal With User Credentials

me@my-computer:~$ oc rsh mongodb-1-vs19d
sh-4.2$ mongo -u $MONGODB_USER -p $MONGODB_PASSWORD $MONGODB_DATABASE
MongoDB shell version: 2.6.9
connecting to: users
> show dbs
2016-07-13T04:51:39.127-0400 listDatabases failed:{
    "ok" : 0,
    "errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }",
    "code" : 13
} at src/mongo/shell/mongo.js:47

Troubleshooting envars are correct:

me@my-computer:~$ oc env pods mongodb-1-vs19d --list
# pods mongodb-1-vs19d, container mongodb
MONGODB_USER=admin
MONGODB_PASSWORD=secret
MONGODB_DATABASE=users
MONGODB_ADMIN_PASSWORD=very-secret

The database was created from local terminal with:

oc new-app mongodb-persistent -p MONGODB_USER=admin,MONGODB_PASSWORD=secret,MONGODB_ADMIN_PASSWORD=very-secret

As per official docs:

https://docs.openshift.com/online/getting_started/beyond_the_basics.html#btb-provisioning-a-database

https://docs.openshift.com/online/using_images/db_images/mongodb.html#running-mongodb-commands-in-containers


Solution

  • It looks like you're trying to show dbs as user who has not been granted the needed role. You can try authenticating yourself as admin as follows:

    mongo -u admin -p $MONGODB_ADMIN_PASSWORD admin
    

    Then you should be able to show dbs, show users, use other databases and show users there, etc...


    It's a bit confusing that your $MONGODB_USER is named admin - this regular user will have access to the $MONGODB_DATABASE. Another admin has been created by the used image most likely as well.