Refine MySQL status messaging and SQL page copy

This commit is contained in:
yuanzhen869
2026-03-20 10:40:37 +08:00
parent a64725d60c
commit ac9720e7de
3 changed files with 27 additions and 8 deletions

View File

@@ -108,7 +108,7 @@ docker compose down -v
- 直接调用 Compose 内 API 查询 MySQL 主表 `mobilemodels.mm_device_catalog`
- 服务端先将输入归一化为 `alias_norm`
- 页面展示实际执行的 SQL、返回结果和 JSON
- 页面同时展示只读连接信息,便于第三方联调
- 页面同时展示只读连接参数,便于核对配置
### 索引查询

View File

@@ -214,6 +214,20 @@ def run_command(args: list[str]) -> subprocess.CompletedProcess[str]:
)
def sanitize_mysql_message(message: str, host: str | None = None, port: str | None = None) -> str:
lines = [line.strip() for line in str(message or "").splitlines() if line.strip()]
filtered = [line for line in lines if not line.startswith("WARNING:")]
text = "\n".join(filtered or lines)
host_text = host or os.environ.get("MYSQL_HOST", "mysql")
port_text = port or os.environ.get("MYSQL_PORT", "3306")
if "Can't connect to server on" in text or "Can't connect to MySQL server on" in text:
return f"MySQL 当前无法连接: {host_text}:{port_text}"
if "Access denied" in text:
return f"MySQL 账号或密码无效: {host_text}:{port_text}"
return text or f"MySQL 当前无法连接: {host_text}:{port_text}"
def normalize_text(text: str) -> str:
return NORMALIZE_RE.sub("", (text or "").lower())
@@ -255,7 +269,9 @@ def run_mysql_query(sql: str, database: str | None = None) -> list[dict[str, str
check=False,
)
if proc.returncode != 0:
message = proc.stderr.strip() or proc.stdout.strip() or f"mysql exited with {proc.returncode}"
message = sanitize_mysql_message(
proc.stderr.strip() or proc.stdout.strip() or f"mysql exited with {proc.returncode}"
)
raise RuntimeError(message)
lines = [line for line in proc.stdout.splitlines() if line.strip()]
@@ -369,7 +385,11 @@ def get_status_payload() -> dict[str, object]:
mysql_ready = True
mysql_status = mysql_proc.stdout.strip() or "MySQL ready"
else:
mysql_status = mysql_proc.stderr.strip() or mysql_proc.stdout.strip() or "MySQL unavailable"
mysql_status = sanitize_mysql_message(
mysql_proc.stderr.strip() or mysql_proc.stdout.strip() or "MySQL unavailable",
host=mysql_host,
port=mysql_port,
)
else:
mysql_status = "MySQL auto load disabled"

View File

@@ -570,14 +570,13 @@
<div class="helper-box">
<ul>
<li>输入客户端原始上报值,服务端会先归一化为 <code>alias_norm</code>,再查询 MySQL。</li>
<li>当前读链路使用只读账号,便于模拟第三方直接查库的效果</li>
<li>当前读链路使用只读账号,便于核对 SQL 查询结果与连接配置</li>
<li>推荐新接入优先对接 `mm_device_catalog`,兼容链路再使用视图。</li>
</ul>
</div>
<div class="helper-box">
<p class="helper-title">只读连接信息</p>
<p>可直接用于第三方连库联调。</p>
<p class="helper-title">只读连接参数</p>
<div id="sqlReadonlyInfo" class="credential-grid">
<div class="credential-item">
<span class="credential-label">Host</span>
@@ -1634,8 +1633,8 @@ LIMIT 20;</pre>
} catch (err) {
sqlReadonlyInfoEl.innerHTML = `
<div class="credential-item">
<span class="credential-label">只读连接信息</span>
<p class="credential-value">${escapeHtml(err.message || String(err))}</p>
<span class="credential-label">只读连接参数</span>
<p class="credential-value">暂时无法读取连接参数</p>
</div>
`;
}