I am trying to create a simple user with the rights permission to access to any database and can do any actions. When I trying to execute the createUser command I got this error:

db.createUser({ user: "mongoadmin" , pwd: "mongoadmin", roles: ["userAdminAnyDatabase", "dbAdminAnyDatabase", "readWriteAnyDatabase"]})
2015-08-20T17:09:42.300+0000 E QUERY    Error: couldn't add user: No role named userAdminAnyDatabase@new_vehicles_catalog

The problem above only happen when I enable the auth configuration and I need it.

So, How do I create an user with admin permission for any database. I want it because I configured my mongo services to uses authentication connection. If I want to execute a dump of my data I have to use this authentication parameters.

Please any help?

Using mongo version 3.0.5.

the service is on Amazon Linux AMI 2015.03 (HVM), SSD Volume Type - ami-1ecae776

2 Answers 2


The '...AnyDatabase' roles should be added to the admin database for the user:

use admin
db.createUser({ user: "mongoadmin" , pwd: "mongoadmin", roles: ["userAdminAnyDatabase", "dbAdminAnyDatabase", "readWriteAnyDatabase"]})

Any database:

  • Database User Roles
  • Database Administration Roles

Admin database:

  • Cluster Administration Roles
  • Backup and Restoration Roles
  • All-Database Roles

Extra: To give different roles on different database and admin:

db.createUser({user:"replSetManager",pwd:"password",roles:[{role:"clusterManager",db:"admin"},{role:"dbOwner", db:"adminsblog"},{role:"readWrite", db:"departmentblog"},{role:"read", db:"otherblog"}]})
  • 1
    I create the mongoadmin user on admin database, but when I try to do any action in other database I get an error of authorization.
    – Robert
    Commented Aug 21, 2015 at 13:14
  • What actions did you try? Have a look at : docs.mongodb.org/manual/reference/built-in-roles Commented Aug 24, 2015 at 5:08
  • No role named userAdminAnyDatabase
    – user269867
    Commented Feb 23, 2021 at 22:23

As per MongoDB BOL Enable Auth With access control enabled, ensure you have a user with userAdmin or userAdminAnyDatabase role in the admin database. This user can administrate user and roles such as: create users, grant or revoke roles from users, and create or modify customs roles.

You can create users either before or after enabling access control. If you enable access control before creating any user, MongoDB provides a localhost exception which allows you to create a user administrator in the admin database. Once created, you must authenticate as the user administrator to create additional users as needed.


Here is the following procedure through which you can Enable Auth. First adds a user administrator to a MongoDB instance running without access control and then enables access control.

mongod --port 27017 --dbpath /data/db1

