{"record":{"id":"83ecc3190d22d89d","repo":"1Panel-dev/1Panel","slug":"failed-to-load-mongodb-user-username","errorCode":null,"errorMessage":"failed to load mongodb user ${userName}","messagePattern":"failed to load mongodb user (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"agent/app/service/database_mongodb.go","lineNumber":472,"sourceCode":"\tif err != nil {\n\t\treturn \"\", err\n\t}\n\tpasswordJSON, err := json.Marshal(password)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\treturn strings.TrimSpace(fmt.Sprintf(`\nconst dbName = %s;\nconst userName = %s;\nconst password = %s;\nconst targetDb = db.getSiblingDB(dbName);\nconst userInfo = targetDb.runCommand({\n  usersInfo: userName,\n  showCredentials: false,\n  showCustomData: false\n});\nif (!userInfo || userInfo.ok !== 1) {\n  throw new Error(\"failed to load mongodb user \" + userName);\n}\nconst roles = [{ role: \"readWrite\", db: dbName }];\nif (Array.isArray(userInfo.users) && userInfo.users.length > 0) {\n  const result = targetDb.runCommand({\n    updateUser: userName,\n    pwd: password,\n    roles: roles\n  });\n  if (!result || result.ok !== 1) {\n    throw new Error(\"failed to update mongodb user \" + userName);\n  }\n} else {\n  const result = targetDb.runCommand({\n    createUser: userName,\n    pwd: password,\n    roles: roles\n  });\n  if (!result || result.ok !== 1) {","sourceCodeStart":454,"sourceCodeEnd":490,"githubUrl":"https://github.com/1Panel-dev/1Panel/blob/5ac7c808815b0691009cd390414f03471312262f/agent/app/service/database_mongodb.go#L454-L490","documentation":"First guard in buildMongodbBindUserScript (database_mongodb.go:472): `targetDb.runCommand({ usersInfo: userName })` returned a falsy result or `ok !== 1`. The script uses usersInfo to decide between createUser and updateUser, so this failure aborts before any write. ok:0 here typically means the authenticated user cannot view users on that db (requires viewUser privilege) or the command was rejected due to auth.","triggerScenarios":"bindMongodbUser (database_mongodb.go:335) invoked with a `from` of local while the stored container credential lacks userAdmin/viewUser on dbName; mongosh connected to a db where the user is not visible; authentication failed so runCommand results degrade.","commonSituations":"Bind/create user flow run right after install with a custom (non-root) admin user; password for the admin account changed directly in MongoDB after install; user exists on the `admin` authenticationDatabase instead of the target db.","solutions":["Check the container credential's roles with connectionStatus (see error 0) and fix it to root/userAdminAnyDatabase if limited","Run `db.getSiblingDB(\"<db>\").runCommand({usersInfo:\"<user>\", showCredentials:false})` manually and read the error","If the admin credential is wrong, correct username/password on the 1Panel database connection record and re-run bind"],"exampleFix":"// manual probe\n// mongosh -u root -p <pass> --authenticationDatabase admin --eval \\\n//   'printjson(db.getSiblingDB(\"mydb\").runCommand({usersInfo:\"appuser\",showCredentials:false}))'","handlingStrategy":"validation","validationCode":"// pre-check visibility of the user before binding\nscript := fmt.Sprintf(`const r=db.getSiblingDB(%s).runCommand({usersInfo:%s,showCredentials:false}); print(r.ok)`, dbNameJSON, userJSON)\nout, err := runMongodbAdminScriptWithStdout(database, script) // expect \"1\"","typeGuard":null,"tryCatchPattern":"if err := bindMongodbUser(...); err != nil {\n    if strings.Contains(err.Error(), \"failed to load mongodb user\") {\n        // credential/privilege problem: show roles check output, not the raw string\n    }\n}","preventionTips":["Use root for the container's admin connection; scope app users instead","Keep user creation and the admin credential on the same authenticationDatabase"],"tags":["mongodb","mongosh","usersinfo","authorization"],"backgroundTag":null,"analyzedSha":"5ac7c808815b0691009cd390414f03471312262f","analyzedAt":"2026-08-15T14:02:06.953Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}