{"record":{"id":"4c7e940db5ce4dc6","repo":"bytebase/bytebase","slug":"cannot-run-buildinfo-command","errorCode":null,"errorMessage":"cannot run buildInfo command","messagePattern":"cannot run buildInfo command","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"backend/plugin/db/mongodb/sync.go","lineNumber":265,"sourceCode":"\t}\n\tslices.Sort(indexNames)\n\tfor _, name := range indexNames {\n\t\tindexes = append(indexes, indexMap[name])\n\t}\n\treturn indexes, nil\n}\n\nfunc isSystemCollection(collectionName string) bool {\n\treturn strings.HasPrefix(collectionName, \"system.\")\n}\n\n// getVersion returns the version of mongod or mongos instance.\nfunc (d *Driver) getVersion(ctx context.Context) (string, error) {\n\tdatabase := d.client.Database(bytebaseDefaultDatabase)\n\tvar commandResult bson.M\n\tcommand := bson.D{{Key: \"buildInfo\", Value: 1}}\n\tif err := database.RunCommand(ctx, command).Decode(&commandResult); err != nil {\n\t\treturn \"\", errors.Wrap(err, \"cannot run buildInfo command\")\n\t}\n\tversion, ok := commandResult[\"version\"]\n\tif !ok {\n\t\treturn \"\", errors.New(\"cannot get version from buildInfo command result\")\n\t}\n\tv, ok := version.(string)\n\tif !ok {\n\t\treturn \"\", errors.New(\"cannot convert version to string\")\n\t}\n\treturn v, nil\n}\n\n// isDatabaseExist returns true if the database exists.\nfunc (d *Driver) isDatabaseExist(ctx context.Context, databaseName string) (bool, error) {\n\t// We do the filter by hand instead of using the filter option of ListDatabaseNames because we may encounter the following error:\n\t// Unallowed argument in listDatabases command: filter\n\tdatabaseList, err := d.client.ListDatabaseNames(ctx, bson.M{})\n\tif err != nil {","sourceCodeStart":247,"sourceCodeEnd":283,"githubUrl":"https://github.com/bytebase/bytebase/blob/1870550677fe08f0d2a78c07acd27541464eb945/backend/plugin/db/mongodb/sync.go#L247-L283","documentation":"getVersion runs the buildInfo administrative command against the \"admin\" database (bytebaseDefaultDatabase) to learn the server version. If RunCommand fails, the error is wrapped as \"cannot run buildInfo command\". Bytebase needs the version to tailor instance behavior, so instance sync cannot proceed without it.","triggerScenarios":"database.RunCommand(ctx, bson.D{{\"buildInfo\", 1}}) fails during driver Open/instance sync: the connected user lacks permission to run buildInfo, the connection was dropped, or a mongos/router rejected the command.","commonSituations":"Locked-down MongoDB deployments where the bytebase user has only read roles on one database; connecting through a proxy that disallows admin commands; server shutdown or network interruption during connection setup.","solutions":["Grant the connection user the clusterMonitor role on the admin database (buildInfo requires it on secured clusters).","Confirm the connection string points at a reachable mongod/mongos and the admin DB exists.","Retry after checking network/firewall stability between Bytebase and MongoDB.","Check mongod logs for the authorization failure and adjust roles accordingly."],"exampleFix":"// before: user with only read role fails buildInfo\n// after: in mongo shell\nuse admin\ndb.grantRolesToUser(\"bytebase\", [{ role: \"clusterMonitor\", db: \"admin\" }])","handlingStrategy":"try-catch","validationCode":"// confirm the user can run buildInfo before opening the driver\nvar result bson.M\nerr := adminDB.RunCommand(ctx, bson.D{{Key: \"buildInfo\", Value: 1}}).Decode(&result)\n// err != nil means grant clusterMonitor on admin first","typeGuard":null,"tryCatchPattern":"version, err := d.getVersion(ctx)\nif err != nil {\n    var cmdErr mongo.CommandError\n    if errors.As(err, &cmdErr) && cmdErr.Code == 13 {\n        // authorization failure: grant clusterMonitor on admin\n    }\n    return err\n}","preventionTips":["Always grant clusterMonitor on admin to the Bytebase MongoDB user.","Test the connection string with the mongo shell using the same credentials.","Watch for network drops during instance connection setup."],"tags":["mongodb","buildinfo","command","permissions"],"backgroundTag":"database-query-failed","analyzedSha":"1870550677fe08f0d2a78c07acd27541464eb945","analyzedAt":"2026-09-06T21:16:13.665Z","contentChangedAt":"2026-09-06T21:16:13.665Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}