MySQL MCP Server
A Model Context Protocol service that enables AI assistants to directly query and manage MySQL databases through natural language. It supports SQL execution, schema inspection, and atomic transactions for comprehensive database interaction.
README
MySQL MCP Server
一个基于 Model Context Protocol (MCP) 的 MySQL 数据库操作服务,允许 AI 助手直接查询和操作 MySQL 数据库。
功能特性
- query - 执行 SELECT 查询语句
- execute - 执行 INSERT/UPDATE/DELETE 等数据修改语句
- execute_many - 批量执行 SQL 语句(批量插入等)
- get_tables - 获取数据库中所有表的信息
- get_table_structure - 获取指定表的结构信息
- get_databases - 获取所有数据库列表
- test_connection - 测试数据库连接
- transaction - 执行事务(多条 SQL 语句,原子性操作)
快速开始
1. 安装依赖
# 创建虚拟环境
python3 -m venv venv
# 激活虚拟环境
source venv/bin/activate # Linux/Mac
# 或
venv\Scripts\activate # Windows
# 安装依赖
pip install -r requirements.txt
2. 配置数据库连接
# 复制环境变量模板
cp .env.example .env
# 编辑 .env 文件,填入你的数据库连接信息
DB_HOST=localhost
DB_PORT=3306
DB_USER=root
DB_PASSWORD=your_password
DB_NAME=your_database
3. 启动服务器
# 使用启动脚本
./run.sh # Linux/Mac
# 或
run.bat # Windows
# 或直接启动
python server.py
在 Claude Desktop 中使用
方法 1: 直接配置
编辑 Claude Desktop 的配置文件:
- macOS:
~/Library/Application Support/Claude/claude_desktop_config.json - Windows:
%APPDATA%/Claude/claude_desktop_config.json
添加以下配置:
{
"mcpServers": {
"mysql": {
"command": "python",
"args": ["mcp_mysql_server/server.py"],
"env": {
"DB_HOST": "localhost",
"DB_PORT": "3306",
"DB_USER": "root",
"DB_PASSWORD": "your_password",
"DB_NAME": "your_database"
}
}
}
}
方法 2: 使用虚拟环境
{
"mcpServers": {
"mysql": {
"command": "python",
"args": ["mcp_mysql_server/server.py"]
}
}
}
使用示例
查询数据
请帮我查询 users 表中所有的用户
AI 会自动调用 query 工具:
SELECT * FROM users
插入数据
请帮我在 users 表中插入一条新记录,name=张三, email=zhangsan@example.com
AI 会自动调用 execute 工具:
INSERT INTO users (name, email) VALUES ('张三', 'zhangsan@example.com')
查看表结构
请帮我查看 users 表的结构
AI 会自动调用 get_table_structure 工具。
执行事务
请帮我执行一个事务:先插入订单,再更新库存
AI 会自动调用 transaction 工具执行多条 SQL。
安全注意事项
- 不要在代码中硬编码数据库密码,使用环境变量
- 限制数据库用户权限,只授予必要的权限
- 不要在生产环境直接使用 root 账户
- 定期更换数据库密码
项目结构
mcp_mysql_server/
├── server.py # 主服务器文件
├── requirements.txt # Python 依赖
├── .env.example # 环境变量模板
├── run.sh # Linux/Mac 启动脚本
├── run.bat # Windows 启动脚本
├── mcp.json # MCP 配置示例
└── README.md # 说明文档
依赖项
mcp- Model Context Protocol SDKpymysql- MySQL 数据库驱动cryptography- 用于加密连接python-dotenv- 环境变量管理
许可证
MIT License
Recommended Servers
playwright-mcp
A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.
Magic Component Platform (MCP)
An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.
Audiense Insights MCP Server
Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.
VeyraX MCP
Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.
graphlit-mcp-server
The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.
Kagi MCP Server
An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.
E2B
Using MCP to run code via e2b.
Neon Database
MCP server for interacting with Neon Management API and databases
Exa Search
A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.
Qdrant Server
This repository is an example of how to create a MCP server for Qdrant, a vector search engine.