Learn how to set up and use MySQL MCP server for secure database interactions in Docker environments
The MySQL MCP Server acts as an intermediary between Model Context Protocol (MCP) clients, such as AI applications like Claude Desktop, Continue, and Cursor, and the MySQL database system. It enables these sophisticated AI tools to interact with relational databases through standardized protocols, enhancing their capabilities for data processing and analysis.
This server provides essential functionalities including read-only queries (query), write operations that are ultimately rolled back (test_execute), listing tables, and describing table structures. These features ensure that AI applications can perform various database operations while maintaining the safety and integrity of data through rollback mechanisms.
Execute Read-Only Query:
query
{
"type": "query",
"payload": {
"sql": "SELECT * FROM your_table"
}
}
{
"success": true,
"data": [
{"id": 1, "name": "example"}
]
}
Test Query Execution (Write Operation):
test_execute
{
"type": "test_execute",
"payload": {
"sql": "UPDATE your_table SET name = 'updated' WHERE id = 1"
}
}
{
"success": true,
"data": "The UPDATE SQL query can be executed."
}
List Tables:
list_tables
{}
{
"success": true,
"data": ["table1", "table2", "table3"]
}
Describe Table:
describe_table
{
"type": "describe_table",
"payload": {
"table": "your_table"
}
}
{
"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": ""}
]
}
mysql2
: A package for interacting with MySQL databases.To set up the MySQL MCP Server, follow these steps:
Create or modify your environment variables file ~/.mcp/.env
as follows:
MYSQL_HOST=host.docker.internal # Hostname to access host services from Docker container
MYSQL_PORT=3306
MYSQL_USER=root
MYSQL_PASSWORD=your_password
Note: Use host.docker.internal
for accessing a MySQL server running on your host. Adjust if connecting to a different database.
Edit the mcp.json
file with the following 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"
]
}
}
}
To start the server, run the following command:
docker run -i --rm --add-host=host.docker.internal:host-gateway --env-file ~/.mcp/.env ghcr.io/xiangma9712/mcp/mysql
For OrbStack users, the --add-host
option is automatically supported.
Imagine an AI model that requires regular updates of a large dataset from a MySQL database. By integrating with the MySQL MCP Server, the AI application can periodically execute query
and list_tables
commands to fetch and analyze the latest data.
A real-time monitoring system can use the test_execute
command to simulate write operations before they are committed. If these operations pass validation checks, they are executed. This ensures that any data changes in the AI application are safely reflected in the database without causing disruptions.
The MySQL MCP Server supports integration with major MCP clients, including:
See the compatibility matrix for detailed support status across these platforms.
MCP Client | Resources | Tools | Prompts | Status |
---|---|---|---|---|
Claude Desktop | ✅ | ✅ | ✅ | Full Support |
Continue | ✅ | ✅ | ✅ | Full Support |
Cursor | ❌ | ✅ | ❌ | Tools Only |
Given that the server is implemented in TypeScript and Docker, it ensures high performance and compatibility across a wide range of Linux distributions.
{
"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"
]
}
}
}
graph TD
A[AI Application] -->|MCP Client| B[MCP Protocol]
B --> C[MCP Server]
C --> D[Data Source/Tool]
style A fill:#e1f5fe
style C fill:#f3e5f5
style D fill:#e8f5e8
Q: How do I install the MySQL MCP Server?
Q: Can this server be used with OrbStack or other systems?
host.docker.internal
is automatically supported by OrbStack.Q: Is there any limit on the SQL queries executed through MCP clients?
Q: How does this server handle security concerns like SQL injection?
Q: What is the recommended network configuration for production use?
Contributions are welcome! To contribute, follow these steps:
npm test
.Explore more about the Model Context Protocol (MCP) on its official website or documentation for further integration insights and resource guidance.
By integrating the MySQL MCP Server, developers can significantly enhance their AI applications' database interactions, ensuring secure, efficient, and scalable data processing capabilities.
RuinedFooocus is a local AI image generator and chatbot image server for seamless creative control
Simplify MySQL queries with Java-based MysqlMcpServer for easy standard input-output communication
Build stunning one-page websites track engagement create QR codes monetize content easily with Acalytica
Learn to set up MCP Airflow Database server for efficient database interactions and querying airflow data
Explore CoRT MCP server for advanced self-arguing AI with multi-LLM inference and enhanced evaluation methods
Access NASA APIs for space data, images, asteroids, weather, and exoplanets via MCP integration