KatCoder MySQL MCP服务器
一个安全且功能丰富的MySQL模型上下文协议(MCP)服务器,使AI代理和应用程序能够通过标准化的接口与MySQL数据库进行交互。
特性
🔒 安全第一
- SQL注入防护:全面的输入验证和净化
- 标识符验证:对表和列名进行严格验证
- 查询白名单:默认情况下为只读操作,写操作需要明确的权限
- 连接池:具有超时控制的安全连接管理
- 错误处理:不暴露敏感信息的安全错误消息
🛠️ 数据库操作
- 列表:浏览表和查看表结构
- 阅读:通过过滤、分页和排序查询数据
- 创建:插入带有验证的新记录
- 添加列:向具有完整类型和约束支持的现有表中添加新列
- 删除列:通过安全检查从表中删除列
- 修改列:更改列定义(类型、约束、默认值)
- 重命名列:重命名现有列,同时保留数据
- 重命名表:使用安全验证重命名表
- 添加索引:创建具有唯一约束的索引(BTREE、HASH、FULLTEXT、SPATIAL)
- 删除索引:从表中删除索引
- 大容量插入:在单个操作中高效插入多条记录
- 更新:安全地修改现有记录
- 删除:删除带有强制WHERE子句的记录
- 执行:运行具有安全限制的自定义SQL查询
- 数据定义语言:执行数据定义语言语句
- 交易:以原子方式执行多个操作
- 效用:数据库运行状况检查和元数据操作
🔧 配置选项
- 连接字符串:标准MySQL连接格式
- 工具选择:仅启用所需的工具
- 连接池:可配置的池设置
- 超时控制:连接和查询超时
工具权限和安全
🎯 推荐方法:使用“所有”工具
对于大多数用例,我们建议启用所有工具 通过使用 "all" 作为工具参数。这提供了:
- 全部功能:访问所有数据库操作,包括DDL、事务和高级功能
- AI 代理兼容性:确保AI代理可以看到并使用所有可用的工具
- 未来考验:添加新工具时自动包含这些工具
- 简化配置:无需手动列出特定工具
# Recommended: Enable all tools
npx katcoder-mysql-mcp "mysql://user:password@localhost:3306/mydb" "all"🔒 安全第一方法:手动工具选择
仅当需要限制访问时才使用手动工具选择 出于安全或合规原因:
只读访问
非常适合报告、分析或只读AI代理:
npx katcoder-mysql-mcp "mysql://readonly:password@localhost:3306/mydb" "list,read,utility"可用工具: list, read, utility
- 列表:浏览表和架构
- 读:使用过滤和分页查询数据
- 效用:数据库健康检查和元数据
基本写入权限
对于需要修改数据但不需要修改架构的应用程序:
npx katcoder-mysql-mcp "mysql://writer:password@localhost:3306/mydb" "list,read,create,update,delete,utility"可用工具: list, read, create, update, delete, utility
- 包括所有只读工具以及:
- 创建:插入新记录
- 更新:修改现有记录
- 删除:删除记录(带强制WHERE子句)
完全数据库访问
对于数据库管理员和开发环境:
npx katcoder-mysql-mcp "mysql://admin:password@localhost:3306/mydb" "all"所有可用工具: list, read, create, update, delete, execute, ddl, transaction, bulk_insert, utility, add_column, drop_column, modify_column, rename_column, rename_table, add_index, drop_index, show_table_data
🛡️ 安全考虑
数据库用户权限
始终使用具有适当权限的MySQL用户帐户:
-- Read-only user
CREATE USER 'readonly'@'%' IDENTIFIED BY 'secure_password';
GRANT SELECT ON mydb.* TO 'readonly'@'%';
-- Write user (no DDL)
CREATE USER 'writer'@'%' IDENTIFIED BY 'secure_password';
GRANT SELECT, INSERT, UPDATE, DELETE ON mydb.* TO 'writer'@'%';
-- Admin user (full access)
CREATE USER 'admin'@'%' IDENTIFIED BY 'secure_password';
GRANT ALL PRIVILEGES ON mydb.* TO 'admin'@'%';
FLUSH PRIVILEGES;工具级与数据库级安全
- 工具级别限制 限制MCP服务器可以执行的操作
- 数据库级权限 提供最终的安全边界
- 最佳实践:使用两层进行纵深防御
生产建议
- 使用特定的数据库用户 具有最低限度的所需权限
- 仅启用必要的工具 适用于生产环境
- 使用只读连接 用于报告和分析
- 监控数据库访问 审计工具的使用
- 使用环境变量 用于连接字符串(永远不要硬编码密码)
📊 工具选择快速参考
| 用例 | 推荐工具 | 安全级别 |
|---|---|---|
| 人工智能开发 | "all" | 中等(使用开发数据库) |
| 生产AI | "all" | 高(受限数据库用户) |
| 报告/分析 | "list,read,utility" | 高 |
| 数据输入应用程序 | "list,read,create,update,delete,utility" | 中等 |
| 数据库管理员 | "all" | 低(可信环境) |
| CI/CD管道 | "all" | 介质(孤立环境) |
安装
备注:此包目前正在开发中,尚未发布到npm。使用下面的开发安装方法。
开发安装(推荐)
git clone https://github.com/katkoder/katcoder-mysql-mcp.git
cd katcoder-mysql-mcp
npm install
npm run build未来npm安装(即将推出)
一旦发布到npm,您将能够全局安装:
# This will be available after publication
npm install -g katcoder-mysql-mcp本地npm安装(即将推出)
# This will be available after publication
npm install katcoder-mysql-mcp用法
命令行接口
当前开发用途
# After building the project (npm run build)
# Basic usage with all tools enabled
node dist/cli.js "mysql://user:password@localhost:3306/database_name"
# With all tools enabled (recommended)
node dist/cli.js "mysql://user:password@localhost:3306/database_name" "all"
# With specific tools enabled (if you need to limit access)
node dist/cli.js "mysql://user:password@localhost:3306/database_name" "list,read,utility"
# With verbose logging
node dist/cli.js "mysql://user:password@localhost:3306/database_name" "all" --verbose未来npm的使用(发布后)
# Basic usage with all tools enabled
npx katcoder-mysql-mcp "mysql://user:password@localhost:3306/database_name"
# With all tools enabled (recommended)
npx katcoder-mysql-mcp "mysql://user:password@localhost:3306/database_name" "all"
# With specific tools enabled (if you need to limit access)
npx katcoder-mysql-mcp "mysql://user:password@localhost:3306/database_name" "list,read,utility"
# With verbose logging
npx katcoder-mysql-mcp "mysql://user:password@localhost:3306/database_name" "all" --verboseAI代理的配置
当前开发配置
Claude桌面配置: 将此配置添加到您的Claude Desktop配置文件中:
{
"mcpServers": {
"katkoder_mysql": {
"command": "node",
"args": [
"/path/to/katcoder-mysql-mcp/dist/cli.js",
"mysql://root:password@localhost:3306/production_db",
"all"
],
"cwd": "/path/to/katcoder-mysql-mcp"
}
}
}光标IDE配置: 对于Cursor IDE,添加到您的设置中:
{
"mcp.servers": {
"katkoder_mysql": {
"command": "node",
"args": [
"/path/to/katcoder-mysql-mcp/dist/cli.js",
"mysql://user:password@localhost:3306/development_db",
"all"
],
"cwd": "/path/to/katcoder-mysql-mcp"
}
}
}未来npm配置(发布后)
Claude桌面配置:
{
"mcpServers": {
"katkoder_mysql": {
"command": "npx",
"args": [
"-y",
"katcoder-mysql-mcp",
"mysql://root:password@localhost:3306/production_db",
"all"
]
}
}
}光标IDE配置:
{
"mcp.servers": {
"katkoder_mysql": {
"command": "npx",
"args": [
"-y",
"katcoder-mysql-mcp",
"mysql://user:password@localhost:3306/development_db",
"all"
]
}
}
}连接字符串格式
mysql://[user[:password]@]host[:port]/database基本示例:
mysql://root@localhost:3306/mydb-无密码的本地数据库mysql://user:password@localhost:3306/mydb-带密码的本地数据库mysql://user:password@192.168.1.100:3306/mydb-远程数据库
高级示例:
mysql://user:password@db.example.com:3306/production?ssl=true-带SSL的远程数据库mysql://root:password@mysql-container:3306/docker_db-Docker数据库mysql://user:password@localhost:3307/alternative_port-不同的端口
可用工具
1.列表工具
浏览数据库结构和表信息。
参数:
table(可选):获取列信息的特定表名
示例:
{
"name": "list",
"arguments": {}
}
{
"name": "list",
"arguments": {
"table": "users"
}
}实际使用场景:
- 数据库发现:连接到新数据库时,使用不带参数的列表工具查看所有可用表
- 模式探索:在编写查询之前,使用表名来理解结构
- 数据建模:通过检查外键约束来检查表之间的关系
- 迁移规划:在进行更改之前了解现有架构
2.读取工具
通过过滤和分页从表中查询数据。
参数:
table(必填):要查询的表名columns(可选):要选择的特定列数组where(可选):具有过滤条件的对象limit(可选):最大行数(最大值:10000)offset(可选):要跳过的行数orderBy(可选):按条款排序
基本示例:
{
"name": "read",
"arguments": {
"table": "users",
"columns": ["id", "name", "email"],
"where": {"status": "active"},
"limit": 10,
"orderBy": "created_at DESC"
}
}
{
"name": "read",
"arguments": {
"table": "products",
"where": {"category": "electronics", "price": {"$gt": 100}},
"limit": 50
}
}高级筛选示例:
{
"name": "read",
"arguments": {
"table": "users",
"columns": ["id", "email", "created_at"],
"where": {"status": "active", "created_at": {"$gte": "2024-01-01"}},
"limit": 25,
"offset": 50,
"orderBy": "last_login DESC"
}
}3.批量插入工具
在一次操作中高效地将多条记录插入到表中。
参数:
table(必填):目标表名data(必填):具有相同列值对的对象数组
示例:
{
"name": "bulk_insert",
"arguments": {
"table": "users",
"data": [
{
"name": "John Doe",
"email": "john@example.com",
"age": 30,
"status": "active"
},
{
"name": "Jane Smith",
"email": "jane@example.com",
"age": 25,
"status": "active"
},
{
"name": "Bob Wilson",
"email": "bob@example.com",
"age": 35,
"status": "inactive"
}
]
}
}交易中的使用:
{
"name": "transaction",
"arguments": {
"operations": [
{
"type": "bulk_insert",
"table": "users",
"data": [
{
"name": "Alice Brown",
"email": "alice@example.com",
"age": 28,
"status": "active"
}
]
},
{
"type": "update",
"table": "user_stats",
"data": { "total_users": 1 },
"where": { "id": 1 }
}
]
}
}响应格式:
{
"success": true,
"table": "users",
"recordCount": 3,
"affectedRows": 3,
"insertedId": 1,
"message": "Successfully inserted 3 records into users"
}4.创建工具
将新记录插入表中。
参数:
table(必填):目标表名data(必填):具有列值对的对象
示例:
{
"name": "create",
"arguments": {
"table": "users",
"data": {
"name": "John Doe",
"email": "john@example.com",
"status": "active"
}
}
}4.更新工具
安全地修改现有记录。
参数:
table(必填):目标表名data(必需):要更新的具有列值对的对象where(必填):具有过滤条件的对象
示例:
{
"name": "update",
"arguments": {
"table": "users",
"data": {
"status": "inactive",
"updated_at": "2024-01-01 12:00:00"
},
"where": {"id": 123}
}
}5.删除工具
删除带有强制WHERE子句的记录。
参数:
table(必填):目标表名where(必填):具有过滤条件的对象
示例:
{
"name": "delete",
"arguments": {
"table": "sessions",
"where": {"expired": true}
}
}6.执行工具
运行具有安全限制的自定义SQL查询。
参数:
query(必填):SQL查询字符串params(可选):查询参数数组allowWrite(可选):允许写操作的布尔值
基本示例:
{
"name": "execute",
"arguments": {
"query": "SELECT COUNT(*) as total FROM users WHERE created_at > ?",
"params": ["2024-01-01"]
}
}
{
"name": "execute",
"arguments": {
"query": "UPDATE users SET last_login = NOW() WHERE id = ?",
"params": [123],
"allowWrite": true
}
}复杂查询示例:
{
"name": "execute",
"arguments": {
"query": "SELECT u.email, COUNT(o.id) as order_count FROM users u LEFT JOIN orders o ON u.id = o.user_id GROUP BY u.id HAVING order_count > 5"
}
}
{
"name": "execute",
"arguments": {
"query": "SELECT DATE(created_at) as date, COUNT(*) as daily_signups FROM users WHERE created_at >= DATE_SUB(NOW(), INTERVAL 30 DAY) GROUP BY DATE(created_at) ORDER BY date",
"params": []
}
}7.DDL工具
执行数据定义语言语句。
参数:
statement(必填):DDL语句
示例:
{
"name": "ddl",
"arguments": {
"statement": "CREATE INDEX idx_email ON users(email)"
}
}8.添加列工具
使用全面的类型和约束支持向现有表添加新列。
参数:
table(必填):目标表名column(必填):具有列定义的对象
- name (必填):新列名 - type (必填):列数据类型(例如VARCHAR(255)、INT、DATETIME) - nullable (可选):列是否可以包含NULL值 - default (可选):列的默认值 - autoIncrement (可选):列是否应自动递增 - comment (可选):列注释
position(可选):指定列位置的对象
- after (可选):将列放置在此现有列之后 - first (可选):将列放置为第一列
示例:
{
"name": "add_column",
"arguments": {
"table": "users",
"column": {
"name": "email",
"type": "VARCHAR(255)",
"nullable": false,
"default": "no-email@example.com"
},
"position": {
"after": "name"
}
}
}{
"name": "add_column",
"arguments": {
"table": "products",
"column": {
"name": "is_active",
"type": "BOOLEAN",
"default": true,
"comment": "Product availability status"
}
}
}9.落柱工具
使用安全验证从表中删除列。
参数:
table(必需):要从中删除列的表名column(必填):要删除的列名
示例:
{
"name": "drop_column",
"arguments": {
"table": "users",
"column": "old_field"
}
}10.修改列工具
更改现有列定义,包括类型、约束和默认值。
参数:
table(必填):包含该列的表名column(必填):要修改的列名newDefinition(必填):具有新列定义的对象
- type (必填):新建列数据类型 - nullable (可选):列是否可以包含NULL值 - default (可选):新默认值 - comment (可选):列注释
示例:
{
"name": "modify_column",
"arguments": {
"table": "users",
"column": "age",
"newDefinition": {
"type": "INT",
"nullable": true,
"default": null
}
}
}11.重命名列工具
在保留数据的同时重命名现有列。
参数:
table(必填):包含该列的表名oldName(必填):当前列名newName(必填):新列名newDefinition(可选):重命名列的列定义
示例:
{
"name": "rename_column",
"arguments": {
"table": "users",
"oldName": "user_name",
"newName": "username"
}
}12.重命名表格工具
使用安全验证重命名表。
参数:
oldName(必填):当前表名newName(必填):新表名
示例:
{
"name": "rename_table",
"arguments": {
"oldName": "user_profiles",
"newName": "user_settings"
}
}13.添加索引工具
在表上创建索引以提高查询性能。
参数:
table(必填):要添加索引的表名name(必填):索引名称columns(必填):要包含在索引中的列名数组type(可选):索引类型(BTREE、HASH、FULLTEXT、SPATIAL)unique(可选):索引是否应唯一
示例:
{
"name": "add_index",
"arguments": {
"table": "users",
"name": "idx_email",
"columns": ["email"],
"unique": true
}
}{
"name": "add_index",
"arguments": {
"table": "products",
"name": "idx_category_price",
"columns": ["category_id", "price"],
"type": "BTREE"
}
}14.下降指数工具
从表中删除索引。
参数:
table(必填):包含索引的表名name(必填):要删除的索引名称
示例:
{
"name": "drop_index",
"arguments": {
"table": "users",
"name": "idx_temp"
}
}15.交易工具
以原子方式执行多个操作。
参数:
operations(必填):事务中要执行的操作数组
基本示例:
{
"name": "transaction",
"arguments": [
{
"type": "create",
"table": "orders",
"data": {"user_id": 123, "total": 99.99}
},
{
"type": "update",
"table": "users",
"data": {"last_order_date": "2024-01-01"},
"where": {"id": 123}
}
]
}具有模式更改的高级事务示例:
{
"name": "transaction",
"arguments": {
"operations": [
{
"type": "add_column",
"table": "users",
"column": {
"name": "phone",
"type": "VARCHAR(20)",
"nullable": true
}
},
{
"type": "add_index",
"table": "users",
"name": "idx_phone",
"columns": ["phone"],
"unique": true
},
{
"type": "update",
"table": "users",
"data": {"phone": "+1234567890"},
"where": {"id": 1}
}
]
}
}响应格式:
{
"success": true,
"operations": 3,
"results": [
{
"description": "Add column 'phone' to table 'users'",
### v1.1.0 (Latest)
- **New Feature**: Added Comprehensive Schema Modification Tools
- Implemented `add_column` tool for adding new columns with full type and constraint support
- Implemented `drop_column` tool for safely removing columns from tables
- Implemented `modify_column` tool for changing column definitions
- Implemented `rename_column` tool for renaming existing columns
- Implemented `rename_table` tool for renaming tables
- Implemented `add_index` tool for creating various types of indexes
- Implemented `drop_index` tool for removing indexes from tables
- Added comprehensive schema validation and security measures
- Enhanced transaction support for schema operations with rollback mechanisms
- Added detailed documentation with examples and usage scenarios
### v1.0.1
- **New Feature**: Added Bulk Insert Tool for efficient multi-record insertion
- Implemented `bulk_insert` tool for batch data imports
- Supports inserting multiple records in a single database operation
- Includes comprehensive validation and error handling
- Can be used within transactions for atomic operations
- Added detailed documentation with examples and usage scenarios
### v1.0.0
- Initial release
- All database operations implemented
- Comprehensive security features
- Full documentation
"affectedRows": 0
},
{
"description": "Create unique index 'idx_phone' on table 'users'",
"affectedRows": 0
},
{
"description": "Update user record with phone number",
"affectedRows": 1
}
]
}{ “name”:“ddl”, “论点”:{ “语句”:“ALTER TABLE用户添加COLUMN电话VARCHAR(20)” } }
### 8. Transaction Tool
Execute multiple operations atomically.
**Parameters:**
- `operations` (required): Array of operations to execute
**Basic Examples:**{ "name": "transaction", "arguments": { "operations": [ { "type": "create", "table": "orders", "data": {"user_id": 123, "total": 99.99} }, { "type": "update", "table": "users", "data": {"last_order_date": "2024-01-01"}, "where": {"id": 123} } ] } }
**高级交易示例:**
{ "name": "transaction", "arguments": { "operations": [ { "type": "create", "table": "orders", "data": {"user_id": 123, "total": 99.99, "status": "pending"} }, { "type": "update", "table": "users", "data": {"last_order_date": "2024-01-01"}, "where": {"id": 123} }, { "type": "create", "table": "order_items", "data": {"order_id": "LAST_INSERT_ID()", "product_id": 456, "quantity": 2} } ] } }
### 9.实用工具
数据库健康检查和元数据操作。
**参数:**
- `action` (必填):实用程序操作(ping、version、stats、describe_table)
- `table` (可选):表名(describe_Table需要)
**示例:**
{ "name": "utility", "arguments": { "action": "ping" } }
{ "name": "utility", "arguments": { "action": "stats" } }
{ "name": "utility", "arguments": { "action": "describe_table", "table": "users" } }
### 10.显示表格数据工具
显示具有高级格式、分页和架构信息的表数据。
**参数:**
- `table` (必填):用于显示数据的表名
- `limit` (可选):要显示的最大行数(默认值:50,最大值:1000)
- `offset` (可选):分页时要跳过的行数(默认值:0)
- `columns` (可选):要显示的特定列的数组(默认值:所有列)
- `where` (可选):具有过滤条件的对象(格式与读取工具相同)
- `orderBy` (可选):要排序的列名(默认为主键或第一列)
- `orderDirection` (可选):排序方向-“ASC”或“DESC”(默认:“ASC”)
- `showSchema` (可选):包括表架构信息(默认值:true)
- `format` (可选):输出格式-“表”、“json”或“csv”(默认:“表”)
**基本示例:**
{ "name": "show_table_data", "arguments": { "table": "users" } }
{ "name": "show_table_data", "arguments": { "table": "products", "limit": 25, "columns": ["id", "name", "price", "category"], "orderBy": "price", "orderDirection": "DESC" } }
**高级示例:**
{ "name": "show_table_data", "arguments": { "table": "orders", "where": {"status": "pending", "created_at": {"$gte": "2024-01-01"}}, "limit": 100, "offset": 50, "format": "csv", "showSchema": false } }
{ "name": "show_table_data", "arguments": { "table": "users", "columns": ["id", "email", "last_login"], "where": {"status": "active"}, "orderBy": "last_login", "orderDirection": "DESC", "format": "json" } }
**响应格式:**
{ "success": true, "table": "users", "format": "table", "pagination": { "currentPage": 1, "totalPages": 5, "limit": 50, "offset": 0, "totalRows": 247, "hasMore": true, "showing": "1-50 of 247" }, "data": [...], "displayInfo": "formatted table string (for table format)", "count": 50, "schema": { "columns": [...], "totalColumns": 8 } }
**实际使用场景:**
- **数据探索**:通过自动格式化快速浏览表格内容
- **数据导出**:以CSV格式导出表数据以供外部分析
- **调试**:通过筛选和分页查看特定行
- **模式分析**:检查数据旁边的表结构
- **报告生成**:为文档生成格式化的数据显示
## 安全特性
### SQL注入防护
- **输入消毒**:所有表名和列名都经过净化
- **参数绑定**:所有查询都使用参数化语句
- **查询验证**:危险的SQL模式被阻止
- **写入操作保护**:写入操作需要明确的权限
### 标识符验证
- **表名称**:只允许使用字母数字字符和下划线
- **列名**:根据SQL注入模式进行验证
- **何处条件**:检查值中是否含有危险成分
### 连接安全性
- **连接池**:安全连接管理
- **超时控制**:防止悬挂连接
- **错误处理**:没有敏感数据的安全错误消息
## 安全最佳实践
### 1.推荐设置:所有具有受限数据库用户的工具
**生产和人工智能代理的最佳实践:**
-- Create user with appropriate database-level permissions CREATE USER 'mcp_ai_agent'@'localhost' IDENTIFIED BY 'secure_password'; GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER, INDEX, DROP ON myapp.* TO 'mcp_ai_agent'@'localhost'; FLUSH PRIVILEGES;
Enable all tools - database permissions provide the security boundary
npx katcoder-mysql-mcp "mysql://mcp_ai_agent:secure_password@localhost:3306/myapp" "all"
**为什么这种方法有效:**
- ✅ AI代理可以看到并使用所有可用的工具
- ✅ 数据库用户权限控制实际访问
- ✅ 新工具自动可用,经得起未来考验
- ✅ 简化配置管理
### 2.安全第一场景
#### 只读分析/报告
CREATE USER 'mcp_readonly'@'localhost' IDENTIFIED BY 'secure_password'; GRANT SELECT ON myapp.* TO 'mcp_readonly'@'localhost'; FLUSH PRIVILEGES;
Restrict tools to read-only operations
npx katcoder-mysql-mcp "mysql://mcp_readonly:secure_password@localhost:3306/myapp" "list,read,utility"
#### 数据输入应用程序(无架构更改)
CREATE USER 'mcp_writer'@'localhost' IDENTIFIED BY 'secure_password'; GRANT SELECT, INSERT, UPDATE, DELETE ON myapp.* TO 'mcp_writer'@'localhost'; FLUSH PRIVILEGES;
Allow data operations but restrict DDL tools
npx katcoder-mysql-mcp "mysql://mcp_writer:secure_password@localhost:3306/myapp" "list,read,create,update,delete,bulk_insert,utility"
### 3.开发环境
Development: Use all tools with admin user
npx katcoder-mysql-mcp "mysql://root:password@localhost:3306/dev_db" "all"
### 4.环境变量(推荐)
Set connection string as environment variable
export MYSQL_URL="mysql://mcp_ai_agent:secure_password@localhost:3306/myapp"
Use with all tools enabled
npx katcoder-mysql-mcp "$MYSQL_URL" "all"
Or with specific tools for restricted access
npx katcoder-mysql-mcp "$MYSQL_URL" "list,read,utility"
### 5.Docker/容器环境
Using Docker secrets or environment variables
export MYSQL_URL="mysql://mcp_user:${DB_PASSWORD}@mysql-container:3306/production_db" npx katcoder-mysql-mcp "$MYSQL_URL" "all"
## 快速参考:选择正确的权限方法
|用例|推荐工具|数据库权限|安全级别|
|----------|------------------|---------------------|----------------|
| **人工智能开发与原型制作** | `"all"` |完全管理员访问权限|低(仅限开发人员)|
| **生产人工智能代理** | `"all"` |仅限于特定的数据库/架构|高⭐ |
| **只读分析** | `"list,read,utility"` |仅选择|高|
| **数据输入应用程序** | `"list,read,create,update,delete,bulk_insert,utility"` |无DDL权限|中等|
| **架构迁移工具** | `"all"` |需要DDL权限|中等|
| **报告仪表板** | `"list,read,utility"` |仅选择|高|
### 🎯 **最常见的设置(推荐)**
1. Create restricted database user
CREATE USER 'mcp_agent'@'localhost' IDENTIFIED BY 'secure_password'; GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER, INDEX, DROP ON your_app.* TO 'mcp_agent'@'localhost';
2. Use all tools - security handled by database permissions
npx katcoder-mysql-mcp "mysql://mcp_agent:secure_password@localhost:3306/your_app" "all"
**为什么这样做:** 数据库权限提供了真正的安全边界,而“所有”工具确保AI代理可以查看和使用所有可用功能。
## 错误处理
服务器在维护安全的同时提供详细的错误消息:
{ "error": true, "message": "Table 'nonexistent_table' does not exist", "details": "Check the table name and try again" }
## 发展
### 建设项目
npm run build
### 以开发模式运行
npm run dev
### 测试
npm test
## 环境变量
- `LOG_LEVEL`:设置日志记录级别(调试、信息、警告、错误)
- `NODE_ENV`:设定环境(开发、生产)
## 故障排除
### 连接问题
- 验证MySQL服务器是否正在运行
- 检查连接字符串格式
- 确保数据库存在
- 验证用户权限
#### 测试连接
Test with all tools enabled
npx katcoder-mysql-mcp "mysql://user:password@localhost:3306/mydb" "all"
Then use: {"name": "utility", "arguments": {"action": "ping"}}
#### 检查数据库版本
npx katcoder-mysql-mcp "mysql://user:password@localhost:3306/mydb" "all"
Then use: {"name": "utility", "arguments": {"action": "version"}}
### 权限错误
- 检查MySQL用户权限
- 确保授予数据库访问权限
- 验证表级权限
### 性能问题
- 监控连接池使用情况
- 检查查询执行时间
- 优化数据库索引
#### 监测性能
npx katcoder-mysql-mcp "mysql://user:password@localhost:3306/mydb" "all"
Then use: {"name": "utility", "arguments": {"action": "stats"}}
## 高级配置
### 自定义连接池设置
Environment variables for connection tuning
export MYSQL_CONNECTION_LIMIT=20 export MYSQL_ACQUIRE_TIMEOUT=30000 export MYSQL_TIMEOUT=45000
npx katcoder-mysql-mcp "mysql://user:password@localhost:3306/mydb"
### 日志记录配置
Enable debug logging
export LOG_LEVEL=debug
Enable verbose output
npx katcoder-mysql-mcp "mysql://user:password@localhost:3306/mydb" "all" --verbose
## 贡献
1. 分叉存储库
1. 创建要素分支
1. 进行更改
1. 添加测试
1. 提交拉取请求
## 许可证
MIT许可证-有关详细信息,请参阅许可证文件。
## 支持
对于问题和疑问:
- GitHub问题:https://github.com/katkoder/katcoder-mysql-mcp/issues
- 文档:https://github.com/katkoder/katcoder-mysql-mcp/wiki
## 更新日志
### v1.0.1(最新)
- **新功能**:添加了批量插入工具,可高效插入多条记录
- 实现 `bulk_insert` 批量数据导入工具
- 支持在单个数据库操作中插入多条记录
- 包括全面的验证和错误处理
- 可以在事务中用于原子操作
- 添加了包含示例和使用场景的详细文档
### v1.0.0
- 初始版本
- 已执行所有数据库操作
- 全面的安全功能
- 全部文件