mysql-shared
mysql-cli shared rules: config and datasources, global flags, safety model, stable exit codes, error self-repair, and output formats. MUST be loaded with Read before using the mysql-query or mysql-schema skill. Also used directly when the user asks about mysql-cli config, connection failures, read-only/permission errors, exit code meanings, or output formats.
- License 许可
- MIT
- 最近更新
- 2026-09-04
- Stars
- ★ 4
- Forks
- 2
01 前言 Front Matter
- name
- mysql-shared
- version
- 2.0.0
- metadata
- { "binary": "mysql-cli", "license": "MIT", "replaces": "designcomputer/mysql_mcp_server", "config_file": "~/.config/mysql-cli/config.toml", "safety_model": "read-only by default; --write (DML), --write --ddl (DDL), --yes (destructive); every write flag triggers a human confirmation prompt (PreToolUse hook) -- --yes requests execution, it does not self-confirm; SELECT default cap 1000 (--no-limit to disable)", "default_output": "json", "output_formats": "json | table | csv | tsv | jsonl" }
- description
- mysql-cli shared rules: config and datasources, global flags, safety model, stable exit codes, error self-repair, and output formats. MUST be loaded with Read before using the mysql-query or mysql-schema skill. Also used directly when the user asks about mysql-cli config, connection failures, read-only/permission errors, exit code meanings, or output formats.
03 SKILL.md 正文
mysql-cli Shared Rules
CRITICAL: This skill is referenced by
mysql-queryandmysql-schema. Read this file with the Read tool before using either.
mysql-cli is a Go CLI that lets any shell-capable AI agent query MySQL without
an MCP runtime. It is a drop-in replacement for designcomputer/mysql_mcp_server,
re-exposing all read/write capabilities as plain subcommands with JSON by
default and stable exit codes. Agents are the primary caller; the REPL is
only for human debugging.
Convention: assume the
mysql-clibinary is onPATH. If not, install withgo install github.com/AllenMuu/mysql-cli/cmd/mysql-cli@latestor point commands at the built binary.
Before Running
Run these checks before the first command in a session. They are cheap and prevent the most common failures (missing config, unreachable datasource).
1. Config file exists
ls ~/.config/mysql-cli/config.toml
If missing, mysql-cli still works via MYSQL_* env vars or --host/--port/...
overrides, but a config file is the normal path. Resolution priority is
CLI flag > env > project-level (trusted) > global > default (see Project-level Config below). Passwords support ${ENV} placeholders (expanded only in trusted configs).
2. Datasource reachable
A lightweight probe via the read-only databases command (no table scan):
mysql-cli databases -f json
- Exit
0+{"success":true,...}-> reachable, proceed. - Exit
2(CONN_FAILED) -> check host/port/credentials/SSH tunnel. - Exit
10(CONFIG_ERROR) -> checkconfig.tomlsyntax or datasource name.
3. (Optional) Pick a datasource
If the config defines multiple [datasource.<name>] profiles, select one with
-d <name>. Otherwise the default entry is used.
Project-level Config
mysql-cli supports project-level config, structurally identical to the global
config and merged override-style (similar to MCP's .mcp.json).
- Project-level config location:
<project-root>/.config/mysql-cli/config.toml. Discovered by walking up from cwd; the first match wins (stops at home/fs root). Shares the relative path.config/mysql-cli/config.tomlwith the global~/.config/mysql-cli/config.toml; only the root differs. - Trust mechanism (security): project-level config is not loaded by
default. First run
mysql-cli config trust --yesinside the project directory to write the project root into the trust list at~/.config/mysql-cli/trusted.config trustrequires--yesin non-interactive mode (or an interactivey/Nprompt on a TTY) -- AI agents must NOT auto-trust; a human decides. When untrusted, it falls back to global (exit 0) and prints a stderr WARN naming the skipped project config (the warning deliberately omits the trust command, to avoid AI auto-trusting). Suppress with--no-trust-warnorMYSQL_CLI_NO_TRUST_WARN=1; escalate to a hard error with--strict-trust.${ENV}password placeholders expand only in trusted project-level configs - preventing malicious repos from harvesting local env vars or hijacking connections. - Priority chain:
--configflag >MYSQL_CLI_CONFIGenv > project-level (trusted) > global >MYSQL_*field-level overrides > default. When--configorMYSQL_CLI_CONFIGis set, only that file is read and auto-discovery is skipped. - Override-style merge: same-named datasources replace wholesale (project
wins, including SSH subtables); different names form a union;
defaultanddefault_limitfrom project override global.
config subcommands
| Command | Purpose |
|---|---|
mysql-cli config path [-j] |
Show effective file chain + trust status (project marked trusted / untrusted, skipped + global) |
mysql-cli config show [name] [-j] |
Show final merged config (passwords redacted: literal -> ***, ${ENV} shown as-is) |
mysql-cli config trust [dir] |
Trust the project root (defaults to detected root), writing it to the trust list |
mysql-cli config init [--project|--global] [--force] |
Generate a template config.toml |
Self-check tip: when query results or connections don't match expectations, first run
mysql-cli config pathto inspect trust status, thenmysql-cli config showto inspect the merged config (passwords redacted).
Global Flags
All commands share global flags: -d/--datasource, -f/--format (default
json), --write, --ddl, --yes, --limit, --timeout (default 30s),
--config, and connection overrides --host/--port/--user/--password/--db.
Output Formats
Default is JSON (agent-friendly, parse with jq). Success and failure share one
strict envelope.
Success:
{"success":true,"data":{"columns":["id","email"],"rows":[[1,"a@x.com"]]},"rows_affected":0,"meta":{}}
| Field | Meaning |
|---|---|
success |
true on success. |
data.columns |
Column names. |
data.rows |
Row values (text columns come back as strings, not base64). |
rows_affected |
For DML/DDL writes. |
meta |
Reserved metadata. |
Failure:
{"success":false,"error":{"code":"READONLY_VIOLATION","message":"UPDATE requires --write"}}
| Field | Meaning |
|---|---|
success |
false on error. |
error.code |
Stable machine-readable code (see Error Handling). |
error.message |
Human-readable detail. |
Switch human-readable rendering with -f table, -f csv, or -f tsv. In
non-JSON formats, errors render as Error [<CODE>]: <message>.
Default row cap
When a SELECT has no LIMIT, mysql-cli returns only the first 1000 rows by default
and sets meta.truncated=true (detected via a cap+1 probe, with no extra
query).
--limit N: explicitly request N rows; returned exactly, no truncation probe.--no-limit: disable the default cap and return the full table (dangerous - may blow up context).default_limit(top-level config.toml) /MYSQL_CLI_DEFAULT_LIMIT(env): tune the default cap value.- Priority:
--limit>--no-limit> config > env > 1000. - When you see
truncated:trueand need the full set, use--no-limitor first runSELECT COUNT(*)to assess the size.
--format jsonl: one JSON object per line ({"col":val,...}), NULL as null;
more token-efficient than json. Truncation info goes to stderr.
Error Handling
mysql-cli maps every error to a stable exit code. Parse the exit code (or
error.code in JSON) and apply the fix below, then retry. The read-only and
multi-statement checks run before a connection is opened, so you get the
correct code without touching the database.
| Exit | Code | Meaning | Fix |
|---|---|---|---|
2 |
CONN_FAILED |
Cannot reach MySQL | Check host/port/credentials/SSH tunnel in config.toml; verify with mysql-cli databases. Use -d <name> for the right datasource. |
3 |
READONLY_VIOLATION |
DML without --write |
Re-run with --write. |
4 |
DDL_REQUIRES_WRITE |
DDL missing flags | Re-run with --write --ddl. |
5 |
DESTRUCTIVE_REQUIRES_YES |
Destructive op needs confirmation | Re-run with --yes (and --write, plus --ddl for DDL-class drops). |
6 |
IDENTIFIER_INVALID |
Table/db name not in ^[a-zA-Z0-9_$]+$ |
Use a valid identifier; avoid quotes/spaces. For db.table use the qualified form. |
7 |
MULTI_STATEMENT |
More than one statement passed to query |
Use mysql-cli txn "<s1>" "<s2>" instead. |
8 |
SQL_ERROR |
SQL syntax/semantic error | Run mysql-cli schema <table> to confirm columns/types, then fix the SQL. |
9 |
QUERY_TIMEOUT |
Exceeded --timeout |
Raise --timeout 60s, add --limit, or narrow the WHERE. |
10 |
CONFIG_ERROR |
Config parse error or unknown datasource | Check config.toml TOML syntax, the default value, and datasource name spelling; point --config at the right file. |
11 |
INTERNAL_ERROR |
Internal panic / unexpected error | Retry once; if it persists, file a bug with the command, config.toml (passwords redacted), and stderr. Not recoverable via flag changes. |
Cobra flag/usage errors (e.g. --bogus) have no sentinel match in mapError and fall through to CONFIG_ERROR (exit 10); exit 1 is no longer separately reserved.
Security Model
mysql-cli is read-only by default. Writes are gated in tiers so a missing
flag never silently mutates data.
| Operation class | Required flags |
|---|---|
SELECT / read exploration |
none (default read-only) |
DML (INSERT/UPDATE/DELETE) |
--write |
DDL (CREATE/ALTER/DROP/...) |
--write --ddl |
Destructive (DROP/TRUNCATE, UPDATE/DELETE without WHERE) |
--yes (+ --write, + --ddl for DDL-class) |
Safety flags at a glance:
--writeunlocks DML ·--ddlunlocks DDL (requires--write) ·--yesmarks a destructive op. Every write flag triggers a human confirmation prompt ----yesrequests execution, it does not self-confirm.
Additional guarantees:
- Human-in-the-loop on writes: a
PreToolUsehook (global~/.claude/settings.json) intercepts anymysql-clicall carrying--write/--ddl/--yesand turns it into a Claude Code permission prompt. The AI cannot self-confirm destructive work -- approval is the human's call. - Identifier allowlist: table/db names must match
^[a-zA-Z0-9_$]+$; qualifieddb.tableis allowed. Prevents injection in schema-exploration SQL. - Multi-statement rejection:
queryaccepts a single statement (one trailing;tolerated); multiple statements must usetxn. - Pre-connection gating: read-only and multi-statement checks run before any DB connection, so the right exit code is returned without touching the database.
- Config safety: prefer a read-only DB user (
ro_user) for the default datasource; reserve write-capable users for explicit-dprofiles.
Best Practices
- Explore before you query: run
explore/analyze/schemafirst to confirm column names and types; this preventsSQL_ERROR(exit8). - Bound large results: always add
--limit NtoSELECT, or usesample/readwhich are already capped. - Default to JSON: keep
-f json(the default) so you can parse withjq; switch totableonly when showing data to the user. - Validate WHERE on reads first: before an
UPDATE/DELETE, run the sameWHEREas aSELECT COUNT(*)to confirm scope. - Match flags to intent: DML ->
--write; DDL ->--write --ddl; destructive -> add--yes. Don't add--yesspeculatively. Any write flag triggers a human confirmation prompt;--yesis a request, not a waiver. - Reuse the connection model: each invocation opens and closes its own pool (and SSH tunnel). Don't try to hold connections across calls; just issue separate commands.
- Prefer read-only DB users: configure the default datasource with a
read-only user; force writes to go through an explicit
-dprofile.
04 同仓库更多 Skills
mysql-schema
Explore MySQL schema with mysql-cli: list databases/tables, table structure, sample/read rows, database overview, one-shot analyze. Use when the user asks about table structures, columns, types, indexes, listing tables/databases, or sampling data. All read-only; no safety flags needed. For DML/DDL/transactions use the mysql-query skill.
mysql-query
Run SQL with mysql-cli: SELECT queries, DML (INSERT/UPDATE/DELETE), DDL (CREATE/ALTER/DROP), and multi-statement atomic transactions. Use when the user asks to run SQL, query data, insert/update/delete rows, execute transactions, or perform DDL. Read-only JSON output by default, stable exit codes, and tiered gating for write operations.
good
A valid skill used only by the skill-format-check self-test.
bad-no-frontmatter
暂无简介
wrong-name
The name does not match the parent directory.
bad-missing-version
Missing the required version field.