使用说明
sql-mcp
用Rust编写的多后端数据库MCP(模型上下文协议)服务器。它通过MCP标准向LLM助手公开MySQL、PostgreSQL和SQLite数据库,允许他们运行SQL查询、检查表模式、获取查询执行计划和接收性能警告——所有这些都是通过一个简单的工具界面实现的。
特性
- 多后端:通过功能标志支持MySQL、PostgreSQL和SQLite
- 阅读查询:选择、显示、解释——始终允许
- 写入操作:INSERT、UPDATE、DELETE——通过配置选择加入
- DDL操作:CREATE、ALTER、DROP、TRUNCATE--选择加入
- 性能提示:对慢速查询自动提供EXPLAIN+索引建议
- 命名会话:在运行时通过以下方式连接到其他数据库
connect - SSH隧道:访问堡垒/跳转主机(MySQL、PostgreSQL)后面的数据库
- 按架构权限:每个数据库的细粒度写控制
- 连接池:可配置的池大小和超时
- SSL支持:带有可选CA验证的加密连接(MySQL、PostgreSQL)
- Unix套接字支持:通过套接字路径而不是主机:端口连接(MySQL)
快速开始
安装
cargo install --git https://github.com/cecil-the-coder/mysql-mcp
或者从源代码构建:
cargo build --release
使用环境变量运行(MySQL)
export DB_BACKEND=mysql
export DB_HOST=localhost
export DB_PORT=3306
export DB_USER=myuser
export DB_PASS=mypassword
export DB_DATABASE=mydatabase
./sql-mcp
使用环境变量运行(PostgreSQL)
export DB_BACKEND=postgres
export DB_HOST=localhost
export DB_PORT=5432
export DB_USER=myuser
export DB_PASS=mypassword
export DB_DATABASE=mydatabase
./sql-mcp
使用环境变量运行(SQLite)
export DB_BACKEND=sqlite
export DB_PATH=/path/to/mydb.sqlite3
./sql-mcp
Claude桌面配置
添加 ~/Library/Application Support/Claude/claude_desktop_config.json (macOS)或 %APPDATA%\Claude\claude_desktop_config.json (Windows):
{
"mcpServers": {
"mysql": {
"command": "/path/to/sql-mcp",
"env": {
"DB_BACKEND": "mysql",
"DB_HOST": "localhost",
"DB_PORT": "3306",
"DB_USER": "myuser",
"DB_PASS": "mypassword",
"DB_DATABASE": "mydatabase"
}
}
}
}
Claude代码配置
添加 .claude/settings.json (项目)或您的全局克劳德代码设置:
{
"mcpServers": {
"mysql": {
"command": "/path/to/sql-mcp",
"env": {
"DB_HOST": "localhost",
"DB_USER": "myuser",
"DB_DATABASE": "mydatabase"
}
}
}
}
TOML配置文件
创建 sql-mcp.toml 在工作目录(或点)中 MCP_CONFIG_FILE 在自定义路径上):
MySQL:
[connection]
host = "localhost"
port = 3306
user = "myuser"
password = "mypassword"
database = "mydatabase"
[pool]
size = 20
max_rows = 1000
[security]
allow_insert = false
allow_update = false
PostgreSQL:
backend = "postgres"
[connection]
host = "localhost"
port = 5432
user = "myuser"
password = "mypassword"
database = "mydatabase"
SQLite:
backend = "sqlite"
[connection]
path = "/path/to/mydb.sqlite3"
配置按以下顺序加载(最高优先级获胜):
- 环境变量
- TOML配置文件(
sql-mcp.toml或$MCP_CONFIG_FILE) - 内置默认值
A. .env 工作目录中的文件(如果存在)会自动加载。
配置参考
后端
| TOML键 | 环境变量 | 类型 | 默认值 | 描述 |
|---|---|---|---|---|
backend |
DB_BACKEND |
字符串 | mysql |
数据库后端: mysql, postgres,或 sqlite |
连接
| TOML键 | 环境变量 | 类型 | 默认值 | 描述 |
|---|---|---|---|---|
connection.host |
DB_HOST |
字符串 | localhost |
服务器主机名(MySQL、PostgreSQL) |
connection.port |
DB_PORT |
u16 | -- | 服务器端口(默认:MySQL 3306,PostgreSQL 5432) |
connection.path |
DB_PATH |
string | -- | SQLite数据库文件路径 |
connection.user |
DB_USER |
string | -- | 用户名(MySQL、PostgreSQL) |
connection.password |
DB_PASS |
字符串 | "" |
密码(MySQL、PostgreSQL) |
connection.database |
DB_DATABASE |
string | -- | 默认数据库名称 |
connection.connection_string |
DB_CONNECTION_STRING |
string | -- | 完整连接URL;覆盖其他连接字段 |
池
| TOML键 | 环境变量 | 类型 | 默认值 | 描述 |
|---|---|---|---|---|
pool.size |
DB_POOL_SIZE |
u32 | 20 |
池连接的最大数量 |
pool.query_timeout_ms |
DB_QUERY_TIMEOUT |
u64 | 30000 |
每次查询超时(毫秒) |
pool.connect_timeout_ms |
DB_CONNECT_TIMEOUT |
u64 | 10000 |
连接建立超时(毫秒) |
pool.performance_hints |
DB_PERFORMANCE_HINTS |
字符串 | none |
何时运行EXPLAIN: none, auto,或 always |
pool.slow_query_threshold_ms |
DB_SLOW_QUERY_THRESHOLD_MS |
u64 | 500 |
使用的阈值 performance_hints=auto |
pool.max_rows |
DB_MAX_ROWS |
u32 | 1000 |
每次查询返回的行数上限 |
pool.cache_ttl_secs |
DB_CACHE_TTL |
u64 | 60 |
模式自检缓存TTL(秒) |
pool.retry_attempts |
DB_RETRY_ATTEMPTS |
u32 | 2 |
重试瞬态网络错误 |
安全
| TOML键 | 环境变量 | 类型 | 默认值 | 描述 |
|---|---|---|---|---|
security.allow_insert |
DB_ALLOW_INSERT bool的。 false |
允许插入语句 | ||
security.allow_update |
DB_ALLOW_UPDATE bool的。 false |
允许更新语句 | ||
security.allow_delete |
DB_ALLOW_DELETE bool的。 false |
允许DELETE语句 | ||
security.allow_ddl |
DB_ALLOW_DDL bool的。 false |
允许DDL语句(CREATE、ALTER、DROP、TRUNCATE) | ||
security.ssl |
DB_SSL bool的。 false |
连接需要SSL/TLS | ||
security.ssl_accept_invalid_certs |
DB_SSL_ACCEPT_INVALID_CERTS bool的。 false |
跳过证书验证 | ||
security.ssl_ca |
DB_SSL_CA |
string | -- | PEM CA证书文件的路径 |
security.schema_permissions |
DB_SCHEMA_<NAME>_PERMISSIONS |
地图 | {} |
按架构写入权限覆盖 |
security.allow_runtime_connections |
DB_ALLOW_RUNTIME_CONNECTIONS bool的。 false |
允许 connect 在运行时接受原始凭据 |
||
security.max_sessions |
DB_MAX_SESSIONS |
u32 | 50 |
并发命名会话的最大数量 |
SSH隧道(仅MySQL、PostgreSQL)
| TOML键 | 环境变量 | 类型 | 默认值 | 描述 |
|---|---|---|---|---|
ssh.host |
DB_SSH_HOST |
string | -- | SSH堡垒主机名 |
ssh.port |
DB_SSH_PORT |
u16 | 22 |
SSH服务器端口 |
ssh.user |
DB_SSH_USER |
string | -- | SSH用户名 |
ssh.private_key |
DB_SSH_PRIVATE_KEY |
string | -- | PEM私钥文件的路径 |
ssh.known_hosts_check |
DB_SSH_KNOWN_HOSTS_CHECK |
字符串 | strict |
主机密钥验证: strict, accept-new,或 insecure |
ssh.known_hosts_file |
DB_SSH_KNOWN_HOSTS_FILE |
string | -- | 覆盖known_hosts文件的路径 |
功能标志
# Build all backends (default)
cargo build
# MySQL only
cargo build --no-default-features --features mysql
# PostgreSQL only
cargo build --no-default-features --features postgres
# SQLite only
cargo build --no-default-features --features sqlite
# MySQL + PostgreSQL (no SQLite)
cargo build --no-default-features --features "mysql,postgres"
MCP工具
query
执行SQL语句。
参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
sql |
string | yes | 要执行的SQL语句 |
explain |
boolean | no | 强制对此查询运行EXPLAIN |
session |
string | no | 要将此查询路由到的已命名会话 |
schema_info
获取表的架构元数据。
参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
table |
string | yes | 表名 |
database |
string | no | 数据库/架构名称 |
include |
字符串数组 | 否 | 其他元数据: "indexes", "foreign_keys", "size" |
session |
string | no | 要使用的命名会话 |
server_info
获取服务器元数据:版本、当前数据库、当前用户和后端特定设置。
参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
session |
string | no | 要使用的命名会话 |
explain_plan
在不运行SELECT查询的情况下获取其执行计划。
参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
sql |
string | yes | 要解释的SELECT语句 |
session |
string | no | 要使用的命名会话 |
connect
创建指向其他数据库服务器的命名会话。需要 DB_ALLOW_RUNTIME_CONNECTIONS=true.
参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
name |
string | yes | 会话标识符 |
host |
string | yes | 服务器主机名 |
user |
string | yes | 用户名 |
port |
integer | 否 | 服务器端口 |
password |
string | 否 | 密码 |
database |
string | 否 | 默认数据库 |
ssl |
boolean | 否 | 启用SSL/TLS |
ssl_ca |
string | no | CA证书文件的路径 |
ssh_host |
string | no | SSH堡垒主机名 |
ssh_port |
integer | 否 | SSH端口 |
ssh_user |
string | no | SSH用户名 |
ssh_private_key |
string | no | SSH私钥路径 |
disconnect
显式关闭命名会话。
list_sessions
列出所有活动的命名会话。
list_tables
列出当前或指定数据库中的所有表。
发展
# Run tests (MySQL via testcontainers, PG/SQLite unit tests)
cargo test
# Run tests against a real MySQL instance
DB_HOST=localhost DB_USER=root DB_DATABASE=mydb cargo test
# Build release binary
cargo build --release
许可证
麻省理工学院