1Panel-dev/1Panel · error · Error

failed to list mongodb databases

Error message

failed to list mongodb databases

What it means

loadLocalMongodbDatabases (database_mongodb.go:542) executes `db.adminCommand({ listDatabases: 1 })` through runMongodbAdminScriptWithStdout; ok !== 1 means the command was rejected. listDatabases requires cluster-wide privileges (root/clusterManager/readAnyDatabase); a limited user, failed auth, or connecting through a URI that lands on a mongos/secondary that refuses admin commands produces ok:0.

Source

Thrown at agent/app/service/database_mongodb.go:542

type mongodbSyncItem struct {
	Name     string `json:"name"`
	Username string `json:"username"`
}

func loadMongodbDatabases(req dto.MongodbLoadDB) ([]mongodbSyncItem, error) {
	if req.From == constant.AppResourceRemote {
		return loadRemoteMongodbDatabases(req.Database)
	}
	return loadLocalMongodbDatabases(req.Database)
}

func loadLocalMongodbDatabases(database string) ([]mongodbSyncItem, error) {
	script := strings.TrimSpace(`
const systemDbs = new Set(["admin", "config", "local"]);
const result = db.adminCommand({ listDatabases: 1, nameOnly: false });
if (!result || result.ok !== 1) {
  throw new Error("failed to list mongodb databases");
}
const items = result.databases
  .filter(item => !systemDbs.has(item.name))
  .map(item => ({ name: item.name, username: "" }));
print("__1panel_json_begin__");
print(JSON.stringify(items));
print("__1panel_json_end__");
`)
	stdout, err := runMongodbAdminScriptWithStdout(database, script)
	if err != nil {
		return nil, err
	}
	items := make([]mongodbSyncItem, 0)
	jsonResult, err := extractMongodbJSONOutput(stdout)
	if err != nil {
		return nil, err
	}
	if err := json.Unmarshal(jsonResult, &items); err != nil {

View on GitHub (pinned to 5ac7c80881)

Solutions

  1. Test the exact URI inside the container: run the same `db.adminCommand({listDatabases:1,nameOnly:false})` and printjson the result to see codeName/errmsg
  2. Fix the stored username/password on the database record if auth is the cause (authenticateFailed → ok:0)
  3. Grant the ops user at least clusterManager/ReadAnyDatabase+listDatabases action, or switch the record back to root
  4. If a parse of the __1panel_json_begin__ markers fails afterwards, confirm mongosh --quiet output is not polluted by warnings (set --eval output clean)

Example fix

// manual check
// docker exec <c> mongosh -u root -p <pass> --authenticationDatabase admin --quiet --eval \
//   'printjson(db.adminCommand({listDatabases:1,nameOnly:false}))'
Defensive patterns

Strategy: try-catch

Validate before calling

// health-check the admin credential at connection-save time, not at list time
// run a tiny listDatabases probe when the user edits the database record

Try / catch

items, err := loadMongodbDatabases(req)
if err != nil {
    if strings.Contains(err.Error(), "failed to list mongodb databases") {
        // instruct user to verify container credential; include `docker exec` probe command
    }
    return nil, err
}

Prevention

When it happens

Trigger: Opening the MongoDB database list in 1Panel when the stored credential is a restricted user; stored password stale so auth fails; mongosh URI from buildMongodbRestoreURI points at a router/secondary where adminCommand is unavailable.

Common situations: Post-install credential rotation outside 1Panel; deployments that replaced root with a least-privilege ops user; sharded or replicated topologies.

Related errors


AI-assisted analysis of 1Panel-dev/1Panel@5ac7c80881 (2026-08-15). Data as JSON: /api/errors/9aa14a01341ebad6. Report an issue: GitHub.