home / mcp / mysql mcp server

MySQL MCP Server

Provides MySQL database operations via MCP for querying, updating, and managing schemas.

Installation
Add the following to your MCP client configuration file.

Configuration

View docs
{
  "mcpServers": {
    "kevinbin-mcp-mysql-server": {
      "command": "node",
      "args": [
        "/absolute/path/to/mcp-mysql-server/build/index.js"
      ],
      "env": {
        "DB_HOST": "localhost",
        "DB_USER": "root",
        "DB_DATABASE": "mydb",
        "DB_PASSWORD": "s3cr3t",
        "DATABASE_URL": "mysql://user:password@host:3306/database"
      }
    }
  }
}

You can connect to a MySQL database through a dedicated MCP (Model Context Protocol) server that exposes standard database operations. This server lets AI models interact with MySQL databases via a consistent interface, enabling queries, updates, and schema exploration without embedding direct database logic in your prompts.

How to use

To use this MCP server, configure your MCP client to connect to the local node-based server you run from the built build/index.js file. Once connected, you can perform common database actions via the MCP tools: connect to the database, run SELECT queries, execute INSERT/UPDATE/DELETE statements, list tables, inspect table schemas, create new tables, and add columns. These operations are executed through the MCP interface with prepared statement support for safe parameter handling, and automatic cleanup of connections when you finish.

How to install

Prerequisites: you need Node.js and npm installed on your system.

# Option 1: Install via Smithery (client side)

npx -y @smithery/cli install @enemyrr/mcp-mysql-server --client claude

Option 2: Install manually and build the server locally.

# Step 1: Clone the repository
git clone https://github.com/enemyrr/mcp-mysql-server.git
cd mcp-mysql-server

# Step 2: Install dependencies
npm install

# Step 3: Build the server
npm run build

Step 4: Add the MCP server to your client (Cursor IDE example). You will point the client to the built runtime. Use the following settings.

Name: mysql
Type: command
Command: node /absolute/path/to/mcp-mysql-server/build/index.js

Configuration and connection methods

Configure how your server connects to MySQL in one of three ways. Choose the method that best fits your deployment.

Database connection methods

1) Database URL in .env (recommended): set a single URL that includes user, password, host, port, and database.

DATABASE_URL=mysql://user:password@host:3306/database

2) Individual parameters in .env: specify host, user, password, and database separately.

DB_HOST=localhost
DB_USER=your_user
DB_PASSWORD=your_password
DB_DATABASE=your_database

3) Direct connection via the MCP tool: pass either a URL or individual connection fields to the tool you are using inside your MCP client.

use_mcp_tool({
  server_name: "mysql",
  tool_name: "connect_db",
  arguments: {
    url: "mysql://user:password@host:3306/database"
    // OR
    workspace: "/path/to/your/project" // Will use project's .env
    // OR
    host: "localhost",
    user: "your_user",
    password: "your_password",
    database: "your_database"
  }
});

Available actions (tools) used by the MCP server

The server provides a set of actions you can perform through the MCP client to interact with MySQL databases.

Security and error handling

- The server uses prepared statements to prevent SQL injection. - Password handling can be secured via environment variables. - Queries are validated before execution. - Connections are automatically closed when finished. - Detailed error messages help diagnose connection failures, invalid queries, missing configuration, database errors, and schema validation issues.

Notes and troubleshooting

- If you encounter connection issues, verify that your DATABASE_URL or the individual env vars are correctly set and that the MySQL server is reachable from your host. - Ensure the build index.js path you provide to the client matches where you built the server. - When running locally, make sure Node.js is available in your PATH and that the port and host configurations do not conflict with existing services.

Notes on the MCP server identity and repository guidance

This server is designed to enable AI models to operate against a MySQL database through a standardized, secure interface. It supports multiple connection methods, schema management tools, prepared statements, and robust error handling to streamline AI-driven database interactions.

Available tools

connect_db

Connect to MySQL using a URL, workspace path, or direct credentials.

query

Execute SELECT queries with optional prepared statement parameters.

execute

Execute INSERT, UPDATE, or DELETE queries with optional prepared statement parameters.

list_tables

List all tables in the connected database.

describe_table

Get the structure of a specific table.

create_table

Create a new table with specified fields and indexes.

add_column

Add a new column to an existing table.