How to Use the MySQL MCP Server for Database Queries
Connect MySQL databases to your AI agent for querying, schema exploration, and data analysis using the MySQL MCP server.
Use the MySQL MCP Server for Database Queries
The MySQL MCP server connects your AI agent directly to your MySQL databases. This integration empowers your agent to autonomously explore schemas, write complex queries, and analyze tabular data without requiring you to manually export CSVs.
Prerequisites
- ▸A MySQL or MariaDB server running and accessible.
- ▸Node.js 18+ installed on your host environment.
- ▸An MCP-compatible client like Claude Desktop.
Creating a Safe MySQL User (Security First)
AI agents can hallucinate DROP TABLE or DELETE commands. You must always create a dedicated read-only user before connecting the MCP server:
CREATE USER 'mcp_reader'@'localhost' IDENTIFIED BY 'secure_password';
GRANT SELECT ON your_database.* TO 'mcp_reader'@'localhost';
FLUSH PRIVILEGES;Configuration Setup
Add the following to your MCP client config. Note that we pass connection parameters as distinct environment variables rather than a single URL.
{
"mcpServers": {
"mysql": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-mysql"],
"env": {
"MYSQL_HOST": "localhost",
"MYSQL_PORT": "3306",
"MYSQL_USER": "mcp_reader",
"MYSQL_PASSWORD": "secure_password",
"MYSQL_DATABASE": "your_database"
}
}
}
}Available Tools
Your agent will automatically detect and use these tools:
| Tool | Description |
|---|---|
query | Execute SQL SELECT queries |
list_tables | List all tables inside the configured database |
describe_table | Get exact table schemas, data types, and primary keys |
list_databases | List all available databases on the host |
Example Prompts
Once connected, ask your AI agent to fetch insights:
- ▸"Show me the table structure of the orders table."
- ▸"Write and execute a query to find the top 10 products by revenue this month."
- ▸"Find all customers who haven't ordered in the last 90 days and list their emails."
Troubleshooting
- ▸Access denied for user: Double-check your
MYSQL_USERandMYSQL_PASSWORD. Ensure the user host restriction (@'localhost') matches where the MCP server is actually running from. - ▸Connection timeout: If you are trying to connect to a remote MySQL server, ensure your firewall (or AWS Security Group) allows inbound traffic on port
3306. - ▸SSL errors: If connecting to a managed DB like PlanetScale, set
"MYSQL_SSL": "true"in your environment variables. - ▸Character encoding: Set
MYSQL_CHARSET=utf8mb4for Unicode support if you notice garbled text in the AI's responses.
Using with OpenAI Codex
You can use this MCP server with the OpenAI Codex CLI by adding it to your configuration:
codex mcp add --name mysql --command "npx -y @modelcontextprotocol/server-mysql mysql://user:password@/mydb"Note: Replace the connection string with your actual MySQL URL.
For a full list of recommended servers, see Best MCP Servers for OpenAI Codex.
Related Guides
How to Use the SQLite MCP Server for Local Database Analysis
Learn how to connect SQLite databases to your AI agent for local data analysis, querying, and schema exploration using the MCP protocol.
DatabasesHow to Use the MongoDB MCP Server for NoSQL Data Access
Connect MongoDB databases to AI agents for document querying, aggregation pipelines, and schema exploration using MCP.
DatabasesHow to Connect PostgreSQL to AI Agents Using MCP
Step-by-step guide to setting up the PostgreSQL MCP server. Give your AI agent read-only access to query your PostgreSQL databases safely.