MySQL MCP
Enables integration with GitHub through the MCP framework, allowing direct cloning of repositories as mentioned in the installation instructions.
Provides tools for MySQL database operations including executing SQL queries, managing table structures, listing tables, describing table schemas, and performing CRUD operations (insert, update, delete data) on MySQL databases.
Click on "Deploy Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@MySQL MCP show me the top 10 customers by total purchase amount"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
MySQL MCP 工具
MySQL MCP 是一个基于 MCP(Model-Control-Panel)框架的 MySQL 数据库操作工具,提供了简单易用的 API 来执行 SQL 查询、管理表结构、操作数据等功能。
MCP 框架简介
MCP(Model-Control-Panel)是一个强大的工具框架,允许您将工具函数暴露为API,使模型(如AI助手)能够直接调用这些函数。MySQL MCP 将 MySQL 数据库操作封装为 MCP 工具,便于与 Cursor IDE 等工具集成使用。
Related MCP server: MySQL MCP Server
如何使用 MCP
1. 配置 MCP
在 ~/.cursor/mcp.json 中添加以下配置:
{
"mcpServers": {
"mysql-mcp": {
"command": "/path/to/uv",
"args": [
"--directory",
"/path/to/mysql-mcp",
"run",
"mysql-mcp.py",
"--host", "xxx.xxx.xxx.xxx",
"--port", "3306",
"--user", "root",
"--password", "********",
"--database", "your_database",
"--connection-timeout", "10",
"--connect-retry-count", "3"
]
}
}
}2. 启动 MCP 服务
配置完成后,Cursor IDE 会自动启动 MCP 服务,无需手动操作。如需单独运行:
python mysql-mcp.py3. 在 MCP 环境中调用工具函数
在 Cursor IDE 中,AI 助手可以直接调用 MySQL MCP 工具:
# 查询所有数据库
await mcp_mysql_mcp_execute_query("SHOW DATABASES")
# 列出当前数据库的所有表
await mcp_mysql_mcp_list_tables()
# 执行复杂查询
await mcp_mysql_mcp_execute_query("SELECT * FROM users WHERE age > %s", [18])可用 MCP 工具函数
1. 执行查询:mcp_mysql_mcp_execute_query
# 执行 SELECT 查询
result = await mcp_mysql_mcp_execute_query(query="SELECT * FROM users WHERE age > %s", params=[18])
# 执行 SHOW DATABASES 查询
result = await mcp_mysql_mcp_execute_query(query="SHOW DATABASES")
# 查询结果格式
# {
# "success": true,
# "rows": [{"id": 1, "name": "张三", "age": 25}, ...],
# "row_count": 10
# }2. 列出表:mcp_mysql_mcp_list_tables
# 列出当前数据库的所有表
result = await mcp_mysql_mcp_list_tables()
# 列出特定数据库的所有表
result = await mcp_mysql_mcp_list_tables(database_name="information_schema")
# 返回结果格式
# {
# "success": true,
# "database": "your_database",
# "tables": ["users", "products", "orders"],
# "count": 3
# }3. 获取表结构:mcp_mysql_mcp_describe_table
# 获取表结构
result = await mcp_mysql_mcp_describe_table(table_name="users")
# 返回结果格式
# {
# "success": true,
# "table": "users",
# "columns": [
# {"Field": "id", "Type": "int(11)", "Null": "NO", "Key": "PRI", "Default": null, "Extra": "auto_increment"},
# {"Field": "name", "Type": "varchar(100)", "Null": "NO", "Key": "", "Default": null, "Extra": ""}
# ]
# }4. 切换数据库:mcp_mysql_mcp_use_database
# 切换到另一个数据库
result = await mcp_mysql_mcp_use_database(database_name="another_database")
# 返回结果格式
# {
# "success": true,
# "message": "已切换到数据库 another_database",
# "current_database": "another_database"
# }5. 其他数据操作工具
创建表:
mcp_mysql_mcp_create_table(table_name, columns_def)插入数据:
mcp_mysql_mcp_insert_data(table_name, data)更新数据:
mcp_mysql_mcp_update_data(table_name, data, condition, params)删除数据:
mcp_mysql_mcp_delete_data(table_name, condition, params)
功能特点
执行 SQL 查询语句并获取结果
列出数据库中的所有表
获取表结构信息
创建新表
插入、更新和删除数据
切换数据库
自动处理连接错误和重试
提供详细的错误信息和原因分析
错误处理
所有工具函数都会返回详细的错误信息和原因分析,便于快速定位和解决问题:
# 查询不存在的表
result = await mcp_mysql_mcp_execute_query(query="SELECT * FROM non_existing_table")
# 返回: {"error": "执行查询失败: Table 'your_database.non_existing_table' doesn't exist\n原因:查询的表不存在", "query": "SELECT * FROM non_existing_table"}安装与配置
安装
确保您已安装 Python 3.12 或更高版本
克隆仓库到本地:
git clone https://github.com/Liu-creators/mysql-mcp.git
cd mysql-mcp创建并激活虚拟环境:
python -m venv .venv
source .venv/bin/activate # 在 Windows 上使用 .venv\Scripts\activate安装依赖:
pip install -e .其他配置方式
除了 MCP 配置外,还可以通过以下方式配置:
环境变量
export MYSQL_HOST=localhost
export MYSQL_PORT=3306
export MYSQL_USER=root
export MYSQL_PASSWORD=your_password
export MYSQL_DATABASE=your_database
export MYSQL_CONNECTION_TIMEOUT=10
export MYSQL_CONNECT_RETRY_COUNT=3命令行参数
python mysql-mcp.py --host localhost --port 3306 --user root --password your_password --database your_database扩展与自定义
您可以修改源代码来添加更多功能或调整现有功能的行为。主要的扩展点包括:
在
mysql-mcp.py中添加新的工具函数修改现有函数的错误处理和返回值
调整数据库连接的默认配置
许可证
Available Tools
8 toolscreate_tableB
创建新表
Args:
table_name: 表名
columns_def: 列定义,例如 "id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100), age INT"
db_config: 数据库连接配置参数,如果为None则使用默认配置
Returns:
包含创建结果的字典
Inputs: table_name (required), columns_def (required), db_config.
delete_dataB
从表中删除数据
Args:
table_name: 表名
condition: WHERE条件子句
params: 条件参数列表
db_config: 数据库连接配置参数,如果为None则使用默认配置
Returns:
包含删除结果的字典
Inputs: table_name (required), condition (required), params, db_config.
describe_tableA
获取表结构
Args:
table_name: 表名
db_config: 数据库连接配置参数,如果为None则使用默认配置
Returns:
包含表结构信息的字典
Inputs: table_name (required), db_config.
execute_queryA
执行SQL查询语句,返回查询结果
Args:
query: SQL查询语句
params: 查询参数,用于参数化查询,防止SQL注入
db_config: 数据库连接配置参数,如果为None则使用默认配置
Returns:
包含查询结果的字典
Inputs: query (required), params, db_config.
insert_dataB
向表中插入数据
Args:
table_name: 表名
data: 要插入的数据,字段名和值的字典
db_config: 数据库连接配置参数,如果为None则使用默认配置
Returns:
包含插入结果的字典
Inputs: table_name (required), data (required), db_config.
list_tablesA
列出指定数据库中的所有表
Args:
database_name: 数据库名称,如果为None则使用默认数据库
db_config: 数据库连接配置参数,如果为None则使用默认配置
Returns:
包含表列表的字典
Inputs: database_name, db_config.
update_dataB
更新表中的数据
Args:
table_name: 表名
data: 要更新的数据,字段名和值的字典
condition: WHERE条件子句
params: 条件参数列表
db_config: 数据库连接配置参数,如果为None则使用默认配置
Returns:
包含更新结果的字典
Inputs: table_name (required), data (required), condition (required), params, +1 more.
use_databaseC
切换到指定的数据库
Args:
database_name: 数据库名称
db_config: 数据库连接配置参数,如果为None则使用默认配置
Returns:
包含切换结果的字典
Inputs: database_name (required), db_config.
Tool Schema Changelog
Recent tool additions, removals, and schema changes observed during successful MCP inspections.
Loading tool schema changelog…
TDQS
Scored across 8 tools
Each tool has a unique and clearly defined purpose targeting different database operations (e.g., create table, insert data, execute query). There is no ambiguity or overlap among the tools.
All tool names follow a consistent verb_noun pattern in snake_case (e.g., create_table, list_tables, update_data). This makes the naming predictable and easy to understand.
With 8 tools, the server is well-scoped for basic MySQL database operations. The count is appropriate, covering essential CRUD and metadata operations without being too sparse or bloated.
The tool set covers core operations like table creation, data manipulation, and querying. However, it is missing important DDL operations such as DROP TABLE or ALTER TABLE, which could be considered minor gaps.
Maintenance
Related MCP Connectors
Query Postgres, MySQL, SQL Server, Oracle, BigQuery, ClickHouse and Redshift from your AI client.
Work with your database from AI tools: ask, export, approved changes, and scheduled jobs.
PostgreSQL, MySQL, and SQL Server in one session. 26 read-only MCP tools for AI agents.
Query and update Teable records, manage tables, and access the Teable API with AI agents.
Related MCP Servers
- AlicenseBqualityDmaintenanceEnables AI models to perform MySQL database operations through a standardized interface, supporting secure connections, query execution, and comprehensive schema management.782 npm33MIT
- AlicenseNot gradedqualityDmaintenanceEnables AI assistants to securely connect to and manage MySQL databases with support for multiple database connections, complete CRUD operations, schema inspection, and dynamic connection management through natural language.31 npm81MIT
- AlicenseNot gradedqualityDmaintenanceEnables Cursor AI to interact with MySQL databases through natural language, supporting SQL queries, table schema retrieval, and data operations.17MIT
- AlicenseNot gradedqualityDmaintenanceEnables secure, runtime-authenticated MySQL database querying and mutation through dynamic tools, allowing AI assistants to list databases/tables, describe schemas, and perform CRUD operations.18 npm2MIT