mcp-server-mysql

liliangshan/mcp-server-mysql

3.2

If you are the rightful owner of mcp-server-mysql and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to henry@mcphub.com.

MCP Tool server application with MySQL DDL operations, permission control and operation logs, supports npx/global commands.

MCP MySQL Server

A MCP MySQL server with DDL support, permission control and operation logs.

Features

  • āœ… SQL query execution (DDL and DML)
  • āœ… Database information retrieval
  • āœ… Operation logging
  • āœ… Connection pool management
  • āœ… Auto-reconnection mechanism
  • āœ… Health checks
  • āœ… Error handling and recovery

Installation

Global Installation (Recommended)

npm install -g @liangshanli/mcp-server-mysql

Local Installation

npm install @liangshanli/mcp-server-mysql

From Source

git clone https://github.com/liliangshan/mcp-server-mysql.git
cd mcp-server-mysql
npm install

Configuration

Set environment variables:

export MYSQL_HOST=localhost
export MYSQL_PORT=3306
export MYSQL_USER=root
export MYSQL_PASSWORD=your_password
export MYSQL_DATABASE=your_database
export ALLOW_DDL=true
export ALLOW_DROP=false
export ALLOW_DELETE=false

Usage

1. Direct Run (Global Installation)

mcp-server-mysql

2. Using npx (Recommended)

npx @liangshanli/mcp-server-mysql

3. Direct Start (Source Installation)

npm start

4. Managed Start (Recommended for Production)

npm run start-managed

Managed start provides:

  • Auto-restart (up to 10 times)
  • Error recovery
  • Process management
  • Logging

5. Development Mode

npm run dev

Editor Integration

Cursor Editor Configuration

  1. Create .cursor/mcp.json file in your project root:
{
  "mcpServers": {
    "mysql": {
      "command": "npx",
      "args": ["@liangshanli/mcp-server-mysql"],
      "env": {
        "MYSQL_HOST": "your_host",
        "MYSQL_PORT": "3306",
        "MYSQL_USER": "your_user",
        "MYSQL_PASSWORD": "your_password",
        "MYSQL_DATABASE": "your_database",
        "ALLOW_DDL": "false",
        "ALLOW_DROP": "false",
        "ALLOW_DELETE": "false"
      }
    }
  }
}

VS Code Configuration

  1. Install the MCP extension for VS Code
  2. Create .vscode/settings.json file:
{
  "mcp.servers": {
    "mysql": {
      "command": "npx",
      "args": ["@liangshanli/mcp-server-mysql"],
      "args": ["@liangshanli/mcp-server-mysql"],
      "env": {
        "MYSQL_HOST": "your_host",
        "MYSQL_PORT": "3306",
        "MYSQL_USER": "your_user",
        "MYSQL_PASSWORD": "your_password",
        "MYSQL_DATABASE": "your_database",
        "ALLOW_DDL": "false",
        "ALLOW_DROP": "false",
        "ALLOW_DELETE": "false"
      }
    }
  }
}

As MCP Server

The server communicates with MCP clients via stdin/stdout after startup:

{"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {"protocolVersion": "2025-06-18"}}

Available Tools

  1. sql_query: Execute SQL queries

    {
      "jsonrpc": "2.0",
      "id": 2,
      "method": "tools/call",
      "params": {
        "name": "sql_query",
        "arguments": {
          "sql": "SELECT * FROM users LIMIT 10"
        }
      }
    }
    
  2. get_database_info: Get database information

    {
      "jsonrpc": "2.0",
      "id": 3,
      "method": "tools/call",
      "params": {
        "name": "get_database_info",
        "arguments": {}
      }
    }
    
  3. get_operation_logs: Get operation logs

    {
      "jsonrpc": "2.0",
      "id": 4,
      "method": "tools/call",
      "params": {
        "name": "get_operation_logs",
        "arguments": {
          "limit": 50,
          "offset": 0
        }
      }
    }
    

Connection Pool Features

  • Auto-creation: Automatically creates connection pool on notifications/initialized
  • Health checks: Checks connection pool status every 5 minutes
  • Auto-reconnection: Automatically recreates connection pool when it fails
  • Connection reuse: Uses connection pool for better performance
  • Graceful shutdown: Properly releases connections when server shuts down

Logging

Log file location: ./logs/mcp-mysql.log

Logged content:

  • All requests and responses
  • SQL operation records
  • Error messages
  • Connection pool status changes

Error Handling

  • Individual request errors don't affect the entire server
  • Connection pool errors are automatically recovered
  • Process exceptions are automatically restarted (managed mode)

Environment Variables

VariableDefaultDescription
MYSQL_HOSTlocalhostMySQL host address
MYSQL_PORT3306MySQL port
MYSQL_USERrootMySQL username
MYSQL_PASSWORDMySQL password
MYSQL_DATABASEDatabase name
ALLOW_DDLfalseWhether to allow DDL operations (CREATE, ALTER, TRUNCATE, RENAME, COMMENT). Set to 'true' to enable
ALLOW_DROPfalseWhether to allow DROP operations. Set to 'true' to enable
ALLOW_DELETEfalseWhether to allow DELETE operations. Set to 'true' to enable
MCP_LOG_DIR./logsLog directory
MCP_LOG_FILEmcp-mysql.logLog filename

Development

Project Structure

mcpmysql/
ā”œā”€ā”€ src/
│   └── server-final.js    # Main server file
ā”œā”€ā”€ start-server.js        # Managed startup script
ā”œā”€ā”€ package.json
└── README.md

Testing

npm test

Quick Start

1. Install Package

npm install -g @liangshanli/mcp-server-mysql

2. Configure Environment Variables

export MYSQL_HOST=localhost
export MYSQL_PORT=3306
export MYSQL_USER=root
export MYSQL_PASSWORD=your_password
export MYSQL_DATABASE=your_database
export ALLOW_DDL=false
export ALLOW_DROP=false
export ALLOW_DELETE=false

Permission Control Examples:

# Default: Disable all destructive operations (safe mode)
export ALLOW_DDL=false
export ALLOW_DROP=false
export ALLOW_DELETE=false

# Allow DDL but disable DROP and DELETE
export ALLOW_DDL=true
export ALLOW_DROP=false
export ALLOW_DELETE=false

# Allow everything except DELETE
export ALLOW_DDL=true
export ALLOW_DROP=true
export ALLOW_DELETE=false

# Enable all operations (use with caution)
export ALLOW_DDL=true
export ALLOW_DROP=true
export ALLOW_DELETE=true

3. Run Server

mcp-server-mysql

License

MIT