C:\Program Files\MongoDB\Server\3.6\bin>mongod --auth --port 27017 --dbpath /data/db1
2018-01-02T00:04:21.038-0700 I CONTROL  [initandlisten] MongoDB starting : pid=8048 port=27017 dbpath=/data/db1 64-bit host=ACSD140013
2018-01-02T00:04:21.038-0700 I CONTROL  [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2
2018-01-02T00:04:21.039-0700 I CONTROL  [initandlisten] db version v3.6.0
2018-01-02T00:04:21.039-0700 I CONTROL  [initandlisten] git version: a57d8e71e6998a2d0afde7edc11bd23e5661c915
2018-01-02T00:04:21.039-0700 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.1u-fips  22 Sep 2016
2018-01-02T00:04:21.039-0700 I CONTROL  [initandlisten] allocator: tcmalloc
2018-01-02T00:04:21.039-0700 I CONTROL  [initandlisten] modules: none
2018-01-02T00:04:21.039-0700 I CONTROL  [initandlisten] build environment:
2018-01-02T00:04:21.039-0700 I CONTROL  [initandlisten]     distmod: 2008plus-ssl
2018-01-02T00:04:21.040-0700 I CONTROL  [initandlisten]     distarch: x86_64
2018-01-02T00:04:21.040-0700 I CONTROL  [initandlisten]     target_arch: x86_64
2018-01-02T00:04:21.040-0700 I CONTROL  [initandlisten] options: { net: { port: 27017 }, security: { authorization: "enabled" }, storage: { dbPath: "/data/db1" } }
2018-01-02T00:04:21.044-0700 I -        [initandlisten] Detected data files in /data/db1 created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
2018-01-02T00:04:21.044-0700 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=1508M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),statistics_log=(wait=0),verbose=(recovery_progress),
2018-01-02T00:04:21.557-0700 I STORAGE  [initandlisten] WiredTiger message [1514876661:556397][8048:140720576475904], txn-recover: Main recovery loop: starting at 1/35584
2018-01-02T00:04:21.677-0700 I STORAGE  [initandlisten] WiredTiger message [1514876661:676479][8048:140720576475904], txn-recover: Recovering log 1 through 2
2018-01-02T00:04:21.792-0700 I STORAGE  [initandlisten] WiredTiger message [1514876661:792524][8048:140720576475904], txn-recover: Recovering log 2 through 2
2018-01-02T00:04:23.008-0700 I CONTROL  [initandlisten]
2018-01-02T00:04:23.008-0700 I CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
2018-01-02T00:04:23.009-0700 I CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server.
2018-01-02T00:04:23.010-0700 I CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP
2018-01-02T00:04:23.010-0700 I CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
2018-01-02T00:04:23.010-0700 I CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
2018-01-02T00:04:23.010-0700 I CONTROL  [initandlisten] **          server with --bind_ip to disable this warning.
2018-01-02T00:04:23.011-0700 I CONTROL  [initandlisten]
2018-01-02T00:04:23.011-0700 I CONTROL  [initandlisten]
2018-01-02T00:04:23.011-0700 I CONTROL  [initandlisten] ** WARNING: The file system cache of this machine is configured to be greater than 40% of the total memory. This can lead to increased memory pressure and poor performance.
2018-01-02T00:04:23.011-0700 I CONTROL  [initandlisten] See http://dochub.mongodb.org/core/wt-windows-system-file-cache
2018-01-02T00:04:23.012-0700 I CONTROL  [initandlisten]
2018-01-02T10:04:23.320+0300 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db1/diagnostic.data'
2018-01-02T10:04:23.322+0300 I NETWORK  [initandlisten] waiting for connections on port 27017
2018-01-02T10:05:09.214+0300 I NETWORK  [listener] connection accepted from #1 (1 connection now open)

Connect to the instance

For example, connect a mongo shell to the instance.

mongo --port 27017

Specify additional command line options as appropriate to connect the mongo shell to your deployment, such as --host.

Create the user administrator

For example here in the admin database, add a user with the userAdminAnyDatabase role. For example, the following creates the user myUserAdmin in the admin database:

> use admin
switched to db admin
> db.createUser(
...   {
...     user: "mongoadmin",
...     pwd: "mongoadmin",
...     roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
...   }
... )
Successfully added user: {
        "user" : "mongoadmin",
        "roles" : [
                        "role" : "userAdminAnyDatabase",
                        "db" : "admin"

NOTE: The database where you create the user (in this example, admin) is the user’s authentication database. Although the user would authenticate to this database, the user can have roles in other databases; i.e. the user’s authentication database does not limit the user’s privileges.

Disconnect the mongo shell.

Re-start the MongoDB instance with access control

Re-start the mongod instance with the --auth command line option or, if using a configuration file, the security.authorization setting.

mongod --auth --port 27017 --dbpath /data/db1

Clients that connect to this instance must now authenticate themselves as a MongoDB user. Clients can only perform actions as determined by their assigned roles.

Connect and authenticate as the user administrator

Using the mongo shell, you can:

  • Connect with authentication by passing in user credentials, or

  • Connect first withouth authentication, and then issue the db.auth() method to authenticate.

To authenticate during connection Start a mongo shell with the -u <username>, -p <password>, and the --authenticationDatabase <database> command line options:

C:\Program Files\MongoDB\Server\3.6\bin>mongo --port 27017 -u "mongoadmin" -p "mongoadmin" --authenticationDatabase "admin"
MongoDB shell version v3.6.0
connecting to: mongodb://
MongoDB server version: 3.6.0
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
Questions? Try the support group
2018-01-02T10:05:09.248+0300 I STORAGE  [main] In File::open(), CreateFileW for 'H:\\.mongorc.js' failed with Access is denied.

To authenticate after connecting

Connect the mongo shell to the mongod:

mongo --port 27017

Switch to the authentication database (in this case, admin), and use db.auth(, ) method to authenticate:

> use admin
switched to db admin
> db.auth("mongoadmin","mongoadmin")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.