Skip to main content
Glama

MySQL Database Server

claude-code-setup.md5.87 kB
# Setting Up MCP MySQL Server with Claude Code This guide shows you how to integrate the MCP MySQL Server with Claude Code for AI-powered database interactions. ## Prerequisites - Node.js 18 or higher - MySQL database running - Claude Code installed ## Step 1: Install and Build ```bash cd mcp-mysql-server npm install npm run build ``` ## Step 2: Configure Environment Create a `.env` file in your project directory: ```env # MySQL Connection MYSQL_HOST=localhost MYSQL_PORT=3306 MYSQL_USER=your_username MYSQL_PASS=your_password MYSQL_DB=your_database # Safety Settings (recommended to start with read-only) ALLOW_INSERT_OPERATION=false ALLOW_UPDATE_OPERATION=false ALLOW_DELETE_OPERATION=false ``` ## Step 3: Add to Claude Code Configuration ### Option A: Using .mcp.json (Recommended) Create or update `.mcp.json` in your home directory or project root: ```json { "mcpServers": { "mysql": { "command": "node", "args": ["/absolute/path/to/mcp-mysql-server/dist/index.js"], "env": { "MYSQL_HOST": "localhost", "MYSQL_PORT": "3306", "MYSQL_USER": "your_username", "MYSQL_PASS": "your_password", "MYSQL_DB": "your_database", "ALLOW_INSERT_OPERATION": "false", "ALLOW_UPDATE_OPERATION": "false", "ALLOW_DELETE_OPERATION": "false" } } } } ``` ### Option B: Using Environment Variables If you prefer using a `.env` file, configure it like this: ```json { "mcpServers": { "mysql": { "command": "node", "args": ["/absolute/path/to/mcp-mysql-server/dist/index.js"] } } } ``` Then place your `.env` file in the same directory as the server. ## Step 4: Restart Claude Code After updating the configuration, restart Claude Code to load the MCP server. ## Step 5: Verify Connection In Claude Code, ask: ``` Can you show me information about the MySQL database connection? ``` Claude should use the `mysql_get_database_info` tool and display your connection details. ## Common Use Cases ### Explore Database Schema ``` What tables are in my database? ``` ``` Show me the structure of the users table ``` ### Query Data ``` Get the first 10 users from the database ``` ``` Find all orders where total is greater than $100 ``` ### Analyze Data ``` Can you analyze the sales data from the last month and give me insights? ``` ### Generate Reports ``` Create a summary report of user registrations by month ``` ## Safety Features in Action ### Read-Only Mode (Default) When `ALLOW_INSERT_OPERATION`, `ALLOW_UPDATE_OPERATION`, and `ALLOW_DELETE_OPERATION` are `false`: - **Allowed**: All SELECT queries, schema inspection, data analysis - **Blocked**: INSERT, UPDATE, DELETE operations - **Always Blocked**: DROP, TRUNCATE, ALTER operations ### Enabling Write Operations To enable specific write operations, update your configuration: ```env # Enable inserts only ALLOW_INSERT_OPERATION=true ALLOW_UPDATE_OPERATION=false ALLOW_DELETE_OPERATION=false ``` Then restart Claude Code. ## Best Practices 1. **Start Read-Only**: Begin with all write operations disabled 2. **Test Queries**: Ask Claude to explain queries before execution 3. **Use Transactions**: For multiple related changes, ask Claude to use transactions 4. **Backup First**: Always backup before enabling DELETE operations 5. **Parameterized Queries**: Claude will automatically use prepared statements ## Troubleshooting ### Server Not Showing Up 1. Check the path in `.mcp.json` is absolute and correct 2. Verify the build succeeded: `ls -la dist/index.js` 3. Check Claude Code's MCP server logs ### Connection Errors 1. Verify MySQL is running: `mysql -u username -p` 2. Test connection credentials 3. Check firewall settings 4. Ensure database exists ### Permission Denied Errors 1. Check MySQL user has required permissions 2. Verify environment variable names are correct 3. Ensure boolean values are `"true"` or `"false"` strings ## Advanced Configuration ### Multiple Databases You can configure multiple MySQL servers: ```json { "mcpServers": { "mysql-production": { "command": "node", "args": ["/path/to/mcp-mysql-server/dist/index.js"], "env": { "MYSQL_HOST": "prod-db.example.com", "MYSQL_DB": "production", "ALLOW_INSERT_OPERATION": "false" } }, "mysql-development": { "command": "node", "args": ["/path/to/mcp-mysql-server/dist/index.js"], "env": { "MYSQL_HOST": "localhost", "MYSQL_DB": "dev", "ALLOW_INSERT_OPERATION": "true", "ALLOW_UPDATE_OPERATION": "true" } } } } ``` ### Using Docker If your MySQL is in Docker: ```env MYSQL_HOST=127.0.0.1 MYSQL_PORT=3306 # or use Docker network name if Claude Code runs in Docker MYSQL_HOST=mysql-container ``` ## Example Workflows ### Data Analysis Workflow 1. **Explore**: "What tables exist in the database?" 2. **Understand**: "Show me the schema of the sales table" 3. **Query**: "Get sales data for the last quarter" 4. **Analyze**: "Calculate total revenue by product category" 5. **Visualize**: "Can you format this as a markdown table?" ### Database Maintenance Workflow 1. **Review**: "Show me all tables" 2. **Inspect**: "Which tables have the most rows?" 3. **Analyze**: "Find any duplicate records in the users table" 4. **Report**: "Generate a database health summary" ### Development Workflow 1. **Schema**: "What's the current schema?" 2. **Test Data**: "Show me sample data from each table" 3. **Validation**: "Check if there are any NULL values in required fields" 4. **Documentation**: "Document the database schema for me" ## Next Steps - Check out [examples/](.) for more usage examples - Read the [main README](../README.md) for detailed API documentation - See [CONTRIBUTING.md](../CONTRIBUTING.md) for development guidelines

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/thebusted/mcp-mysql-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server