{"record":{"id":"8ba02fa845d0ce4d","repo":"coollabsio/coolify","slug":"mysql-database-not-found","errorCode":null,"errorMessage":"MYSQL_DATABASE not found","messagePattern":"MYSQL_DATABASE not found","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"app/Jobs/DatabaseBackupJob.php","lineNumber":191,"sourceCode":"                    $commands[] = \"docker exec $this->container_name env | grep MYSQL_\";\n                    $envs = instant_remote_process($commands, $this->server, true, false, null, disableMultiplexing: true);\n                    $envs = str($envs)->explode(\"\\n\");\n\n                    $rootPassword = $envs->filter(function ($env) {\n                        return str($env)->startsWith('MYSQL_ROOT_PASSWORD=');\n                    })->first();\n                    if ($rootPassword) {\n                        $this->database->mysql_root_password = str($rootPassword)->after('MYSQL_ROOT_PASSWORD=')->value();\n                    }\n\n                    $db = $envs->filter(function ($env) {\n                        return str($env)->startsWith('MYSQL_DATABASE=');\n                    })->first();\n\n                    if ($db) {\n                        $databasesToBackup = str($db)->after('MYSQL_DATABASE=')->value();\n                    } else {\n                        throw new \\Exception('MYSQL_DATABASE not found');\n                    }\n                } elseif (str($databaseType)->contains('mariadb')) {\n                    $this->container_name = \"{$this->database->name}-$serviceUuid\";\n                    $this->directory_name = $serviceName.'-'.$this->container_name;\n                    $commands[] = \"docker exec $this->container_name env\";\n                    $envs = instant_remote_process($commands, $this->server, true, false, null, disableMultiplexing: true);\n                    $envs = str($envs)->explode(\"\\n\");\n                    $rootPassword = $envs->filter(function ($env) {\n                        return str($env)->startsWith('MARIADB_ROOT_PASSWORD=');\n                    })->first();\n                    if ($rootPassword) {\n                        $this->database->mariadb_root_password = str($rootPassword)->after('MARIADB_ROOT_PASSWORD=')->value();\n                    } else {\n                        $rootPassword = $envs->filter(function ($env) {\n                            return str($env)->startsWith('MYSQL_ROOT_PASSWORD=');\n                        })->first();\n                        if ($rootPassword) {\n                            $this->database->mariadb_root_password = str($rootPassword)->after('MYSQL_ROOT_PASSWORD=')->value();","sourceCodeStart":173,"sourceCodeEnd":209,"githubUrl":"https://github.com/coollabsio/coolify/blob/70b9acc42467278373e00de77abb40684e25b395/app/Jobs/DatabaseBackupJob.php#L173-L209","documentation":"For MySQL databases that belong to a service, DatabaseBackupJob runs docker exec <container> env and parses the MYSQL_DATABASE= line to determine which database(s) to dump. If no line starts with MYSQL_DATABASE=, it throws — without that value the job cannot name a database to back up. (The equivalent MARIADB_DATABASE is handled in the mariadb branch.)","triggerScenarios":"Backing up a service-based MySQL database whose container was started without MYSQL_DATABASE in its environment: custom env overrides dropped it, the service template omitted it, or the variable was set to an empty name.","commonSituations":"Users override the service's environment and accidentally remove MYSQL_DATABASE; templates/custom compose files that rely only on MYSQL_ROOT_PASSWORD; databases created manually inside the container instead of via the initial env.","solutions":["Add MYSQL_DATABASE=<name> to the service database's environment in Coolify and redeploy the service.","Verify inside the container: docker exec <container> env | grep MYSQL_DATABASE — the line must exist and be non-empty.","If you need multiple databases, set MYSQL_DATABASE to the primary one and back up others explicitly, since the job backs up the value it finds."],"exampleFix":"# before: service database container lacks the variable\nenvironment:\n  - MYSQL_ROOT_PASSWORD=rootpw\n\n# after: name the database to back up\nenvironment:\n  - MYSQL_ROOT_PASSWORD=rootpw\n  - MYSQL_DATABASE=main","handlingStrategy":"validation","validationCode":"// Pre-flight inside the job before failing hard\n$envOutput = instant_remote_process([\"docker exec {$this->container_name} env\"], $this->server, true, false, null, disableMultiplexing: true);\nif (! str($envOutput)->explode(\"\\n\")->contains(fn ($line) => str($line)->startsWith('MYSQL_DATABASE='))) {\n    // surface actionable feedback instead of a bare exception\n    throw new \\RuntimeException(\"MYSQL_DATABASE is not set on container {$this->container_name}; set it in the service environment.\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    // ... backup logic that reads MYSQL_DATABASE ...\n} catch (\\Exception $e) {\n    if ($e->getMessage() === 'MYSQL_DATABASE not found') {\n        // notify with a fix hint; mark execution failed with context\n        $this->backup->executions()->create([\n            'status' => 'failed',\n            'message' => 'Set MYSQL_DATABASE in the service database environment and redeploy.',\n        ]);\n\n        return;\n    }\n    throw $e;\n}","preventionTips":["Always define MYSQL_DATABASE (and MYSQL_ROOT_PASSWORD) when creating MySQL service databases.","After editing a service's environment, redeploy and run docker exec <c> env | grep MYSQL to confirm.","When overriding service env, merge rather than replace so required variables survive."],"tags":["backup","mysql","environment-variables","docker","service"],"backgroundTag":"missing-environment-variable","analyzedSha":"70b9acc42467278373e00de77abb40684e25b395","analyzedAt":"2026-08-17T01:41:01.313Z","schemaVersion":2},"datasetVersion":"2026-08-17T04:17:16.089Z"}