CLI 目录 横向对比 FCRS-1.0 标准 /llms.txt
← 返回 CLI 目录

MySQL CLI

$ mysql 官方出品 高风险
Oracle · 数据库与数据处理 · GPL-2.0 · macos / linux / windows

经典的 mysql 客户端用于向 MySQL 与 MariaDB 服务器执行查询及导入 .sql 文件。支持 -e 非交互执行、--batch(-B)TSV 输出以及 --init-command 会话级设置,但缺少原生 JSON 输出参数。

FCRS-1.0 综合评分
77 /100
良好 (Good)
高置信度 (实测)
零摩擦安装命令
brew install mysql-client
CLI vs MCP 选型建议 优先选 CLI

mysql --batch --raw -e "..." 速度极快且随处可用;需 JSON 时可结合 SQL JSON_ARRAYAGG(JSON_OBJECT(...)) 或管道转给 mlr。

Agent 友好度体检单

本地探针实测证据
— 原生 JSON 输出
— 字段裁剪
✓ 无头非交互
— 支持 Dry-Run
— 只读模式开关
— 支持 NO_COLOR
✓ 自省/Schema 发现
— 官方 SKILL.md
实测版本
mysql Ver 9.3.0 for macos15.2 on arm64 (Homebrew)
冷启动耗时
66 ms
退出码 (help / 非法参数)
0 / 2
NO_COLOR 无色码污染 / 无 TTY 不挂起
Yes / Safe
抓取的 --help 摘要
mysql  Ver 9.3.0 for macos15.2 on arm64 (Homebrew)
Copyright (c) 2000, 2025, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Usage: mysql [OPTIONS] [database]
  -?, --help          Display this help and exit.
  -I, --help          Synonym for -?
  --auto-rehash       Enable automatic rehashing. One doesn't need to use
                      'rehash' to get table and field completion, but startup
                      and reconnecting may take a longer time. Disable with
                      --disable-auto-rehash.
                      (Defaults to on; use --skip-auto-rehash to disable.)
  -A, --no-auto-rehash 
                      No automatic rehashing. One has to use 'rehash' to get
                      table and field completion. This gives a quicker start of
                      mysql and disables rehashing on reconnect.
  --auto-vertical-output 
                      Automatically switch to vertical output mode if the
                      result is wider than the terminal width.
  -B, --batch         Don't use history file. Disable interactive behavior.
                      (Enables --silent.)
  --bind-address=name IP address to bind to.
  --binary-as-hex     Print binary data as hex. Enabled by default for
                      interactive terminals.
  --character-sets-dir=name 
                      Directory for character set files.
  --column-type-info  Display column type information.
  -c, --comments      Preserve comments. Send comments to the server. The
                      default is --comments (keep comments), disable with
                      --skip-comments.
                      (Defaults to on; use --skip-comments to disable.)
  -C, --compress      Use compression in server/client protocol.
  -#, --debug[=#]     This is a non-debug version. Catch this and exit.
  --debug-check       This is a non-debug version. Catch this and exit.
  -T, --debug-info    This is a non-debug version. Catch this and exit.
  -D, --database=name Database to use.
  --default-character-set=name 
                      Set the default character set.
  --delimiter=name    Delimiter to be used.
  --enable-cleartext-plugin 
                      Enable/disable the clear text authentication plugin.
  -e, --execute=name  Execute command and quit. (Disables --force and history
                      file.)
  -E, --vertical      Print the output

FCRS-1.0 六维评分拆解

Agent 友好度 17 / 25

支持 -e、--batch、-N 与 --xml,但缺少原生 --json 开关且历史鉴权方式略繁琐。

  • Supports -e, --batch, -N; no native --json output
安全与权限边界 18 / 25

可通过 --init-command="SET SESSION TRANSACTION READ ONLY;" 锁定只读,但默认未开启 --safe-updates(-U),且 -p 易在 ps 进程列表中泄露密码。

  • Server-side read-only via --init-command; -p leaks passwords in argv
安装与运行摩擦 13 / 15

各大系统与容器中均可轻松安装 mysql-client 或 mariadb-client。

  • Standard mysql-client package across OSes
能力覆盖与边际价值 13 / 15

原生 C/C++ 客户端,查询与批量导入性能优异。

  • Native wire-protocol performance for queries and batch scripts
维护与来源背书 9 / 10

Oracle 官方 mysql/mysql-server 核心客户端,持续稳定维护。

  • Official Oracle MySQL client
文档与可验证性 7 / 10

dev.mysql.com 官方手册完整,但需注意 MySQL 与 MariaDB 客户端参数细微差异。

  • Official MySQL Reference Manual

安全边界、鉴权与放权指南

鉴权方式
本地配置文件 环境变量密钥
凭证存储位置

连接配置存于 ~/.my.cnf 或 --defaults-extra-file;密码可通过 MYSQL_PWD(不推荐)或 mysql_config_editor 生成的 ~/.mylogin.cnf 提供。

潜在副作用
只读查询 本地写入 远端写入 破坏性删除
费用与配额提示

基于 GPL-2.0 协议 100% 免费开源。

可复制到 CLAUDE.md / AGENTS.md 的安全护栏
# MySQL CLI Agent Rules
- Always pass `-e "<sql>"` and `--batch` (`-B`); never start interactive `mysql` shell.
- Prefix read queries with `--init-command="SET SESSION TRANSACTION READ ONLY;"`.
- Use `--defaults-extra-file` or `~/.mylogin.cnf` instead of `-p<password>` on the command line.

Agent 高频安全命令配方

只读查询(安全推荐)

以只读会话模式执行批处理查询
mysql --defaults-extra-file=.my.cnf --batch --init-command="SET SESSION TRANSACTION READ ONLY;" -e "SHOW TABLES;"

在服务端强制启用只读事务,并返回制表符分隔的结果行。

借助 SQL 函数输出 NDJSON 行数据
mysql --defaults-extra-file=.my.cnf --batch -N -e "SELECT JSON_OBJECT('id', id, 'name', name) FROM users LIMIT 10;"

使用 -N 隐藏表头,每行输出一个 JSON 对象。

核心优势

  • + --batch -N -e 配合 JSON_OBJECT() 可稳定产出易解析的 TSV 或 NDJSON。
  • + --init-command="SET SESSION TRANSACTION READ ONLY;" 可防止误写。
  • + 几乎在所有 Linux 与容器镜像中都有现成软件包。

注意事项与踩坑点

  • ! 没有内置 --json 输出参数(仅支持表格、TSV、--xml 和 --html)。
  • ! 命令行直接传 -p<secret> 会在系统进程列表中泄露密码。

已验证兼容的 Agent 宿主

Claude Code Codex CLI Gemini CLI

Follow 家族生态联动