{"record":{"id":"9aa14a01341ebad6","repo":"1Panel-dev/1Panel","slug":"failed-to-list-mongodb-databases","errorCode":null,"errorMessage":"failed to list mongodb databases","messagePattern":"failed to list mongodb databases","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"agent/app/service/database_mongodb.go","lineNumber":542,"sourceCode":"\ntype mongodbSyncItem struct {\n\tName     string `json:\"name\"`\n\tUsername string `json:\"username\"`\n}\n\nfunc loadMongodbDatabases(req dto.MongodbLoadDB) ([]mongodbSyncItem, error) {\n\tif req.From == constant.AppResourceRemote {\n\t\treturn loadRemoteMongodbDatabases(req.Database)\n\t}\n\treturn loadLocalMongodbDatabases(req.Database)\n}\n\nfunc loadLocalMongodbDatabases(database string) ([]mongodbSyncItem, error) {\n\tscript := strings.TrimSpace(`\nconst systemDbs = new Set([\"admin\", \"config\", \"local\"]);\nconst result = db.adminCommand({ listDatabases: 1, nameOnly: false });\nif (!result || result.ok !== 1) {\n  throw new Error(\"failed to list mongodb databases\");\n}\nconst items = result.databases\n  .filter(item => !systemDbs.has(item.name))\n  .map(item => ({ name: item.name, username: \"\" }));\nprint(\"__1panel_json_begin__\");\nprint(JSON.stringify(items));\nprint(\"__1panel_json_end__\");\n`)\n\tstdout, err := runMongodbAdminScriptWithStdout(database, script)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\titems := make([]mongodbSyncItem, 0)\n\tjsonResult, err := extractMongodbJSONOutput(stdout)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tif err := json.Unmarshal(jsonResult, &items); err != nil {","sourceCodeStart":524,"sourceCodeEnd":560,"githubUrl":"https://github.com/1Panel-dev/1Panel/blob/5ac7c808815b0691009cd390414f03471312262f/agent/app/service/database_mongodb.go#L524-L560","documentation":"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.","triggerScenarios":"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.","commonSituations":"Post-install credential rotation outside 1Panel; deployments that replaced root with a least-privilege ops user; sharded or replicated topologies.","solutions":["Test the exact URI inside the container: run the same `db.adminCommand({listDatabases:1,nameOnly:false})` and printjson the result to see codeName/errmsg","Fix the stored username/password on the database record if auth is the cause (authenticateFailed → ok:0)","Grant the ops user at least clusterManager/ReadAnyDatabase+listDatabases action, or switch the record back to root","If a parse of the __1panel_json_begin__ markers fails afterwards, confirm mongosh --quiet output is not polluted by warnings (set --eval output clean)"],"exampleFix":"// manual check\n// docker exec <c> mongosh -u root -p <pass> --authenticationDatabase admin --quiet --eval \\\n//   'printjson(db.adminCommand({listDatabases:1,nameOnly:false}))'","handlingStrategy":"try-catch","validationCode":"// health-check the admin credential at connection-save time, not at list time\n// run a tiny listDatabases probe when the user edits the database record","typeGuard":null,"tryCatchPattern":"items, err := loadMongodbDatabases(req)\nif err != nil {\n    if strings.Contains(err.Error(), \"failed to list mongodb databases\") {\n        // instruct user to verify container credential; include `docker exec` probe command\n    }\n    return nil, err\n}","preventionTips":["Grant the admin user listDatabases action or use root","Probe the credential after every password change on the install","Pin the mongosh URI's authSource explicitly in buildMongodbRestoreURI"],"tags":["mongodb","listdatabases","authorization","docker-exec"],"backgroundTag":null,"analyzedSha":"5ac7c808815b0691009cd390414f03471312262f","analyzedAt":"2026-08-15T14:02:06.953Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}