Add MySQL MCP to Claude Code
Adding MySQL MCP to Claude Code connects it directly to your database for schema inspection, query execution, and migration generation. This guide walks through the setup using a MySQL-compatible MCP server so Claude Code can write accurate SQL against your actual tables.
The Problem
Claude Code generates SQL that looks correct but fails on execution because it does not know your actual table structure. Column names are guessed, JOIN conditions reference nonexistent foreign keys, and data types are wrong. You spend more time fixing generated queries than writing them from scratch. Without direct schema access, Claude Code treats your database as a black box.
Quick Solution
Step 1: Install the MySQL MCP server
npm install -g @anthropic-ai/mcp-server-mysql
Step 2: Get your MySQL connection details
You need: host, port, username, password, and database name. For local development:
Host: 127.0.0.1
Port: 3306
User: root
Password: your-password
Database: myapp_dev
Step 3: Configure MCP in your project
Create .claude/mcp.json in your project root:
{
"mcpServers": {
"mysql": {
"command": "npx",
"args": ["-y", "@anthropic-ai/mcp-server-mysql"],
"env": {
"MYSQL_HOST": "127.0.0.1",
"MYSQL_PORT": "3306",
"MYSQL_USER": "root",
"MYSQL_PASSWORD": "your-password",
"MYSQL_DATABASE": "myapp_dev"
}
}
}
}
Step 4: Restart Claude Code and test
claude
Verify by asking:
> Show me the schema for the users table
Claude Code will query INFORMATION_SCHEMA through MCP and return your actual column definitions, indexes, and constraints.
How It Works
The MySQL MCP server establishes a persistent connection to your database and exposes tools for schema inspection and query execution. When Claude Code needs to generate a query, it calls the describe_table tool to fetch column names, types, and constraints. For complex queries involving JOINs, it calls list_tables and inspects foreign key relationships.
All queries execute on your local machine through the MCP server process. The MCP server acts as a thin bridge between Claude Code’s stdio protocol and the MySQL wire protocol. Your credentials and query results never leave your machine – they flow directly between the MCP process and your MySQL server.
CLAUDE.md rules complement MCP by encoding business logic that schema alone cannot convey – like which columns are soft-delete flags, which tables contain PII, or which indexes should be used for specific query patterns.
Common Issues
“Access denied for user” error on startup Verify credentials by connecting directly:
mysql -h 127.0.0.1 -P 3306 -u root -p myapp_dev
If this works but MCP fails, check that the password in mcp.json does not contain unescaped special characters in the JSON string.
MCP server cannot connect to Docker MySQL
If MySQL runs in Docker, use host.docker.internal on Mac or the container’s bridge IP on Linux. Alternatively, expose the port with -p 3306:3306 and connect to 127.0.0.1.
Slow schema queries on large databases
Databases with hundreds of tables cause slow INFORMATION_SCHEMA queries. Add a MYSQL_DATABASE environment variable to restrict the MCP server to a single database rather than scanning all schemas.
Example CLAUDE.md Section
# MySQL Project Configuration
## Database
- MySQL 8.0 via MCP (.claude/mcp.json)
- Development: myapp_dev (local Docker)
- Test: myapp_test (local Docker, wiped between runs)
- ORM: Prisma with mysql provider
## Schema Conventions
- Primary keys: `id` BIGINT AUTO_INCREMENT
- Timestamps: `created_at`, `updated_at` (DATETIME, NOT NULL)
- Soft deletes: `deleted_at` DATETIME NULL
- Foreign keys: `{table_singular}_id` naming convention
- Indexes: prefix with `idx_`
## Rules
- NEVER run DROP, TRUNCATE, or DELETE without WHERE clause
- Always generate Prisma migrations, not raw DDL
- Use transactions for multi-table updates
- Soft-delete by setting deleted_at, never hard delete
- All queries must account for deleted_at IS NULL
## Commands
- Migrate: `npx prisma migrate dev`
- Generate client: `npx prisma generate`
- Seed: `npx prisma db seed`
- Studio: `npx prisma studio`
Best Practices
-
Use a read-only MySQL user – Create a user with
SELECTandSHOW DATABASESprivileges only. This prevents Claude Code from accidentally running destructive queries during exploration. -
Connect to development, never production – Always point MCP at your local or staging database. Add a comment in your
mcp.jsonnoting which environment it targets. -
Combine MCP with your ORM – Tell Claude Code to read schemas via MCP but generate changes through your ORM (Prisma, TypeORM, Knex). This ensures migrations are tracked properly.
-
Document soft-delete patterns – MySQL MCP cannot infer that
deleted_at IS NULLshould be appended to every query. State this rule explicitly in CLAUDE.md. -
Restrict to one database – Set the
MYSQL_DATABASEenvironment variable to avoid exposing other databases on the same server to Claude Code.
Related Reading
- Claude Code MCP Server Setup
- Claude Code Database Schema Design Guide
- Claude Code for Postgres Full Text Search Workflow
Built by theluckystrike. More at zovo.one