数据库行业第二春来了:AI应用通过MCP接管两大国产数据库PolarDB & OB
模型上下文协议(MCP) 是一种开放协议,可实现 LLM 应用程序与外部数据源和工具之间的无缝集成。无论您是构建 AI 驱动的 IDE、增强聊天界面还是创建自定义 AI 工作流,MCP 都提供了一种标准化方法,可将 LLM 与所需的上下文连接起来。
如果我们需要将数据库接入AI应用, 让大模型来管理数据库, 理论上我们只需要以下4种模块即可:
- 数据库相关的mcp server , (mcp server里需要定义清楚与你期望的数据库管理行为相关的: 资源、工具、prompt)
mcp client和mcp server之间的消息需遵循JSON-RPC 2.0 ( https://www.jsonrpc.org/specification )
流程图如下:
MCP 目前为客户端 - 服务端通信定义了两种标准传输机制:
- HTTP with Server-Sent Events(SSE)
客户端应该尽可能支持 stdio。如果不支持sse, 有个开源项目mcp proxy, 可以把sse转换为stdio通信.
- https://modelcontextprotocol.io/docs/concepts/transports
- https://github.com/sparfenyuk/mcp-proxy
例如Claude Desktop这款应用加入只支持stdio通信, 那么就可以用mcp-proxy来使Claude Desktop能够和支持sse的远程mcp server进行交互.
或者反之, 如果mcp server只支持stdio, 为了让远程AI应用能与之通信, 也可以使用mcp proxy.
如何开发mcp server/client:
- https://modelcontextprotocol.io/quickstart/server
- https://modelcontextprotocol.io/quickstart/client
为什么说是第二春呢?门槛降低了,你不用懂数据库,你只需要动动手,LLM会通过mcp帮你完成需求;使用数据库的频率/流量变大了,一方面是降低门槛后扩展的用户群体边界,另一方面是用户从人扩展到了AI Agent。流量变大,不就相当于第二春么!
下面我选择一款AI应用, 通过mcp把两大国产数据库PolarDB & OB与AI完美整合.
两大国产数据库PolarDB & OB通过MCP与AI完美整合
- 数据库PolarDB和Oceanbase, 搭建方法很简单, 参考如下:
- 例如天舟老哥开源的dbhub. https://github.com/bytebase/dbhub
- postgresql mcp server: https://github.com/modelcontextprotocol/servers/tree/main/src/postgres
- oceanbase 用mysql mcp server也能连. https://github.com/designcomputer/mysql_mcp_server
- oceanbase 自带了mcp server. https://github.com/oceanbase/mcp-oceanbase
- PolarDB 因为兼容PostgreSQL, 所以可以选择支持PostgreSQL的mcp server即可,
- 更多mcp server项目参考: https://github.com/modelcontextprotocol/servers
- 选择一款AI应用: ollama ,  别问我为什么不用Claude desktop, 我们不在服务区内. 
- 因为ollama目前不支持mcp client, 无法和mcp server进行通信. 所以选择oterm这个开源项目, 它能利用ollama, 并支持mcp.
- https://ggozad.github.io/oterm/app_config/
- https://github.com/ggozad/oterm/tree/main/docs
准备工作就绪就可以开干了.
1、参照下面两篇文章构建PolarDB和Oceanbase实例.
在Oceanbase中创建测试用户digoal, 允许从任何地址连接
CREATE USER 'digoal' IDENTIFIED BY 'test';  
GRANT ALL PRIVILEGES ON *.* TO 'digoal'@'%' IDENTIFIED BY 'test';  
在PolarDB中创建测试用户digoal, 允许从任何地址连接
create role digoal login superuser encrypted password 'test';  
  
# pg_hba.conf  
~/primary/pg_hba.conf  
host all all 0.0.0.0/0 md5  
假设PolarDB和OB的监听分别为:
OB: 172.17.0.2:2881   
PolarDB: 172.17.0.3:5432   
  
在容器内也可以访问这个域名: "host.docker.internal" 指向宿主机地址, 前提是你的数据库容器启动时已将port expose到宿主机  
2、配置数据库的mcp server
PolarDB使用bytebase开源的dbhub, 或postgresql mcp server
cd ~  
docker pull mcp/postgres  
docker pull bytebase/dbhub   
Oceanbase使用自带的mcp或mysql mcp
cd ~  
git clone --depth 1 https://github.com/oceanbase/mcp-oceanbase  
git clone --depth 1 https://github.com/designcomputer/mysql_mcp_server  
3、拉起ollama服务.
OLLAMA_HOST=0.0.0.0:11434 OLLAMA_KEEP_ALIVE=-1 nohup ollama serve >> ~/.ollama.log 2>&1 &  
目前已拉去哪些模型? 注意只有支持tools的模型能和mcp整合. 例如 qwen2.5:1.5b
$ ollama list  
NAME                             ID              SIZE      MODIFIED       
ds-qwen2.5-1.5b-digoal:latest    2c6aaa8a497c    3.6 GB    4 weeks ago       
qwen2.5:1.5b                     65ec06548149    986 MB    4 weeks ago       
deepseek-r1:7b                   0a8c26691023    4.7 GB    5 weeks ago       
qwen_1.5b_test1:latest           682ad25636bd    1.1 GB    6 weeks ago       
deepseek-r1:1.5b                 a42b25d8c10a    1.1 GB    6 weeks ago       
deepseek-r1:14b                  ea35dfe18182    9.0 GB    2 months ago      
mxbai-embed-large:latest         468836162de7    669 MB    4 months ago   
如何查询模型是否支持tools call? /show modelfile
ollama run qwen2.5:1.5b  
/show modelfile  
  
...  
  
FROM /Users/digoal/.ollama/models/blobs/sha256-183715c435899236895da3869489cc30ac241476b4971a20285b1a462818a5b4  
TEMPLATE """{{- if .Messages }}  
{{- if or .System .Tools }}<|im_start|>system  
{{- if .System }}  
{{ .System }}  
{{- end }}  
{{- if .Tools }}  
# Tools  
You may call one or more functions to assist with the user query.  
You are provided with function signatures within <tools></tools> XML tags:  
<tools>  
{{- range .Tools }}  
{"type": "function", "function": {{ .Function }}}  
{{- end }}  
</tools>  
  
...  
4、安装并配置oterm
# 假设你已升级到python 3.12.x  
pip install oterm  
配置oterm , 增加mcpServers设置.
# command + args 从命令行运行的命令得到.    
# 例如: python /Users/digoal/mysql_mcp_server/src/mysql_mcp_server/server.py   
# 例如: docker run -i --rm bytebase/dbhub --transport stdio --dsn postgres://digoal:test@172.17.0.3:5432/postgres?sslmode=disable   
$ cat /Users/digoal/Library/Application\ Support/oterm/config.json   
{"theme": "textual-dark", "splash-screen": true,  
"mcpServers": {  
    "oceanbase": {  
      "command": "python",  
      "args": [  
        "/Users/digoal/mcp-oceanbase/src/oceanbase_mcp_server/server.py"
      ],  
      "env": {  
        "OB_HOST": "localhost",  
        "OB_PORT": "2881",  
        "OB_USER": "digoal",  
        "OB_PASSWORD": "test",  
        "OB_DATABASE": "test"
      }  
    },  
    "dbhub": {  
      "command": "docker",  
      "args": [  
        "run", "-i", "--rm", "bytebase/dbhub", "--transport", "stdio", "--dsn", "postgres://digoal:test@172.17.0.3:5432/postgres?sslmode=disable"
      ]  
    },  
    "PolarDB": {  
      "command": "docker",  
      "args": [  
        "run",   
        "-i",   
        "--rm",   
        "mcp/postgres",   
        "postgresql://digoal:test@172.17.0.3:5432/postgres?sslmode=disable"]  
    },  
    "mysql": {  
      "command": "python",  
      "args": [  
        "/Users/digoal/mysql_mcp_server/src/mysql_mcp_server/server.py"
      ],  
      "env": {  
        "MYSQL_HOST": "localhost",  
        "MYSQL_PORT": "2881",  
        "MYSQL_USER": "digoal",  
        "MYSQL_PASSWORD": "test",  
        "MYSQL_DATABASE": "test"
      }  
    }  
  }  
}  
启动oterm
oterm  
新建chat对话框, 配置tools, 提示词等.在对话框中使用自然语言向大模型发起请求, 调用对应的tools, 通过mcp server向数据库发起SQL.
前面都还好, 到后面不知道为什么就犯浑不调用tools执行了. 看样子还是要换Claude desktop试一试.
更多让AI真正能干DBA活的function或工作流定义可以参考这个开源项目定义“剧本”的做法: https://github.com/xataio/agent题外话
接下里的AI前端应用应该都会陆续加入mcp的能力, 让AI和更多的应用连接起来才能真正发挥AI的能力, 实现AI驱动/赋能万物.
但是这样的AI应用入口会在哪里?
C端应用可能出现在苹果手机端?
企业应用入口呢? Oracle? salesforce? ITIL?
参考
https://github.com/xataio/agent
https://modelcontextprotocol.io/introduction
https://modelcontextprotocol.io/docs/concepts/transports
https://spec.modelcontextprotocol.io/
https://github.com/modelcontextprotocol/python-sdk
https://github.com/modelcontextprotocol/quickstart-resources/blob/main/mcp-client-python/client.py
https://github.com/oceanbase/mcp-oceanbase/blob/main/src/oceanbase_mcp_server/server.py
https://github.com/punkpeye/awesome-mcp-clients/?tab=readme-ov-file#what-is-mcp
https://github.com/evalstate/fast-agent
https://llmindset.co.uk/resources/fast-agent/
https://mcp.so/client/mcp_client_ollama/georgi-terziyski?tab=content
https://github.com/georgi-terziyski/mcp_client_ollama/tree/749dbe0e8f91e05831c159d64908141e7808b824
https://modelcontextprotocol.io/docs/concepts/transports
https://github.com/bytebase/dbhub
https://github.com/sparfenyuk/mcp-proxy
https://github.com/ollama/ollama/issues/7865
https://ggozad.github.io/oterm/app_config/
https://github.com/mark3labs/mcphost
https://github.com/bytebase/dbhub/issues/9
https://github.com/designcomputer/mysql_mcp_server
https://mcp.so/server/mcp_client_ollama?tab=content
https://github.com/oceanbase/mcp-oceanbase/blob/main/doc/oceanbase_mcp_server.md