Root user can't connect to any database with MongoDB - mongodb

I'm using MongoDB with Docker and want to automate the database creation.
I found that when passing MONGO_INITDB_ROOT_USERNAME and MONGO_INITDB_ROOT_PASSWORD environment variables to the docker run command I can create a new root user on the admin database.
My issue starts when I'm trying to create a new database and create collections in it.
I wrote a script that selects the admin database, authenticate into the mongo service and creates new db, but for some reasons I can not access the database with my credentials..
use admin;
db.auth('myuser', 'mypassword');
use newDatabase;
db.createCollection('newCol');
when trying to authenticate to newDatabase with the given credentials (myuser and mypassword) I found that I do not have the needed permissions to the db.
What did I do wrong? How can I do it automatically anyway?

I just understood that the root cannot access the other databases for some reasons, so I used db.createUser and created a new user just for the new database.

Related

Unable to connect to local Postgres via GUI

I recently ran a Postgres via Docker where in I have a Dockerfile that copies an init script to add new user, enable UUID extension, created database and grant privileges.
I can login using the default user "postgres" but not the second account "XXX_itsm". It says FATAL: database "XXX_itsm" does not exist.
What I'm planning to do is just create a database but another problem occurred when connecting via GUI (Postico / TablePlus).
Which leads me to the following questions:
What is the proper way of creating a user? (I have this current init script in entrypoint)
NOTE: I have the variables defined in Dockerfile ENV DB_USER DB_PASS DB_NAME
Why it's only allowing me to login in GUI with the default user?

how to create a database other than admin in cosmos db?

I recently created an cosmosdb account which uses Mongo API. when I create new collections the only database it uses is "admin".
I tried attaching the db name like testdb to the url but that didn't work.
I tried mongo shell to create a db but it didn't work either.
Either create your database ahead of time (using either the portal or the Azure SDK), or create via MongoDB.
Not sure how you tried attaching to testdb but from the shell, you should be able to execute:
use testdb
Once you do this, you can execute something like
db.newcollection.save({foo:"bar"})
This will result in a new collection created, within testdb. You can see this via show dbs:
globaldb:PRIMARY> show dbs
admin 0.000GB
testdb 0.000GB
Note: When you create a collection implicitly this way (vs creating explicitly via either the portal or the Cosmos DB SDK), you will get a default RU setting for the collection (1000 RU currently), and would need to adjust accordingly via the portal afterward to suit your needs.

What is the default dbuser and dbpassword for a MongoDB database provisioned by Heroku and MongoLab?

I am new to Heroku and MongoDB. I created a Heroku app which has an added-on MongoDB by MongoLab.
Everything was set up automatically by Heroku. When I navigated to MongoLab database manager page (SSO protected) it showed a standard MongoDB URL as:
mongodb://<dbuser>:<dbpassword>#dsxxxxxx.mongolab.com:39674/heroku_xxxxxxxx
Those "x" letters represents numbers.
I didn't bother to specify a dbuser and dbpassword at all. So what is the dbuser and dbpassword?
None of these answers are correct, if you want to know your URI to your database go to your project in heroku and look at settings, reveal config vars and you find all the URI
In your terminal, navigate to your project folder and type $ heroku config:get MONGODB_URI to get your Heroku provisioned username and password.
Mongolab provides you with database hosting services using MongoDB as the database engine. This means you have to have a subscription to their services, in order to have access to a MongoDB database. Once you sign up for one of their plans you will have your own database username and database password to authenticate database connections with.
So dbuser will be your MongoDb username and dbpassword will be your MongoDB password. You use these elements to gain access to your own databases and collections.
https://mongolab.com/plans/pricing/
When you create a MongoLab add-on for your Heroku app, a MONGOLAB_URI environment variable is automatically created with connection info for your database add-on:
https://devcenter.heroku.com/articles/mongolab#getting-your-connection-uri

MongoDB authenticating as superuser always fails

I have a fresh MongoDB 2.4.7 installation. I run commands below in first run:
db.getSiblingDB('admin').addUser({
user: 'root',
pwd: 'root',
roles:['userAdminAnyDatabase', 'dbAdminAnyDatabase']
});
After restarting mongod using --auth, running db.auth('root', 'root') on any dbs fails including admin db.
How can i fix it to have a super user (root access) and add other users for dbs?
You need to authenticate against the admin database and the MongoDB documentation recommend to use this user only to create new users and give appropriated permissions. But if you want to have a super user, also add the role readWriteAnyDatabase.

Do I have to be admin to create a new user in MongoDB?

I want to create a new user in MongoDB.
Do I do that by logging in as admin (use admin) and then using the command add user to create new user?
If no users created you can create new user without any authentification, but if you have created admin user for specific database you should authentifcate, and then perform any operation.
Documentation:
If no users are configured in
admin.system.users, one may access the
database from the localhost interface
without authenticating. Thus, from the
server running the database (and thus
on localhost), run the database shell
and configure an administrative user:
$ ./mongo
> use admin
> db.addUser("theadmin", "anadminpassword")
We now have a user created for
database admin. Note that if we have
not previously authenticated, we now
must if we wish to perform further
operations, as there is a user in
admin.system.users.
> db.auth("theadmin", "anadminpassword")
We can view existing users for the
database with the command:
> db.system.users.find()
Now, let's configure a "regular" user
for another database.
> use projectx
> db.addUser("joe", "passwordForJoe")