Back to Codex
Databases·
intermediate
·9 min read·Apr 4, 2026

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.

MySQLdatabaseSQLdata analysisqueries

Use the MySQL MCP Server for Database Queries

The MySQL MCP server connects your AI agent to MySQL databases, enabling schema exploration and SQL querying.

Prerequisites

  • MySQL server running and accessible
  • Node.js 18+
  • An MCP-compatible host (Claude Desktop, etc.)

Configuration

json
{
  "mcpServers": {
    "mysql": {
      "command": "npx",
      "args": ["-y", "@benborber/mcp-server-mysql"],
      "env": {
        "MYSQL_HOST": "localhost",
        "MYSQL_PORT": "3306",
        "MYSQL_USER": "mcp_reader",
        "MYSQL_PASSWORD": "your_password",
        "MYSQL_DATABASE": "your_database"
      }
    }
  }
}

Available Tools

ToolDescription
code
query
Execute SQL queries
code
list_tables
List all database tables
code
describe_table
Get table schema and column info
code
list_databases
List available databases

Creating a Safe MySQL User

Always create a dedicated read-only user:

sql
CREATE USER 'mcp_reader'@'localhost' IDENTIFIED BY 'secure_password';
GRANT SELECT ON your_database.* TO 'mcp_reader'@'localhost';
FLUSH PRIVILEGES;

Example Queries

Once connected, ask your AI agent:

  • "Show me the table structure of the orders table"
  • "What are the top 10 products by revenue this month?"
  • "Find all customers who haven't ordered in the last 90 days"
  • "Create a summary of daily order volumes for the past quarter"

Connection Options

For remote databases or SSL connections:

json
{
  "env": {
    "MYSQL_HOST": "db.example.com",
    "MYSQL_PORT": "3306",
    "MYSQL_USER": "mcp_reader",
    "MYSQL_PASSWORD": "password",
    "MYSQL_DATABASE": "production",
    "MYSQL_SSL": "true"
  }
}

Troubleshooting

  • Access denied: Check user permissions and host restrictions
  • Connection timeout: Verify network connectivity and firewall rules
  • SSL errors: Ensure SSL certificates are properly configured
  • Character encoding: Set
    code
    MYSQL_CHARSET=utf8mb4
    for Unicode support