mysql-mcp-server
by: xiangma9712
mcp server accessing MySQL database
📌Overview
Purpose: To provide an MCP (Managed Cloud Platform) server that allows interaction with MySQL databases, supporting query execution while maintaining data integrity.
Overview: The MySQL MCP Server enables users to execute read-only queries and perform test write queries that are rolled back, ensuring no permanent changes are made to the database. It is designed to run within a Docker container and communicates through JSON formatted commands.
Key Features:
-
Execute Read-only Query: Allows users to perform SELECT statements to retrieve data from a MySQL database, ensuring safe data retrieval without altering the database state.
-
Test Query Execution: Enables users to test write operations (like UPDATE) by confirming if the operation can be executed without making permanent changes, aiding in safer application development.
-
List Tables & Describe Table: Provides functionality to view all database tables and detailed structure of specific tables, which facilitates better understanding of the database schema.
-
Environment Configuration: Utilizes environment variables for secure connection settings, making it easy to configure and deploy in various environments.
MySQL MCP Server
An MCP server for interacting with MySQL databases.
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
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. Otherwise, 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
If you're using OrbStack,
host.docker.internal
is automatically supported, so the--add-host
option can be omitted. Docker Desktop also typically supports this automatically, but adding--add-host
is recommended for better reliability.
Available Commands
1. Execute Read-only Query
Request:
{
"type": "query",
"payload": {
"sql": "SELECT * FROM your_table"
}
}
Response:
{
"success": true,
"data": [
{
"id": 1,
"name": "example"
}
]
}
2. Test Query Execution
Request:
{
"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
Request:
{
"type": "list_tables"
}
Response:
{
"success": true,
"data": ["table1", "table2", "table3"]
}
4. Describe Table
Request:
{
"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