mysql mcp server
Enables interaction with a MySQL database via JSON commands, supporting read-only queries, test execution of write queries, and table information retrieval through Docker.
Enables interaction with a MySQL database via JSON commands, supporting read-only queries, test execution of write queries, and table information retrieval through Docker.
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).
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.
{
"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"
]
}
}
}
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.
{
"type": "query",
"payload": {
"sql": "SELECT * FROM your_table"
}
}
Response:
{
"success": true,
"data": [
{
"id": 1,
"name": "example"
}
]
}
{
"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."
}
{
"type": "list_tables"
}
Response:
{
"success": true,
"data": ["table1", "table2", "table3"]
}
{
"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": ""
}
]
}
host.docker.internal
to connect to host MySQL (compatible with both OrbStack and Docker Desktop)[
{
"description": "読み取り専用のSQLクエリを実行します。",
"inputSchema": {
"properties": {
"sql": {
"type": "string"
}
},
"required": [
"sql"
],
"type": "object"
},
"name": "query"
},
{
"description": "SQLが実行可能か確認し、最後にロールバックします。",
"inputSchema": {
"properties": {
"sql": {
"type": "string"
}
},
"required": [
"sql"
],
"type": "object"
},
"name": "test_execute"
},
{
"description": "データベース内のテーブル一覧を取得します。",
"inputSchema": {
"type": "object"
},
"name": "list_tables"
},
{
"description": "テーブルのカラム情報を取得します。",
"inputSchema": {
"properties": {
"tableName": {
"type": "string"
}
},
"required": [
"tableName"
],
"type": "object"
},
"name": "describe_table"
}
]