MySQL MCP Server
An MCP server for interacting with MySQL databases.
This server supports executing read-only queries (query) and write queries that are ultimately rolled back (test_execute).
Setup
Environment Variables
Add the following environment variables to ~/.mcp/.env
:
MYSQL_HOST=host.docker.internal # Hostname to access host services from Docker container
MYSQL_PORT=3306
MYSQL_USER=root
MYSQL_PASSWORD=your_password
Note:
host.docker.internal
is a special DNS name for accessing host machine services from Docker containers. Use this setting when connecting to a MySQL server running on your host machine. If connecting to a different MySQL server, change to the appropriate hostname.
mcp.json Configuration
{
"mcpServers": {
"mysql": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"--add-host=host.docker.internal:host-gateway",
"--env-file",
"/Users/username/.mcp/.env",
"ghcr.io/xiangma9712/mcp/mysql"
]
}
}
}
Usage
Starting the Server
docker run -i --rm --add-host=host.docker.internal:host-gateway --env-file ~/.mcp/.env ghcr.io/xiangma9712/mcp/mysql
Note: If you’re using OrbStack,
host.docker.internal
is automatically supported, so the--add-host
option can be omitted. While Docker Desktop also typically supports this automatically, adding the--add-host
option is recommended for better reliability.
Available Commands
1. Execute Read-only Query
{
"type": "query",
"payload": {
"sql": "SELECT * FROM your_table"
}
}
Response:
{
"success": true,
"data": [
{
"id": 1,
"name": "example"
}
]
}
2. Test Query Execution
{
"type": "test_execute",
"payload": {
"sql": "UPDATE your_table SET name = 'updated' WHERE id = 1"
}
}
Response:
{
"success": true,
"data": "The UPDATE SQL query can be executed."
}
3. List Tables
{
"type": "list_tables"
}
Response:
{
"success": true,
"data": ["table1", "table2", "table3"]
}
4. Describe Table
{
"type": "describe_table",
"payload": {
"table": "your_table"
}
}
Response:
{
"success": true,
"data": [
{
"Field": "id",
"Type": "int(11)",
"Null": "NO",
"Key": "PRI",
"Default": null,
"Extra": ""
},
{
"Field": "name",
"Type": "varchar(255)",
"Null": "YES",
"Key": "",
"Default": null,
"Extra": ""
}
]
}
Implementation Details
- Implemented in TypeScript
- Uses mysql2 package
- Runs as a Docker container
- Accepts JSON commands through standard input
- Returns JSON responses through standard output
- Uses
host.docker.internal
to connect to host MySQL (compatible with both OrbStack and Docker Desktop)
Security Considerations
- Uses environment variables for sensitive information management
- SQL injection prevention is the implementer’s responsibility
- Proper network configuration required for production use
- Appropriate firewall settings needed when connecting to host machine services
MySQL MCP Server
Project Details
- xiangma9712/mysql-mcp-server
- mcp-mysql
- MIT License
- Last Updated: 3/25/2025
Categories
Recomended MCP Servers
This an Model Context Protocol (MCP) server that integrates Notion into your AI workflows. This server enables seamless...
An MCP server for managing todos within LLMs, created for educational purposes
大家好!我是功能丰富的 MCP 服务,旨在打破设备与服务的隔阂,为用户带来便捷体验。 天气工具和气象平台联动,快速为用户推送全球实时天气,助力大家规划出行。控制浏览器工具模拟人工操作,自动搜索、浏览网页,大幅节省时间。摄像头工具调用本地摄像头拍照、录像,实现人脸识别,保障家庭安防。 为实现工具协同,我搭建了稳定框架,开发者可以基于现有服务进行拓展
Model Context Protocol Server for NebulaGraph 3.x
simple logseq mcp server
Model Context Protocol (MCP) server for using the Eyevinn Open Source Cloud API
Model Context Protocol Minecraft Server
A Model Context Protocol (MCP) server that enables LLMs to interact directly with Couchbase databases. Query buckets, execute...
Model Context Protocol (MCP) server that interacts with a Language Server