mcp_mysql_server
導入
mcp_mysql_server_pro は、MySQL CRUD 操作だけではなく、データベース異常分析機能も備えており、開発者がカスタム ツールを使用して簡単に拡張できます。
- STDIOモードとSSEモードの両方をサポート
- 「;」で区切られた複数の SQL 実行をサポートします。
- テーブルコメントに基づいてデータベーステーブル名とフィールドのクエリをサポートします
- SQL実行プラン分析をサポート
- 中国語フィールドからピンインへの変換をサポート
- テーブルロック分析をサポート
- 読み取り専用、書き込み、管理者の 3 つのロールによる権限制御をサポートします
"readonly": ["SELECT", "SHOW", "DESCRIBE", "EXPLAIN"], # Read-only permissions
"writer": ["SELECT", "SHOW", "DESCRIBE", "EXPLAIN", "INSERT", "UPDATE", "DELETE"], # Read-write permissions
"admin": ["SELECT", "SHOW", "DESCRIBE", "EXPLAIN", "INSERT", "UPDATE", "DELETE",
"CREATE", "ALTER", "DROP", "TRUNCATE"] # Administrator permissions
使用方法
SSEモード
カーソル、クラインなどの MCP クライアント ツールに次のコンテンツを追加します。
mcp json は次のようになります。
{
"mcpServers": {
"operateMysql": {
"name": "operateMysql",
"description": "",
"isActive": true,
"baseUrl": "http://localhost:9000/sse"
}
}
}
.env ファイルの内容を変更して、データベース接続情報をデータベースの詳細に更新します。
# MySQL Database Configuration
MYSQL_HOST=192.168.xxx.xxx
MYSQL_PORT=3306
MYSQL_USER=root
MYSQL_PASSWORD=root
MYSQL_DATABASE=a_llm
MYSQL_ROLE=readonly # Optional, default is 'readonly'. Available values: readonly, writer, admin
開始コマンド:
# Download dependencies
uv sync
# Start
uv run server.py
STDIOモード
カーソル、クラインなどの MCP クライアント ツールに次のコンテンツを追加します。
mcp json は次のようになります。
{
"mcpServers": {
"operateMysql": {
"isActive": true,
"name": "operateMysql",
"command": "uv",
"args": [
"--directory",
"G:\\python\\mysql_mcp\\src", # Replace this with your project path
"run",
"server.py",
"--stdio"
],
"env": {
"MYSQL_HOST": "192.168.xxx.xxx",
"MYSQL_PORT": "3306",
"MYSQL_USER": "root",
"MYSQL_PASSWORD": "root",
"MYSQL_DATABASE": "a_llm",
"MYSQL_ROLE": "readonly" # Optional, default is 'readonly'. Available values: readonly, writer, admin
}
}
}
}
カスタムツール拡張機能
- ハンドル パッケージに新しいツール クラスを追加し、BaseHandler から継承し、get_tool_description メソッドと run_tool メソッドを実装します。
- init.pyに新しいツールをインポートして、サーバーで使用できるようにします。
例
- 新しいテーブルを作成し、データを挿入します。プロンプトの形式は次のとおりです。
# Task
Create an organizational structure table with the following structure: department name, department number, parent department, is valid.
# Requirements
- Table name: t_admin_rms_zzjg
- Field requirements: string type uses 'varchar(255)', integer type uses 'int', float type uses 'float', date and time type uses 'datetime', boolean type uses 'boolean', text type uses 'text', large text type uses 'longtext', large integer type uses 'bigint', large float type uses 'double'
- Table header needs to include primary key field, serial number XH varchar(255)
- Table must include these fixed fields at the end: creator-CJR varchar(50), creation time-CJSJ datetime, modifier-XGR varchar(50), modification time-XGSJ datetime
- Field naming should use tool return content
- Common fields need indexes
- Each field needs comments, table needs comment
- Generate 5 real data records after creation
- テーブルのコメントに基づいてデータをクエリします。次のようにプロンプトが表示されます。
Query Zhang San's data from the user information table
- 遅い SQL を分析し、次のようにプロンプトを表示します。
select * from t_jcsjzx_hjkq_cd_xsz_sk xsz
left join t_jcsjzx_hjkq_jcd jcd on jcd.cddm = xsz.cddm
Based on current index situation, review execution plan and provide optimization suggestions in markdown format, including table index status, execution details, and optimization recommendations
- SQL デッドロックの問題を分析し、次のようにプロンプトを表示します。
update t_admin_rms_zzjg set sfyx = '0' where xh = '1' is stuck, please analyze the cause