Skip to main content
Glama
MCP Server for MariaDB/MySQL

MCP Server for MariaDB/MySQL

by Zdendys79

MCP Server for MariaDB/MySQL

A Model Context Protocol (MCP) server that provides database access to MariaDB and MySQL databases. This server enables Claude Code and other MCP clients to interact with your databases through a secure, standardized interface.

Features

  • Database Operations: Execute SQL queries, list tables, and inspect table structures

  • Multiple Database Support: Switch between databases on the same connection

  • Secure Connection: Support for both local and remote database connections

  • MCP Standard Compliance: Built with official @modelcontextprotocol/sdk

  • TypeScript: Fully typed for better development experience

Related MCP server: mcp-mssql-server

Installation

From source

git clone https://github.com/Zdendys79/mcp-server-mariadb.git
cd mcp-server-mariadb
npm install
npm run build

Configuration

Claude Code Configuration

Add this to your global Claude Code configuration file (~/.claude.json):

{
  "mcpServers": {
    "mariadb": {
      "command": "node",
      "args": ["/home/user/mcp-servers/mariadb/dist/index.js"],
      "env": {
        "DB_HOST": "localhost",
        "DB_PORT": "3306",
        "DB_USER": "your_database_user",
        "DB_PASS": "your_database_password",
        "DB_NAME": "your_database_name"
      }
    }
  }
}

Note on credentials:

  • ✅ Safe: Credentials in ~/.claude.json (global config, outside project, not versioned)

  • ⚠️ Advanced: Use wrapper scripts + environment variables (see Advanced Configuration below)

  • ❌ Never: Store credentials in project files (would be versioned in git)

Multiple Database Connections

You can configure multiple MCP server instances to connect to different databases simultaneously. This is useful when working with both local and remote databases, or multiple database servers.

Example: Local database + SSH-tunneled remote database

{
  "mcpServers": {
    "mariadb-local": {
      "command": "node",
      "args": ["/home/user/mcp-servers/mariadb/dist/index.js"],
      "env": {
        "DB_HOST": "127.0.0.1",
        "DB_PORT": "3306",
        "DB_USER": "local_user",
        "DB_PASS": "local_password",
        "DB_NAME": "local_database"
      }
    },
    "mariadb-remote": {
      "command": "node",
      "args": ["/home/user/mcp-servers/mariadb/dist/index.js"],
      "env": {
        "DB_HOST": "127.0.0.1",
        "DB_PORT": "3336",
        "DB_USER": "remote_user",
        "DB_PASS": "remote_password",
        "DB_NAME": "remote_database"
      }
    }
  }
}

Note: When using SSH tunnels, set up the tunnel first:

ssh -L 3336:localhost:3306 user@remote-server -N -f

Then configure the MCP server to connect to 127.0.0.1:3336.

Environment Variables

Database Connection

  • DB_HOST - Database host (default: 127.0.0.1)

  • DB_PORT - Database port (default: 3306)

  • DB_USER - Database username (default: claude_mcp)

  • DB_PASS - Database password (default: empty string)

  • DB_NAME - Database name (optional)

    • If set (non-empty): Database is automatically selected on startup - no need to call switch_database

    • If empty or not set: You must use the switch_database tool before executing queries

Connection Pool Settings (Advanced)

  • POOL_CONNECTION_LIMIT - Maximum number of connections in pool (default: 10)

  • POOL_QUEUE_LIMIT - Maximum number of queued connection requests (default: 0 = unlimited)

  • POOL_WAIT_FOR_CONNECTIONS - Wait for available connection when pool is full (default: true)

When to adjust pool settings:

  • High concurrent usage → increase POOL_CONNECTION_LIMIT

  • Memory constraints → decrease POOL_CONNECTION_LIMIT

  • Prevent queue buildup → set POOL_QUEUE_LIMIT > 0

Available Tools

list_databases

Lists all available databases on the server.

No parameters required

switch_database

Switches to a different database.

{
  "database": "your_database_name"
}

When to use:

  • Required: When no database is selected yet (DB_NAME not set and no previous switch_database call)

  • Optional: To change from currently active database to a different one

  • Not needed: If DB_NAME is set or database was already selected via previous switch_database call

