1Panel-dev/1Panel · error · Error

failed to update mongodb user password ${userName}

Error message

failed to update mongodb user password ${userName}

What it means

buildMongodbPasswordScript (database_mongodb.go:520) runs a bare `updateUser` with only a new pwd. Unlike the bind script it performs no usersInfo pre-check, so the most common ok:0 cause is updating a user that does not exist on that db (MongoDB code 11 UserNotFound); the second cause is password policy rejection.

Source

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

	usernameJSON, err := json.Marshal(username)
	if err != nil {
		return "", err
	}
	passwordJSON, err := json.Marshal(password)
	if err != nil {
		return "", err
	}
	return strings.TrimSpace(fmt.Sprintf(`
const dbName = %s;
const userName = %s;
const password = %s;
const targetDb = db.getSiblingDB(dbName);
const result = targetDb.runCommand({
  updateUser: userName,
  pwd: password
});
if (!result || result.ok !== 1) {
  throw new Error("failed to update mongodb user password " + userName);
}
`, dbNameJSON, usernameJSON, passwordJSON)), nil
}

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(`

View on GitHub (pinned to 5ac7c80881)

Solutions

  1. Verify the user exists on the exact db: `db.getSiblingDB("<db>").runCommand({usersInfo:"<u>"})` — if empty, the user is elsewhere or gone
  2. If the user is missing, re-create it via the bind flow, then rotate the password
  3. Run updateUser manually and read codeName; fix password strength if validation failed
  4. Ensure the exec'd admin credential may changePassword for that user

Example fix

// before: blind update
// db.getSiblingDB("mydb").runCommand({updateUser:"appuser", pwd:NEWPASS})
// after: guarded update
// const t = db.getSiblingDB("mydb");
// if ((t.runCommand({usersInfo:"appuser"}).users||[]).length) t.runCommand({updateUser:"appuser", pwd:NEWPASS});
Defensive patterns

Strategy: validation

Validate before calling

// verify the user exists on the target db before rotating
script := fmt.Sprintf(`print(db.getSiblingDB(%s).runCommand({usersInfo:%s}).users ? 1 : 0)`, dbNameJSON, userJSON)
if out, _ := runMongodbAdminScriptWithStdout(database, script); strings.TrimSpace(out) != "1" {
    return fmt.Errorf("user %s not found on %s; bind first", username, dbName)
}

Try / catch

if err := updateMongodbPassword(...); err != nil {
    if strings.Contains(err.Error(), "failed to update mongodb user password") {
        // differentiate UserNotFound vs password policy via manual updateUser + docker logs
    }
}

Prevention

When it happens

Trigger: updateMongodbPassword (database_mongodb.go:346) called with a `database/connectionName` whose target db does not own the user; user was deleted beforehand; new password violates passwordValidationRegex.

Common situations: Password rotation for a user that was moved to the admin authenticationDatabase; rotation immediately after someone deleted the user; strict password policy deployments.

Related errors


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