【LLM】使用MySQL MCP Server让大模型轻松操作本地数据库

        随着MCP协议(Model Context Protocol)的出现,使得 LLM 应用与外部数据源和工具之间的无缝集成成为可能,本章就介绍如何通过MCP Server让LLM能够直接与本地的MySQL数据库进行交互,例如新增、修改、删除数据,甚至是创建新的表格等操作。

        什么是MySQL MCP Server?

        简单来说,MySQL MCP Server是一个基于MCP协议开发的服务端程序,它的主要作用是作为大模型与MySQL数据库之间的桥梁。通过MySQL MCP Server,大模型可以轻松地对本地的MySQL数据库进行一系列的操作,包括但不限于:

  • 新增数据:无论是单条记录还是批量导入,都可以通过简单的指令完成。
  • 修改数据:根据特定条件更新数据库中的信息变得轻而易举。
  • 删除数据:精准地移除不再需要的数据,保持数据库的整洁有序。
  • 查询数据:支持复杂的查询语句,帮助用户快速获取所需的信息。
  • 建表操作:当需要在数据库中添加新的表格时,也能通过MySQL MCP Server实现。
  • 分析数据库健康状态:分析mysql的连接情况、事务情况、运行情况、锁情况
  • 分析锁:查询当前mysql服务器是否存在行级锁、表级锁情况
  • 分析索引状态:包含冗余索引情况、性能较差的索引情况、未使用索引

无论你是想要利用大模型来简化数据分析流程,还是希望将机器学习的结果自动存储到数据库中,MySQL MCP Server都能为你提供强有力的支持。

        如何开始使用MySQL MCP Server?

        为了让更多的开发者能够体验到MySQL MCP Server带来的便利,项目已经在GitHub、Gitee上开源。你可以访问项目的主页,查看详细的文档说明,了解如何安装配置以及编写调用MySQL MCP Server的代码示例。目前支持 STDIO  与 SSE 两种部署方式

        1. 下载 mysql mcp server 

        github:https://github.com/wenb1n-dev/mysql_mcp_server_pro.git

        gitee:https://gitee.com/xwb602625136/mysql_mcp_server_pro.git

        ps:觉得不错的话,帮忙在github点个小星星,感谢支持啦~~~        

        2.配置 mcp server ,支持 STDIO  与 SSE

        可以是用支持mcp协议的客户端,例如cursor、cline、cherry studio等

{
  "mcpServers": {
      "operateMysql": {
        "isActive": true,
        "name": "operateMysql",
        "command": "uv",
        "args": [
          "--directory",
          "G:\\python\\mysql_mcp\\src",  # 这里需要替换为你的项目路径
          "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": "admin"
       }
    }
  }
}    

        3.启动mcp,进行问答

4. 效果展示

     4.1 建表、以及插入数据效果展示

        4.1.1 可以看到大模型成功调用了我们mcp server的两个工具,get_chinese_initials、execute_sql

    4.1.2 数据库情况,可以看到已经建表成功且插入了5条数据

4.2 根据表注释查询数据库表名、字段名进行查询数据

  4.2.1 可以看到大模型同样调用了两个工具,一个拿到了表名,一个拿到了字段名

4.3 锁分析

4.4 mysql健康分析

 

4.5 调用预设prompt模版进行问答

        4.5.1 mysql 分析prompt 调用示例

  • step1: 选择 analyzing-mysql-prompt

  • step2: 自动生成对应prompt

  • step3: 开始问答

        

        4.5.2 表数据查询 prompt 调用示例

  • step1: 选择 query-table-data-prompt

  • step2: 输入问题描述(可选),不输入则会初始化为mysql数据查询助手

### MCP MySQL Server Configuration and Management #### Overview of MCP MySQL Server Setup For configuring an MCP (Multi-Cloud Platform) environment with a MySQL server, the setup involves ensuring that all necessary configurations are correctly applied to integrate MySQL as part of the data storage solution. In scenarios where applications like Nacos need to connect to a MySQL database, it is crucial to ensure proper initialization and configuration. #### Initialization of MySQL Database for Application Integration When initializing a MySQL database specifically for application integration such as switching from default settings to using MySQL, one can modify `application.properties` or configure during Docker startup[^1]. This ensures that the application connects to the correct MySQL instance configured within the MCP environment. #### Schema Creation for Application Data Persistence To avoid issues related to no datasource exceptions when deploying services on platforms like Nacos via Docker while aiming at persisting data into MySQL, creating databases and tables beforehand is essential. The SQL script located at `D:\software\nacos\nacos-server-2.3.1\conf\mysql-schema.sql` serves this purpose by providing commands needed for setting up schemas required by Nacos[^2]. #### Troubleshooting Common Issues During Deployment In cases similar to those described in experiences involving deployment challenges—such as encountering errors due to missing database creation steps—it becomes evident how critical these preparatory actions are before attempting service deployments. Ensuring that both the database exists along with its schema helps prevent common pitfalls associated with connectivity problems between containers running inside Docker environments and external databases[^3]. ```sql -- Example command to create a new database named 'mcp_db' CREATE DATABASE mcp_db; USE mcp_db; SOURCE D:/software/nacos/nacos-server-2.3.1/conf/mysql-schema.sql; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值