Note: This overrides any database set via DB_NAME env variable. The newly selected database remains active until another switch_database call.

get_current_database

Returns the currently selected database name.

No parameters required

list_tables

Lists all tables in the currently selected database.

No parameters required

Note: Requires an active database. Database is active when set via DB_NAME env var OR selected via switch_database tool.

describe_table

Shows the structure of a specific table.

{
  "table": "table_name"
}

Note: Requires an active database. Database is active when set via DB_NAME env var OR selected via switch_database tool.

execute_sql

Executes a SQL query on the currently selected database.

{
  "query": "SELECT * FROM users LIMIT 10"
}

Note: Requires an active database. Database is active when set via DB_NAME env var OR selected via switch_database tool.

Usage Example

Once configured, you can use Claude Code to interact with your database:

You: Show me all tables in the 'production' database

Claude: [Uses switch_database tool with database="production"]
        [Uses list_tables tool]
        Here are the tables in your production database:
        - users
        - orders
        - products
        ...

You: What's the structure of the users table?

Claude: [Uses describe_table tool with table="users"]
        The users table has the following columns:
        - id (INT, PRIMARY KEY)
        - username (VARCHAR(255))
        - email (VARCHAR(255))
        ...

Security Considerations

Database Credentials

Safe credential storage:

  • ✅ Global config: ~/.claude.json (outside projects, not versioned in git)

  • ✅ Environment variables: ~/.profile or ~/.config/environment.d/*.conf

  • ✅ Wrapper scripts: Load credentials from environment (advanced)

Never:

  • ❌ Project files: Don't store credentials in project directories (would be versioned)

  • ❌ Git repositories: Never commit credentials to version control

  • ❌ Hardcoded: Don't hardcode credentials in source code

Best practices:

  • Use read-only users: Create dedicated database user with limited permissions

  • Network security: Use SSH tunnels or VPNs for remote database connections

  • Minimal permissions: Grant only necessary database privileges

Example: SSH Tunnel Setup

For secure remote access, set up an SSH tunnel:

ssh -L 3306:localhost:3306 user@remote-server

Then configure the MCP server to connect to localhost:3306.

Create a dedicated user with minimal permissions:

CREATE USER 'mcp_user'@'localhost' IDENTIFIED BY 'secure_password';
GRANT SELECT ON your_database.* TO 'mcp_user'@'localhost';
GRANT SHOW DATABASES ON *.* TO 'mcp_user'@'localhost';
FLUSH PRIVILEGES;

Adjust permissions based on your needs (add INSERT, UPDATE, DELETE if required).

Development

Building

npm run build

Running locally

npm start

Project Structure

mcp-server-mariadb/
├── src/
│   └── index.ts          # Main server implementation
├── dist/                 # Compiled JavaScript (generated)
├── examples/
│   └── legacy/           # Legacy bash implementation
├── .mcp.json.template    # Configuration template
├── package.json
├── tsconfig.json
└── README.md

Troubleshooting

Connection Issues

Error: ER_ACCESS_DENIED_ERROR

  • Check your DB_USER and DB_PASS environment variables

  • Verify the user has permission to connect from your host

Error: ECONNREFUSED

  • Check if MariaDB/MySQL is running

  • Verify DB_HOST and DB_PORT are correct

  • Check firewall rules

"No database selected" Error

You must call the switch_database tool before using list_tables, describe_table, or execute_sql.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository

  2. Create your feature branch (git checkout -b feature/amazing-feature)

  3. Commit your changes (git commit -m 'Add some amazing feature')

  4. Push to the branch (git push origin feature/amazing-feature)

  5. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

Support

Advanced Configuration

Using Environment Variables with Wrapper Scripts

For advanced use cases where you want to manage credentials centrally via environment variables, you can use wrapper scripts.

1. Create environment config file:

~/.profile (loaded for all sessions):

# MariaDB credentials for MCP
export MARIADB_LOCAL_USER="claude"
export MARIADB_LOCAL_PASS="your_password"
export MARIADB_LOCAL_DB="local_database"

export MARIADB_BASE7_USER="claude"
export MARIADB_BASE7_PASS="your_password"
export MARIADB_BASE7_DB="dh_scribe"

OR ~/.config/environment.d/mariadb.conf (systemd user session, for GUI apps):

MARIADB_LOCAL_USER=claude
MARIADB_LOCAL_PASS=your_password
MARIADB_LOCAL_DB=local_database

MARIADB_BASE7_USER=claude
MARIADB_BASE7_PASS=your_password
MARIADB_BASE7_DB=dh_scribe

2. Create wrapper scripts (examples in project repository):

  • mcp-mariadb-local.sh - loads MARIADB_LOCAL_* variables

  • mcp-mariadb-base7.sh - loads MARIADB_BASE7_* variables

3. Use wrapper scripts in ~/.claude.json:

{
  "mcpServers": {
    "mariadb-local": {
      "command": "/home/user/mcp-servers/mariadb/mcp-mariadb-local.sh",
      "args": []
    },
    "mariadb-base7": {
      "command": "/home/user/mcp-servers/mariadb/mcp-mariadb-base7.sh",
      "args": []
    }
  }
}

Benefits:

  • Centralized credential management

  • Credentials outside config files

  • Reusable across multiple tools

When to use:

  • Multiple projects sharing same database credentials

  • Corporate environments with centralized credential management

  • When you want credentials in system environment variables

When NOT to use:

  • Simple single-user setup → just use ~/.claude.json directly

  • Credentials already secure in ~/.claude.json → no need for extra complexity

Changelog

See CHANGELOG.md for version history and changes.

Available Tools

6 tools
describe_tableC

Show the structure of a table

Inputs: table (required).

execute_sqlC

Execute a SQL query on the MariaDB database

Inputs: query (required).

get_current_databaseA

Get the name of the currently selected database

Inputs: no named parameters.

list_databasesA

List all available databases

Inputs: no named parameters.

list_tablesB

List all tables in the current database

Inputs: no named parameters.

switch_databaseA

Switch to a different database. Must be called before executing queries.

Inputs: database (required).

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

Loading tool schema changelog…

    TDQS

    A3.7/5.0

    Scored across 6 tools

    Disambiguation5/5

    Each tool targets a clearly distinct operation: executing queries, listing tables, describing structure, listing databases, switching context, and reading current context. The only surface overlap (list_tables vs list_databases) is resolved by their distinct resources. No two tools could be reasonably confused.

    Naming Consistency5/5

    All six tools follow a strict snake_case verb_noun pattern (execute_sql, list_tables, describe_table, list_databases, switch_database, get_current_database). The convention is uniform and predictable throughout.

    Tool Count5/5

    Six tools is well-scoped for a database interaction server, covering the essential query and inspection operations without redundancy. Each tool earns its place and nothing feels padded or missing.

    Completeness4/5

    execute_sql as a catch-all gives full CRUD/DML capability, and metadata tools cover discovery and schema inspection. Minor gaps exist around index/schema detail beyond describe_table and explicit transaction/session management, but these are workaroundable via raw SQL.

    Maintenance

    ActivityStale
    ResponsivenessNo recent community issues

    Related MCP Connectors

    Related MCP Servers

    • F
      license
      Not graded
      quality
      D
      maintenance
      Enables LLMs and agents to interact with relational databases (SQL Server, MySQL, PostgreSQL) through MCP tools. Supports executing queries, inserting records, listing tables, and exposing database schemas with secure credential management.
      -
    • A
      license
      Not graded
      quality
      D
      maintenance
      Enables Claude Code and other MCP clients to interact with Microsoft SQL Server databases through standardized tools for query execution, schema exploration, table management, and stored procedure execution.
      1
      ISC
    • A
      license
      Not graded
      quality
      B
      maintenance
      Enables Claude Code to securely interact with PostgreSQL, MySQL, SQLite, and SQL Server databases, featuring read-only mode, query validation, SSH tunneling, and field redaction for production-safe data access.
      4
      MIT
    • A
      license
      Not graded
      quality
      B
      maintenance
      Enables Claude and other MCP clients to map a SQLite database's schema and run read-only SQL queries against it, with writes blocked at the SQLite level through multiple enforcement layers. Results come back as context-friendly Markdown tables with row caps and a query timeout.
      2
      MIT