Content
# MySQL MCP Server
Let AI assistants (Claude, VS Code, OpenCode, etc.) connect and operate MySQL databases directly through the MCP protocol.
## Quick Start
```bash
# 1. Clone this repository
git clone https://github.com/panda-doctor/MySQL-MCP.git
cd MySQL-MCP
# 2. Start your MCP client (Claude Code or others)
claude
```
The MCP server will start automatically, and the AI can query your MySQL database.
## Prerequisites
| Dependency | Description |
|---|---|
| **MySQL Database** | Locally running MySQL instance (configure target database name in `.mcp.json`) |
| **mysql_mcp_server** | MySQL MCP driver, responsible for translating MCP protocol to MySQL protocol |
| **MCP Client** | Claude Code / VS Code + GitHub Copilot / OpenCode, etc. |
### Install mysql_mcp_server
```bash
# Method 1: pip installation (recommended)
pip install mysql-mcp-server
# Method 2: npm installation
npm install -g @anthropic/mysql-mcp-server
# Verify installation
mysql_mcp_server --version
```
## Configuration File Description
### `.mcp.json` — MCP Server Configuration (Example)
```json
{
"mcpServers": {
"mysql": {
"type": "stdio",
"command": "mysql_mcp_server",
"args": [],
"env": {
"MYSQL_HOST": "localhost",
"MYSQL_PORT": "3306",
"MYSQL_USER": "root",
"MYSQL_PASSWORD": "your database password",
"MYSQL_DATABASE": "your database name"
}
}
}
}
```
> ⚠️ **Security Reminder**: The password in `.mcp.json` is in plain text. It is recommended to change `MYSQL_PASSWORD` to read from an environment variable or ensure the repository is private.
### `.claude/settings.local.json` — Claude Permission Configuration
Configured `mysql_mcp_server` SQL execution permissions and `playwright-cli` browser automation skills.
## Available Tools
After successful connection, AI can call the following database tools:
### 1. Execute SQL Statements
```sql
-- Query data
SELECT * FROM users LIMIT 10;
-- Insert data
INSERT INTO posts (title, content) VALUES ('Hello', 'World');
-- Update data
UPDATE users SET name = 'new name' WHERE id = 1;
-- Create table, drop table, and other DDL operations
CREATE TABLE test (id INT PRIMARY KEY);
```
### 2. View Table Structure
```sql
-- View all tables in the current database
SHOW TABLES;
-- View single table structure
DESCRIBE users;
-- View across databases
DESCRIBE other_database.table_name;
```
### 3. Preview Data
```sql
SELECT * FROM users LIMIT 5;
SELECT COUNT(*) FROM users;
```
## Usage Examples
### Scenario 1: Explore Database
> **You:** "View the tables in the current database"
>
> **Claude:** Execute `SHOW TABLES`, return table list
>
> **You:** "Check the structure and data of the users table"
>
> **Claude:** Execute `DESCRIBE users` + `SELECT * FROM users LIMIT 5`
### Scenario 2: Data Analysis
> **You:** "Query the number of registered users this month"
>
> **Claude:**
> ```sql
> SELECT COUNT(*) FROM users
> WHERE created_at >= DATE_FORMAT(CURDATE(), '%Y-%m-01');
> ```
### Scenario 3: Database Management
> **You:** "Add a category field to the posts table"
>
> **Claude:**
> ```sql
> ALTER TABLE posts ADD COLUMN category VARCHAR(50);
> ```
## Cross-Database Queries
If you need to access other databases, use the format `database_name.table_name`:
```sql
SELECT * FROM other_db.table_name;
```
No need to execute `USE other_db;` first.
## Multi-Platform Usage
### Claude Code (Command Line)
```bash
# Start in the project directory
cd MySQL-MCP
claude
# Or specify configuration with --mcp in other directories
claude --mcp .mcp.json
```
### VS Code + GitHub Copilot
1. Configure MCP server to point to `.mcp.json` in VS Code settings
2. Ask database questions in the Chat panel
### OpenCode / Cursor / Windsurf
Refer to each tool's documentation and import the `.mcp.json` configuration file of this project.
## Related Skills
This repository comes with a `playwright-cli` skill (located in `.claude/skills/playwright-cli/`), supporting browser automation operations, which can be used for end-to-end testing with databases.
## Frequently Asked Questions
**Q: Connection prompts "Access denied"**
Check if the username and password in `.mcp.json` are correct, and if MySQL allows remote connections.
**Q: mysql_mcp_server not found**
Ensure it is correctly installed and in the PATH environment variable. You can also change the command to an absolute path.
**Q: Want to manage password with environment variables only**
Set `MYSQL_PASSWORD` in system environment variables, then reference it in `.mcp.json` `env`:
```json
"env": {
"MYSQL_PASSWORD": "${MYSQL_PASSWORD}"
}
```