code 13

Fix MongoDB code 13: not authorized on admin to execute command

MongoDB throws code 13 when you try db.createUser without the right auth. Fix it by logging into admin with proper credentials or disabling auth temporarily.

Quick answer

Log into the admin database with the existing admin user, or restart MongoDB without auth if you haven't created an admin yet.

You're getting this because MongoDB's authentication is on, and the user you're connected as doesn't have the userAdminAnyDatabase role. The db.createUser() command requires that role on the database you're targeting. When you run it from the admin database with an unprivileged user, MongoDB responds with code 13: not authorized on admin to execute command.

This usually happens right after you enable auth but before you actually create an admin user. You set security.authorization: enabled in your mongod.conf, restarted, and then tried to create a user—only to get locked out. It's a classic chicken-and-egg problem.

Fix steps

1. Check if you already have an admin user

Run this from your mongo shell:

db.adminCommand({listUsers: 1})

If you see a user with userAdminAnyDatabase or root role, you're fine—just use those credentials. If you get a permission error, you don't have an admin user yet.

2. Log in with admin credentials

If you do have an admin user, connect like this:

mongo --port 27017 -u "yourAdminUser" -p --authenticationDatabase admin

You'll be prompted for the password. After that, switch to admin and create your user:

use admin
db.createUser({
  user: "newUser",
  pwd: "strongPassword",
  roles: ["readWriteAnyDatabase"]
})

You should see Successfully added user if the roles are valid. If not, you'll get a different error message.

3. If you don't have an admin user, restart without auth

Stop the MongoDB service:

sudo systemctl stop mongod    # or: brew services stop mongodb-community

Then start it with the --noauth flag:

mongod --dbpath /var/lib/mongodb --noauth

Or edit your config file to temporarily comment out the security: section (usually in /etc/mongod.conf).

Now connect without credentials:

mongo

Create your admin user immediately:

use admin
db.createUser({
  user: "rootAdmin",
  pwd: "aVeryStrongPass",
  roles: ["root"]
})

Then shut down MongoDB cleanly, restore your auth setting, and start it again. Always test the new login before you rely on it.

Alternative fixes

Use localhost exception

If MongoDB is running with auth enabled and you haven't created any users, you can still access it from localhost. That's a built-in safety net. Connect to the shell from the same machine and you can create the first admin user without auth. It only works for the first user, so this is your best shot if you skipped the noauth step.

mongo --port 27017

Then run the createUser commands as above. After you create that first user, the exception disappears.

Drop and recreate the database (last resort)

If you've got data you can afford to lose, wipe the MongoDB data directory and start fresh. Stop the service, remove /var/lib/mongodb (or wherever your dbpath is), then restart with auth off and create your admin. This nukes all users and data, so only do this if you have backups or nothing valuable.

Prevention tip

The real fix is to always create an admin user before enabling auth. When you set up a new MongoDB instance, create your admin user first, then flip on the auth setting and restart. That way you never get locked out.

Also, keep a backup of your mongod.conf so you can revert if a config change breaks something. And document your admin credentials in a password manager—sounds obvious, but you'd be surprised how many tickets I've seen where the admin forgot the password and couldn't create new users.

If you're scripting this, add a check that lists users before it tries to create one. A simple if (db.getUsers().length == 0) guard can save you from a failed deployment.

Related Errors in Database Errors
0X00001A91 Fix RM_NOT_ACTIVE (0X00001A91) on Windows Server 0X8004D010 Fix XACT_E_UNKNOWNRMGRID (0x8004D010) Fast 0X00001A3B Fix ERROR_TRANSACTION_NOT_FOUND (0x00001A3B) in Windows 0X8004E007 CONTEXT_E_OLDREF 0X8004E007 Fix for COM+ Database

Was this solution helpful?

EP
Erropedia Team
Tech Support Editors
The Erropedia editorial team researches and documents real-world tech errors from across Windows, Linux, macOS, networking, databases, cloud platforms, and more. Every solution is reviewed for accuracy and updated as software and systems evolve.