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

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.

Related

After migrating to MongoDB Atlas, no previous users can authenticate

I assume I am doing something wrong, but the documentation has not pointed me in the right direction yet.
I am migrating a self-hosted MongoDB instance database to the MongoDB Atlas platform. I have successfully uploaded a dump using mongorestore, but now none of my users objects can authenticate. I want to say I read somewhere in the docs that you can't migrate pre-existing users or something?
This is the command I am using to restore from a dump
mongorestore --host <MY_CLUSTER>:27017 --nsExclude 'admin.system.*' --ssl --username <USER> --password <PASSWORD> --authenticationDatabase admin --gzip --db=<DB_NAME> .backups/12-23-2019/backup/
You cannot restore users to atlas structure as mentioned before
you have to configure a mongocli environment to mass create users in your Atlas cluster, unless you want to use the "Database Access" panel and create one by one (painful).
Not gonna go into details about how to configure mongocli, but once you've done with it, I made a simple script that retrieve all the users from you original server and script into the create users into mongocli, it uses mongo shell and jq.
mongo mongodb://youroriginalcluster --quiet --eval 'db.system.users.find({}, {"user" : 1, "db" : 1, "roles" : 1}).toArray();' | jq -r '.[] | " mongocli atlas dbuser create --username \(.user) --password PASSWORD --role \(.roles[].role)#\(.db)"'
Still needs some minor adjustments and it does not retrieve the password, but it should save you some time
mongorestore has a specific option for restoring users and roles: --restoreDbUsersAndRoles (note that it works only if you backed an entire instance or used the --dumpDbUsersAndRoles option with mongodump for a single --db).
Regardless of this, Atlas does not support migrating users from the source database since it manages users from the Atlas console rather than in the database itself. Users can be created in Atlas using the Database Access menu option.
From the Atlas Manual:
Atlas fully manages MongoDB database user creation. If the source cluster enforces authentication, use the mongorestore --nsExclude to exclude the admin.system.* namespace. You cannot migrate any existing user or role information to Atlas.

Root user can't connect to any database with 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.

Why is db.auth() only working after selecting a particular database?

Right after I launch mongo, running
db.auth("admin", "myGreatPassword")
returns 0. But doing this:
use admin
db.auth("admin", "myGreatPassword")
returns 1.
I find this strange because the admin user has root privileges and can access all databases and do anything.
What could be the reason for the above behavior? Why do I need to first select a particular database, in this case admin, to log in?
With mongodb you authenticate against a particular database: that database contains the collections that define the users and their roles. That user may then be authorised to do things on multiple databases, but the act of logging in happens against a specific database.
When using the command line tools this is the --authenticationDatabase option, in the mongo shell the authentication commands run against the current database.

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

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")