mysql_mcp_server

by wenb1n-dev
Verified

hybrid server

The server is able to function both locally and remotely, depending on the configuration or use case.

Integrations

  • Enables configuration of MySQL database connection settings (host, port, user, password, database) through environment variables stored in .env files.

  • Allows executing SQL queries against MySQL databases, supporting multiple SQL statements, table name/field querying based on comments, SQL execution plan analysis, and Chinese to pinyin field conversion.

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モード

  • uvを使用してサービスを開始します

カーソル、クラインなどの 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 } } } }

カスタムツール拡張機能

  1. ハンドル パッケージに新しいツール クラスを追加し、BaseHandler から継承し、get_tool_description メソッドと run_tool メソッドを実装します。
  2. init.pyに新しいツールをインポートして、サーバーで使用できるようにします。

  1. 新しいテーブルを作成し、データを挿入します。プロンプトの形式は次のとおりです。
# 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
  1. テーブルのコメントに基づいてデータをクエリします。次のようにプロンプトが表示されます。
Query Zhang San's data from the user information table
  1. 遅い 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
  1. SQL デッドロックの問題を分析し、次のようにプロンプトを表示します。
update t_admin_rms_zzjg set sfyx = '0' where xh = '1' is stuck, please analyze the cause

You must be authenticated.

A
security – no known vulnerabilities
F
license - not found
A
quality - confirmed to work

STDIOモードとSSEモードのサポートを追加 「;」で区切られた複数のSQL実行のサポートを追加 テーブルコメントに基づいてデータベーステーブル名とフィールドをクエリする機能を追加 SQL実行プラン分析を追加 ピンイン変換に中国語フィールドを追加

  1. Introduction
    1. Usage Instructions
      1. SSE Mode
      2. STDIO Mode
    2. Custom Tool Extensions
      1. Examples
        ID: 3w3wd880vr