Databases·
intermediate
·11 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 analysisqueriesarchitecture

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

  1. A MySQL or MariaDB server running and accessible.
  2. Node.js 18+ installed on your host environment.
  3. 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:

sql
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.

json
{
  "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:

ToolDescription
queryExecute SQL SELECT queries
list_tablesList all tables inside the configured database
describe_tableGet exact table schemas, data types, and primary keys
list_databasesList 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_USER and MYSQL_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=utf8mb4 for 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:

bash
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