{"record":{"id":"2cdbf0582fcaa349","repo":"louislam/uptime-kuma","slug":"database-connection-query-failed-error-message","errorCode":null,"errorMessage":"Database connection/query failed: ${error.message}","messagePattern":"Database connection/query failed: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"server/monitor-types/mssql.js","lineNumber":57,"sourceCode":"                    throw new Error(`Query result did not meet the specified conditions (${result})`);\n                }\n\n                heartbeat.status = UP;\n                heartbeat.msg = \"Query did meet specified conditions\";\n            } else {\n                // Backwards compatible: just check connection and return row count\n                const result = await this.mssqlQuery(monitor.databaseConnectionString, query);\n                heartbeat.ping = dayjs().valueOf() - startTime;\n                heartbeat.status = UP;\n                heartbeat.msg = result;\n            }\n        } catch (error) {\n            heartbeat.ping = dayjs().valueOf() - startTime;\n            // Re-throw condition errors as-is, wrap database errors\n            if (error.message.includes(\"did not meet the specified conditions\")) {\n                throw error;\n            }\n            throw new Error(`Database connection/query failed: ${error.message}`);\n        }\n    }\n\n    /**\n     * Run a query on MSSQL server (backwards compatible - returns row count)\n     * @param {string} connectionString The database connection string\n     * @param {string} query The query to validate the database with\n     * @returns {Promise<string>} Row count message\n     */\n    async mssqlQuery(connectionString, query) {\n        let pool;\n        try {\n            pool = new mssql.ConnectionPool(connectionString);\n            await pool.connect();\n            const result = await pool.request().query(query);\n\n            if (result.recordset) {\n                return \"Rows: \" + result.recordset.length;","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/louislam/uptime-kuma/blob/6b5ea0155793e666666745fb8d6fef1e829543a2/server/monitor-types/mssql.js#L39-L75","documentation":"Catch-all wrapper in MssqlMonitorType.check: any error whose message does NOT contain 'did not meet the specified conditions' is re-thrown as 'Database connection/query failed: <original>'. This captures connection failures, auth failures, timeouts, TLS errors, and SQL syntax errors. Condition-evaluation errors are intentionally re-thrown unchanged so they surface verbatim.","triggerScenarios":"new mssql.ConnectionPool(connectionString).connect() rejects (network unreachable, login failed, timeout), pool.request().query(query) rejects (syntax error, permission denied, object not found), or the single-value helpers throw 'Query returned no results'/'Multiple values...'. The substring guard then forwards everything except condition failures.","commonSituations":"Wrong connection string format (Server=name vs full data-source syntax); SQL Server Browser/port 1433 blocked by firewall; encrypted (TLS) connection required but not negotiated; SQL login disabled / wrong credentials; query references a table in the wrong database/schema; mssql npm driver version mismatch with server.","solutions":["Test the identical connection string with sqlcmd or Azure Data Studio from the Uptime Kuma host to isolate network vs auth vs query.","Confirm port 1433 (or named-instance port) is reachable and SQL Browser (UDP 1434) is allowed if using a named instance.","Verify credentials and that 'SQL Server Authentication' / the AD identity is enabled; check the SQL errorlog for the failed login.","If TLS/encrypt is required, supply the right options in the connection string (e.g. encrypt=true, trustServerCertificate as appropriate).","Run the query standalone to catch syntax/permission errors before relying on the monitor."],"exampleFix":"// before: Server=10.0.0.5,1433;Database=app;User Id=u;Password=p;   (encrypt required by server)\n// after:  Server=10.0.0.5,1433;Database=app;User Id=u;Password=p;Encrypt=true;TrustServerCertificate=true;","handlingStrategy":"retry","validationCode":"// Pre-flight reachability check before relying on the monitor:\nconst net = require('net');\nfunction portOpen(host, port, ms=2000) {\n  return new Promise(res => { const s = new net.Socket(); s.setTimeout(ms);\n    s.on('connect', () => { s.destroy(); res(true); });\n    s.on('timeout', () => { s.destroy(); res(false); });\n    s.on('error', () => res(false));\n    s.connect(port, host); });\n}","typeGuard":null,"tryCatchPattern":"async function withRetry(fn, retries=3, backoffMs=1000) {\n  for (let i=0; i<=retries; i++) { try { return await fn(); } catch (e) {\n    if (/Database connection\\/query failed/.test(e.message) && i<retries) await new Promise(r=>setTimeout(r, backoffMs*2**i)); else throw e; } }\n}","preventionTips":["Keep a known-good connection string in a secret manager and rotate credentials deliberately.","Test connectivity from the Kuma host with sqlcmd before enabling the monitor.","Wrap transient DB failures with bounded exponential-backoff retry, and alert on repeated failures."],"tags":["mssql","sqlserver","database","connection","uptime-kuma"],"backgroundTag":null,"analyzedSha":"6b5ea0155793e666666745fb8d6fef1e829543a2","analyzedAt":"2026-08-12T23:42:12.959Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}