@zilosoft/mcp-server-mysql 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,859 @@
1
+ # MCP Server for MySQL - Security-Hardened Fork
2
+
3
+ > **🔒 Security-hardened fork of [@benborla29/mcp-server-mysql](https://github.com/benborla/mcp-server-mysql)**
4
+ > **Package:** `@zilosoft/mcp-server-mysql`
5
+ > **Original Author:** [@benborla29](https://github.com/benborla)
6
+ > **Original Repository:** [https://github.com/benborla/mcp-server-mysql](https://github.com/benborla/mcp-server-mysql)
7
+ > **Fork Maintainer:** [@ZILosoft](https://github.com/ZILosoft)
8
+ > **License:** MIT
9
+
10
+ ## MCP Server for MySQL based on NodeJS
11
+
12
+ [![Trust Score](https://archestra.ai/mcp-catalog/api/badge/quality/benborla/mcp-server-mysql)](https://archestra.ai/mcp-catalog/benborla__mcp-server-mysql)
13
+
14
+ ### Security Fixes in This Fork
15
+
16
+ - 🔒 **Constant-time Bearer token comparison** — prevents timing attacks on the remote MCP secret
17
+ - 🔒 **Strict SSL validation** — at startup the server requires either a CA certificate (`MYSQL_SSL_CA`) or explicit opt-out (`MYSQL_SSL_SKIP_VERIFY=true`); no more silent MITM-vulnerable encryption
18
+ - 🔒 **SSL auto-detection** — remote hosts automatically get SSL enabled; localhost/sockets skip it
19
+ - 🔒 **AST-based schema extraction** — replaces regex-only parser; fixes permission bypass via cross-database references like `INSERT INTO forbidden_db.users`
20
+ - 🔒 **Removed `mysql_clear_password` plaintext auth plugin**
21
+ - 🔒 **Enforced multi-DB write lockdown** — in multi-DB mode, writes are truly disabled unless `MULTI_DB_WRITE_MODE=true`
22
+ - 🔒 **Zod input validation** on the `mysql_query` tool parameter
23
+ - 🔒 **Masked SSL file paths** in startup logs
24
+
25
+ ### Key Features (inherited from upstream)
26
+
27
+ - ✅ **Claude Code Integration** - Optimized for use with Anthropic's Claude Code CLI
28
+ - ✅ **SSH Tunnel Support** - Built-in support for SSH tunnels to remote databases
29
+ - ✅ **Auto-start/stop Hooks** - Automatic tunnel management with Claude start/stop
30
+ - ✅ **DDL Operations** - Added `MYSQL_DISABLE_READ_ONLY_TRANSACTIONS` for CREATE TABLE support
31
+ - ✅ **Multi-Project Setup** - Easy configuration for multiple projects with different databases
32
+
33
+ ### Quick Start for Claude Code Users
34
+
35
+ 1. **Read the Setup Guide**: See [PROJECT_SETUP_GUIDE.md](PROJECT_SETUP_GUIDE.md) for detailed instructions
36
+ 2. **Configure SSH Tunnels**: Set up automatic SSH tunnels for remote databases
37
+ 3. **Use with Claude**: Integrated MCP server works seamlessly with Claude Code
38
+
39
+ A Model Context Protocol server that provides access to MySQL databases through SSH tunnels. This server enables Claude and other LLMs to inspect database schemas and execute SQL queries securely.
40
+
41
+ ## Table of Contents
42
+
43
+ - [Requirements](#requirements)
44
+ - [Installation](#installation)
45
+ - [Smithery](#using-smithery)
46
+ - [Clone to Local Repository](#running-from-local-repository)
47
+ - [Remote mode](#run-in-remote-mode)
48
+ - [Components](#components)
49
+ - [Configuration](#configuration)
50
+ - [Environment Variables](#environment-variables)
51
+ - [Multi-DB Mode](#multi-db-mode)
52
+ - [Schema-Specific Permissions](#schema-specific-permissions)
53
+ - [Testing](#testing)
54
+ - [Troubleshooting](#troubleshooting)
55
+ - [Contributing](#contributing)
56
+ - [License](#license)
57
+
58
+ ## Requirements
59
+
60
+ - Node.js v20 or higher
61
+ - MySQL 5.7 or higher (MySQL 8.0+ recommended)
62
+ - MySQL user with appropriate permissions for the operations you need
63
+ - For write operations: MySQL user with INSERT, UPDATE, and/or DELETE privileges
64
+
65
+ ## Installation
66
+
67
+ > **Note:** This security-hardened fork is published as `@zilosoft/mcp-server-mysql`.
68
+ > The Smithery and Cursor one-click installs below point to the original upstream
69
+ > `@benborla29/mcp-server-mysql` package, which does NOT include the security fixes
70
+ > from this fork. To get the hardened build, install `@zilosoft/mcp-server-mysql`
71
+ > via npm/pnpm as shown in the other sections.
72
+
73
+ ### Using Smithery
74
+
75
+ > ⚠️ **Upstream only — this Smithery install does NOT include the security fixes from this fork.**
76
+ > To get the hardened build, install `@zilosoft/mcp-server-mysql` via npm/pnpm as shown in the sections below.
77
+
78
+ There are several ways to install and configure the MCP server but the most common would be checking this website [https://smithery.ai/server/@benborla29/mcp-server-mysql](https://smithery.ai/server/@benborla29/mcp-server-mysql)
79
+
80
+ ### Cursor
81
+
82
+ > ⚠️ **Upstream only — this Cursor install does NOT include the security fixes from this fork.**
83
+
84
+ For Cursor IDE, you can install this MCP server with the following command in your project:
85
+
86
+ 1. Visit [https://smithery.ai/server/@benborla29/mcp-server-mysql](https://smithery.ai/server/@benborla29/mcp-server-mysql)
87
+ 2. Follow the instruction for Cursor
88
+
89
+ MCP Get provides a centralized registry of MCP servers and simplifies the installation process.
90
+
91
+ ### Codex CLI
92
+
93
+ Codex CLI installation is similar to Claude Code below
94
+
95
+ ```bash
96
+ codex mcp add mcp_server_mysql \
97
+ --env MYSQL_HOST="127.0.0.1" \
98
+ --env MYSQL_PORT="3306" \
99
+ --env MYSQL_USER="root" \
100
+ --env MYSQL_PASS="your_password" \
101
+ --env MYSQL_DB="your_database" \
102
+ --env ALLOW_INSERT_OPERATION="false" \
103
+ --env ALLOW_UPDATE_OPERATION="false" \
104
+ --env ALLOW_DELETE_OPERATION="false" \
105
+ -- npx -y @zilosoft/mcp-server-mysql
106
+ ```
107
+
108
+ ### Claude Code
109
+
110
+ #### Option 1: Import from Claude Desktop (Recommended if already configured)
111
+
112
+ If you already have this MCP server configured in Claude Desktop, you can import it automatically:
113
+
114
+ ```bash
115
+ claude mcp add-from-claude-desktop
116
+ ```
117
+
118
+ This will show an interactive dialog where you can select your `mcp_server_mysql` server to import with all existing configuration.
119
+
120
+ #### Option 2: Manual Configuration
121
+
122
+ **Using NPM/PNPM Global Installation:**
123
+
124
+ First, install the package globally:
125
+
126
+ ```bash
127
+ # Using npm
128
+ npm install -g @zilosoft/mcp-server-mysql
129
+
130
+ # Using pnpm
131
+ pnpm add -g @zilosoft/mcp-server-mysql
132
+ ```
133
+
134
+ Then add the server to Claude Code:
135
+
136
+ ```bash
137
+ claude mcp add mcp_server_mysql \
138
+ -e MYSQL_HOST="127.0.0.1" \
139
+ -e MYSQL_PORT="3306" \
140
+ -e MYSQL_USER="root" \
141
+ -e MYSQL_PASS="your_password" \
142
+ -e MYSQL_DB="your_database" \
143
+ -e ALLOW_INSERT_OPERATION="false" \
144
+ -e ALLOW_UPDATE_OPERATION="false" \
145
+ -e ALLOW_DELETE_OPERATION="false" \
146
+ -- npx @zilosoft/mcp-server-mysql
147
+ ```
148
+
149
+ **Using Local Repository (for development):**
150
+
151
+ If you're running from a cloned repository:
152
+
153
+ ```bash
154
+ claude mcp add mcp_server_mysql \
155
+ -e MYSQL_HOST="127.0.0.1" \
156
+ -e MYSQL_PORT="3306" \
157
+ -e MYSQL_USER="root" \
158
+ -e MYSQL_PASS="your_password" \
159
+ -e MYSQL_DB="your_database" \
160
+ -e ALLOW_INSERT_OPERATION="false" \
161
+ -e ALLOW_UPDATE_OPERATION="false" \
162
+ -e ALLOW_DELETE_OPERATION="false" \
163
+ -e PATH="/path/to/node/bin:/usr/bin:/bin" \
164
+ -e NODE_PATH="/path/to/node/lib/node_modules" \
165
+ -- /path/to/node /full/path/to/mcp-server-mysql/dist/index.js
166
+ ```
167
+
168
+ Replace:
169
+
170
+ - `/path/to/node` with your Node.js binary path (find with `which node`)
171
+ - `/full/path/to/mcp-server-mysql` with the full path to your cloned repository
172
+ - Update MySQL credentials to match your environment
173
+
174
+ **Using Unix Socket Connection:**
175
+
176
+ For local MySQL instances using Unix sockets:
177
+
178
+ ```bash
179
+ claude mcp add mcp_server_mysql \
180
+ -e MYSQL_SOCKET_PATH="/tmp/mysql.sock" \
181
+ -e MYSQL_USER="root" \
182
+ -e MYSQL_PASS="your_password" \
183
+ -e MYSQL_DB="your_database" \
184
+ -e ALLOW_INSERT_OPERATION="false" \
185
+ -e ALLOW_UPDATE_OPERATION="false" \
186
+ -e ALLOW_DELETE_OPERATION="false" \
187
+ -- npx @zilosoft/mcp-server-mysql
188
+ ```
189
+
190
+ #### Choosing the Right Scope
191
+
192
+ Consider which scope to use based on your needs:
193
+
194
+ ```bash
195
+ # Local scope (default) - only available in current project
196
+ claude mcp add mcp_server_mysql [options...]
197
+
198
+ # User scope - available across all your projects
199
+ claude mcp add mcp_server_mysql -s user [options...]
200
+
201
+ # Project scope - shared with team members via .mcp.json
202
+ claude mcp add mcp_server_mysql -s project [options...]
203
+ ```
204
+
205
+ For database servers with credentials, **local** or **user** scope is recommended to keep credentials private.
206
+
207
+ #### Verification
208
+
209
+ After adding the server, verify it's configured correctly:
210
+
211
+ ```bash
212
+ # List all configured servers
213
+ claude mcp list
214
+
215
+ # Get details for your MySQL server
216
+ claude mcp get mcp_server_mysql
217
+
218
+ # Check server status within Claude Code
219
+ /mcp
220
+ ```
221
+
222
+ #### Multi-Database Configuration
223
+
224
+ For multi-database mode, omit the `MYSQL_DB` environment variable:
225
+
226
+ ```bash
227
+ claude mcp add mcp_server_mysql_multi \
228
+ -e MYSQL_HOST="127.0.0.1" \
229
+ -e MYSQL_PORT="3306" \
230
+ -e MYSQL_USER="root" \
231
+ -e MYSQL_PASS="your_password" \
232
+ -e MULTI_DB_WRITE_MODE="false" \
233
+ -- npx @zilosoft/mcp-server-mysql
234
+ ```
235
+
236
+ #### Advanced Configuration
237
+
238
+ For advanced features, add additional environment variables:
239
+
240
+ ```bash
241
+ claude mcp add mcp_server_mysql \
242
+ -e MYSQL_HOST="127.0.0.1" \
243
+ -e MYSQL_PORT="3306" \
244
+ -e MYSQL_USER="root" \
245
+ -e MYSQL_PASS="your_password" \
246
+ -e MYSQL_DB="your_database" \
247
+ -e MYSQL_POOL_SIZE="10" \
248
+ -e MYSQL_QUERY_TIMEOUT="30000" \
249
+ -e MYSQL_CACHE_TTL="60000" \
250
+ -e MYSQL_RATE_LIMIT="100" \
251
+ -e MYSQL_SSL="true" \
252
+ -e ALLOW_INSERT_OPERATION="false" \
253
+ -e ALLOW_UPDATE_OPERATION="false" \
254
+ -e ALLOW_DELETE_OPERATION="false" \
255
+ -e MYSQL_ENABLE_LOGGING="true" \
256
+ -- npx @zilosoft/mcp-server-mysql
257
+ ```
258
+
259
+ #### Troubleshooting Claude Code Setup
260
+
261
+ 1. **Server Connection Issues**: Use `/mcp` command in Claude Code to check server status and authenticate if needed.
262
+
263
+ 2. **Path Issues**: If using a local repository, ensure Node.js paths are correctly set:
264
+
265
+ ```bash
266
+ # Find your Node.js path
267
+ which node
268
+
269
+ # For PATH environment variable
270
+ echo "$(which node)/../"
271
+
272
+ # For NODE_PATH environment variable
273
+ echo "$(which node)/../../lib/node_modules"
274
+ ```
275
+
276
+ 3. **Permission Errors**: Ensure your MySQL user has appropriate permissions for the operations you've enabled.
277
+
278
+ 4. **Server Not Starting**: Check Claude Code logs or run the server directly to debug:
279
+
280
+ ```bash
281
+ # Test the server directly
282
+ npx @zilosoft/mcp-server-mysql
283
+ ```
284
+
285
+ ### Using NPM/PNPM
286
+
287
+ For manual installation:
288
+
289
+ ```bash
290
+ # Using npm
291
+ npm install -g @zilosoft/mcp-server-mysql
292
+
293
+ # Using pnpm
294
+ pnpm add -g @zilosoft/mcp-server-mysql
295
+ ```
296
+
297
+ After manual installation, you'll need to configure your LLM application to use the MCP server (see Configuration section below).
298
+
299
+ ### Running from Local Repository
300
+
301
+ If you want to clone and run this MCP server directly from the source code, follow these steps:
302
+
303
+ 1. **Clone the repository**
304
+
305
+ ```bash
306
+ git clone https://github.com/benborla/mcp-server-mysql.git
307
+ cd mcp-server-mysql
308
+ ```
309
+
310
+ 2. **Install dependencies**
311
+
312
+ ```bash
313
+ npm install
314
+ # or
315
+ pnpm install
316
+ ```
317
+
318
+ 3. **Build the project**
319
+
320
+ ```bash
321
+ npm run build
322
+ # or
323
+ pnpm run build
324
+ ```
325
+
326
+ 4. **Configure Claude Desktop**
327
+
328
+ Add the following to your Claude Desktop configuration file (`claude_desktop_config.json`):
329
+
330
+ ```json
331
+ {
332
+ "mcpServers": {
333
+ "mcp_server_mysql": {
334
+ "command": "/path/to/node",
335
+ "args": [
336
+ "/full/path/to/mcp-server-mysql/dist/index.js"
337
+ ],
338
+ "env": {
339
+ "MYSQL_HOST": "127.0.0.1",
340
+ "MYSQL_PORT": "3306",
341
+ "MYSQL_USER": "root",
342
+ "MYSQL_PASS": "your_password",
343
+ "MYSQL_DB": "your_database",
344
+ "ALLOW_INSERT_OPERATION": "false",
345
+ "ALLOW_UPDATE_OPERATION": "false",
346
+ "ALLOW_DELETE_OPERATION": "false",
347
+ "PATH": "/path/to/node/bin:/usr/bin:/bin", // <--- Important to add the following, run in your terminal `echo "$(which node)/../"` to get the path
348
+ "NODE_PATH": "/path/to/node/lib/node_modules" // <--- Important to add the following, run in your terminal `echo "$(which node)/../../lib/node_modules"`
349
+ }
350
+ }
351
+ }
352
+ }
353
+ ```
354
+
355
+ Replace:
356
+ - `/path/to/node` with the full path to your Node.js binary (find it with `which node`)
357
+ - `/full/path/to/mcp-server-mysql` with the full path to where you cloned the repository
358
+ - Set the MySQL credentials to match your environment
359
+
360
+ 5. **Test the server**
361
+
362
+ ```bash
363
+ # Run the server directly to test
364
+ node dist/index.js
365
+ ```
366
+
367
+ If it connects to MySQL successfully, you're ready to use it with Claude Desktop.
368
+
369
+ ### Run in remote mode
370
+
371
+ To run in remote mode, you'll need to provide [environment variables](https://github.com/benborla/mcp-server-mysql?tab=readme-ov-file#environment-variables) to the npx script.
372
+
373
+ 1. Create env file in preferred directory
374
+
375
+ ```bash
376
+ # create .env file
377
+ touch .env
378
+ ```
379
+
380
+ 2. Copy-paste [example file](https://github.com/benborla/mcp-server-mysql/blob/main/.env) from this repository
381
+ 3. Set the MySQL credentials to match your environment
382
+ 4. Set `IS_REMOTE_MCP=true`
383
+ 5. Set `REMOTE_SECRET_KEY` to a secure string.
384
+ 6. Provide custom `PORT` if needed. Default is 3000.
385
+ 7. Load variables in current session:
386
+
387
+ ```bash
388
+ source .env
389
+ ```
390
+
391
+ 8. Run the server
392
+
393
+ ```bash
394
+ npx @zilosoft/mcp-server-mysql
395
+ ```
396
+
397
+ 9. Configure your agent to connect to the MCP with the next configuration:
398
+
399
+ ```json
400
+ {
401
+ "mcpServers": {
402
+ "mysql": {
403
+ "url": "http://your-host:3000/mcp",
404
+ "type": "streamableHttp",
405
+ "headers": {
406
+ "Authorization": "Bearer <REMOTE_SECRET_KEY>"
407
+ }
408
+ }
409
+ }
410
+ }
411
+ ```
412
+
413
+ ## Components
414
+
415
+ ### Tools
416
+
417
+ - **mysql_query**
418
+ - Execute SQL queries against the connected database
419
+ - Input: `sql` (string): The SQL query to execute
420
+ - By default, limited to READ ONLY operations
421
+ - Optional write operations (when enabled via configuration):
422
+ - INSERT: Add new data to tables (requires `ALLOW_INSERT_OPERATION=true`)
423
+ - UPDATE: Modify existing data (requires `ALLOW_UPDATE_OPERATION=true`)
424
+ - DELETE: Remove data (requires `ALLOW_DELETE_OPERATION=true`)
425
+ - All operations are executed within a transaction with proper commit/rollback handling
426
+ - Supports prepared statements for secure parameter handling
427
+ - Configurable query timeouts and result pagination
428
+ - Built-in query execution statistics
429
+
430
+ ### Resources
431
+
432
+ The server provides comprehensive database information:
433
+
434
+ - **Table Schemas**
435
+ - JSON schema information for each table
436
+ - Column names and data types
437
+ - Index information and constraints
438
+ - Foreign key relationships
439
+ - Table statistics and metrics
440
+ - Automatically discovered from database metadata
441
+
442
+ ### Security Features
443
+
444
+ - SQL injection prevention through prepared statements
445
+ - Query whitelisting/blacklisting capabilities
446
+ - Rate limiting for query execution
447
+ - Query complexity analysis
448
+ - Configurable connection encryption
449
+ - Read-only transaction enforcement
450
+
451
+ ### Performance Optimizations
452
+
453
+ - Optimized connection pooling
454
+ - Query result caching
455
+ - Large result set streaming
456
+ - Query execution plan analysis
457
+ - Configurable query timeouts
458
+
459
+ ### Monitoring and Debugging
460
+
461
+ - Comprehensive query logging
462
+ - Performance metrics collection
463
+ - Error tracking and reporting
464
+ - Health check endpoints
465
+ - Query execution statistics
466
+
467
+ ## Configuration
468
+
469
+ ### Automatic Configuration with Smithery
470
+
471
+ If you installed using Smithery, your configuration is already set up. You can view or modify it with:
472
+
473
+ ```bash
474
+ smithery configure @benborla29/mcp-server-mysql
475
+ ```
476
+
477
+ When reconfiguring, you can update any of the MySQL connection details as well as the write operation settings:
478
+
479
+ - **Basic connection settings**:
480
+ - MySQL Host, Port, User, Password, Database
481
+ - SSL/TLS configuration (if your database requires secure connections)
482
+
483
+ - **Write operation permissions**:
484
+ - Allow INSERT Operations: Set to true if you want to allow adding new data
485
+ - Allow UPDATE Operations: Set to true if you want to allow updating existing data
486
+ - Allow DELETE Operations: Set to true if you want to allow deleting data
487
+
488
+ For security reasons, all write operations are disabled by default. Only enable these settings if you specifically need Claude to modify your database data.
489
+
490
+ ### Advanced Configuration Options
491
+
492
+ For more control over the MCP server's behavior, you can use these advanced configuration options:
493
+
494
+ ```json
495
+ {
496
+ "mcpServers": {
497
+ "mcp_server_mysql": {
498
+ "command": "/path/to/npx/binary/npx",
499
+ "args": [
500
+ "-y",
501
+ "@zilosoft/mcp-server-mysql"
502
+ ],
503
+ "env": {
504
+ // Basic connection settings
505
+ "MYSQL_HOST": "127.0.0.1",
506
+ "MYSQL_PORT": "3306",
507
+ "MYSQL_USER": "root",
508
+ "MYSQL_PASS": "",
509
+ "MYSQL_DB": "db_name",
510
+ "PATH": "/path/to/node/bin:/usr/bin:/bin",
511
+
512
+ // Performance settings
513
+ "MYSQL_POOL_SIZE": "10",
514
+ "MYSQL_QUERY_TIMEOUT": "30000",
515
+ "MYSQL_CACHE_TTL": "60000",
516
+
517
+ // Security settings
518
+ "MYSQL_RATE_LIMIT": "100",
519
+ "MYSQL_MAX_QUERY_COMPLEXITY": "1000",
520
+ "MYSQL_SSL": "true",
521
+
522
+ // Monitoring settings
523
+ "ENABLE_LOGGING": "true",
524
+ "MYSQL_LOG_LEVEL": "info",
525
+ "MYSQL_METRICS_ENABLED": "true",
526
+
527
+ // Write operation flags
528
+ "ALLOW_INSERT_OPERATION": "false",
529
+ "ALLOW_UPDATE_OPERATION": "false",
530
+ "ALLOW_DELETE_OPERATION": "false"
531
+ }
532
+ }
533
+ }
534
+ }
535
+ ```
536
+
537
+ ## Environment Variables
538
+
539
+ ### Basic Connection
540
+
541
+ - `MYSQL_SOCKET_PATH`: Unix socket path for local connections (e.g., "/tmp/mysql.sock")
542
+ - `MYSQL_HOST`: MySQL server host (default: "127.0.0.1") - ignored if MYSQL_SOCKET_PATH is set
543
+ - `MYSQL_PORT`: MySQL server port (default: "3306") - ignored if MYSQL_SOCKET_PATH is set
544
+ - `MYSQL_USER`: MySQL username (default: "root")
545
+ - `MYSQL_PASS`: MySQL password
546
+ - `MYSQL_DB`: Target database name (leave empty for multi-DB mode)
547
+
548
+ #### Alternative: Connection String
549
+
550
+ For scenarios requiring frequent credential rotation or temporary connections, you can use a MySQL connection string instead of individual environment variables:
551
+
552
+ - `MYSQL_CONNECTION_STRING`: MySQL CLI-format connection string (e.g., `mysql --default-auth=mysql_native_password -A -hHOST -PPORT -uUSER -pPASS database_name`)
553
+
554
+ When `MYSQL_CONNECTION_STRING` is provided, it takes precedence over individual connection settings. This is particularly useful for:
555
+
556
+ - Rotating credentials that expire frequently
557
+ - Temporary database connections
558
+ - Quick testing with different database configurations
559
+
560
+ **Note:** For security, this should only be set via environment variables, not stored in version-controlled configuration files. Consider using the `prompt` input type in Claude Code's MCP configuration for credentials that expire.
561
+
562
+ ### Performance Configuration
563
+
564
+ - `MYSQL_POOL_SIZE`: Connection pool size (default: "10")
565
+ - `MYSQL_QUERY_TIMEOUT`: Query timeout in milliseconds (default: "30000")
566
+ - `MYSQL_CACHE_TTL`: Cache time-to-live in milliseconds (default: "60000")
567
+ - `MYSQL_QUEUE_LIMIT`: Maximum number of queued connection requests (default: "100")
568
+ - `MYSQL_CONNECT_TIMEOUT`: Connection timeout in milliseconds (default: "10000")
569
+
570
+ ### Security Configuration
571
+
572
+ - `MYSQL_RATE_LIMIT`: Maximum queries per minute (default: "100")
573
+ - `MYSQL_MAX_QUERY_COMPLEXITY`: Maximum query complexity score (default: "1000")
574
+ - `MYSQL_SSL`: Enable SSL/TLS encryption (default: "false")
575
+ - `MYSQL_SSL_CA`: Path to SSL CA certificate file (PEM format). Only used when `MYSQL_SSL=true`. Required for connecting to MySQL instances with self-signed certificates or custom CAs.
576
+ - `MYSQL_SSL_CERT`: Path to the client certificate file (PEM format) for mTLS. Only used when `MYSQL_SSL=true`. Enables mutual TLS (mTLS) authentication, where both the server and client present certificates. Required by some database configurations that enforce client certificate verification.
577
+ - `MYSQL_SSL_KEY`: Path to the client private key file (PEM format) for mTLS. Only used when `MYSQL_SSL=true`. Must correspond to the certificate specified by `MYSQL_SSL_CERT`.
578
+ - `ALLOW_INSERT_OPERATION`: Enable INSERT operations (default: "false")
579
+ - `ALLOW_UPDATE_OPERATION`: Enable UPDATE operations (default: "false")
580
+ - `ALLOW_DELETE_OPERATION`: Enable DELETE operations (default: "false")
581
+ - `ALLOW_DDL_OPERATION`: Enable DDL operations (default: "false")
582
+ - `MYSQL_DISABLE_READ_ONLY_TRANSACTIONS`: **[NEW]** Disable read-only transaction enforcement (default: "false") ⚠️ **Security Warning:** Only enable this if you need full write capabilities and trust the LLM with your database
583
+ - `SCHEMA_INSERT_PERMISSIONS`: Schema-specific INSERT permissions
584
+ - `SCHEMA_UPDATE_PERMISSIONS`: Schema-specific UPDATE permissions
585
+ - `SCHEMA_DELETE_PERMISSIONS`: Schema-specific DELETE permissions
586
+ - `SCHEMA_DDL_PERMISSIONS`: Schema-specific DDL permissions
587
+ - `MULTI_DB_WRITE_MODE`: Enable write operations in multi-DB mode (default: "false")
588
+
589
+ ### Timezone and Date Configuration
590
+
591
+ - `MYSQL_TIMEZONE`: Set the timezone for date/time values. Accepts formats like `+08:00` (UTC+8), `-05:00` (UTC-5), `Z` (UTC), or `local` (system timezone). Useful for ensuring consistent date/time handling across different server locations.
592
+ - `MYSQL_DATE_STRINGS`: When set to `"true"`, returns date/datetime values as strings instead of JavaScript Date objects. This preserves the exact database values without any timezone conversion, which is particularly useful for:
593
+ - Applications that need precise control over date formatting
594
+ - Cross-timezone database operations
595
+ - Avoiding JavaScript Date timezone quirks
596
+
597
+ ### Monitoring Configuration
598
+
599
+ - `MYSQL_ENABLE_LOGGING`: Enable query logging (default: "false")
600
+ - `MYSQL_LOG_LEVEL`: Logging level (default: "info")
601
+ - `MYSQL_METRICS_ENABLED`: Enable performance metrics (default: "false")
602
+
603
+ ### Remote MCP Configuration
604
+
605
+ - `IS_REMOTE_MCP`: Enable remote MCP mode (default: "false")
606
+ - `REMOTE_SECRET_KEY`: Secret key for remote MCP authentication (default: ""). If not provided, remote MCP mode will be disabled.
607
+ - `PORT`: Port number for the remote MCP server (default: 3000)
608
+
609
+ ## Multi-DB Mode
610
+
611
+ MCP-Server-MySQL supports connecting to multiple databases when no specific database is set. This allows the LLM to query any database the MySQL user has access to. For full details, see [README-MULTI-DB.md](./README-MULTI-DB.md).
612
+
613
+ ### Enabling Multi-DB Mode
614
+
615
+ To enable multi-DB mode, simply leave the `MYSQL_DB` environment variable empty. In multi-DB mode, queries require schema qualification:
616
+
617
+ ```sql
618
+ -- Use fully qualified table names
619
+ SELECT * FROM database_name.table_name;
620
+
621
+ -- Or use USE statements to switch between databases
622
+ USE database_name;
623
+ SELECT * FROM table_name;
624
+ ```
625
+
626
+ ## Schema-Specific Permissions
627
+
628
+ For fine-grained control over database operations, MCP-Server-MySQL now supports schema-specific permissions. This allows different databases to have different levels of access (read-only, read-write, etc.).
629
+
630
+ ### Configuration Example
631
+
632
+ ```txt
633
+ SCHEMA_INSERT_PERMISSIONS=development:true,test:true,production:false
634
+ SCHEMA_UPDATE_PERMISSIONS=development:true,test:true,production:false
635
+ SCHEMA_DELETE_PERMISSIONS=development:false,test:true,production:false
636
+ SCHEMA_DDL_PERMISSIONS=development:false,test:true,production:false
637
+ ```
638
+
639
+ For complete details and security recommendations, see [README-MULTI-DB.md](./README-MULTI-DB.md).
640
+
641
+ ## Testing
642
+
643
+ ### Database Setup
644
+
645
+ Before running tests, you need to set up the test database and seed it with test data:
646
+
647
+ 1. **Create Test Database and User**
648
+
649
+ ```sql
650
+ -- Connect as root and create test database
651
+ CREATE DATABASE IF NOT EXISTS mcp_test;
652
+
653
+ -- Create test user with appropriate permissions
654
+ CREATE USER IF NOT EXISTS 'mcp_test'@'localhost' IDENTIFIED BY 'mcp_test_password';
655
+ GRANT ALL PRIVILEGES ON mcp_test.* TO 'mcp_test'@'localhost';
656
+ FLUSH PRIVILEGES;
657
+ ```
658
+
659
+ 2. **Run Database Setup Script**
660
+
661
+ ```bash
662
+ # Run the database setup script
663
+ pnpm run setup:test:db
664
+ ```
665
+
666
+ This will create the necessary tables and seed data. The script is located in `scripts/setup-test-db.ts`
667
+
668
+ 3. **Configure Test Environment**
669
+ Create a `.env.test` file in the project root (if not existing):
670
+
671
+ ```env
672
+ MYSQL_HOST=127.0.0.1
673
+ MYSQL_PORT=3306
674
+ MYSQL_USER=mcp_test
675
+ MYSQL_PASS=mcp_test_password
676
+ MYSQL_DB=mcp_test
677
+ ```
678
+
679
+ 4. **Update package.json Scripts**
680
+ Add these scripts to your package.json:
681
+
682
+ ```json
683
+ {
684
+ "scripts": {
685
+ "setup:test:db": "ts-node scripts/setup-test-db.ts",
686
+ "pretest": "pnpm run setup:test:db",
687
+ "test": "vitest run",
688
+ "test:watch": "vitest",
689
+ "test:coverage": "vitest run --coverage"
690
+ }
691
+ }
692
+ ```
693
+
694
+ ### Running Tests
695
+
696
+ The project includes a comprehensive test suite to ensure functionality and reliability:
697
+
698
+ ```bash
699
+ # First-time setup
700
+ pnpm run setup:test:db
701
+
702
+ # Run all tests
703
+ pnpm test
704
+ ```
705
+
706
+ ## Running evals
707
+
708
+ The evals package loads an mcp client that then runs the index.ts file, so there is no need to rebuild between tests. You can load environment variables by prefixing the npx command. Full documentation can be found at [MCP Evals](https://www.mcpevals.io/docs).
709
+
710
+ ```bash
711
+ OPENAI_API_KEY=your-key npx mcp-eval evals.ts index.ts
712
+ ```
713
+
714
+ ## Troubleshooting
715
+
716
+ ### Common Issues
717
+
718
+ 1. **Connection Issues**
719
+ - Verify MySQL server is running and accessible
720
+ - Check credentials and permissions
721
+ - Ensure SSL/TLS configuration is correct if enabled
722
+ - Try connecting with a MySQL client to confirm access
723
+
724
+ 2. **Performance Issues**
725
+ - Adjust connection pool size
726
+ - Configure query timeout values
727
+ - Enable query caching if needed
728
+ - Check query complexity settings
729
+ - Monitor server resource usage
730
+
731
+ 3. **Security Restrictions**
732
+ - Review rate limiting configuration
733
+ - Check query whitelist/blacklist settings
734
+ - Verify SSL/TLS settings
735
+ - Ensure the user has appropriate MySQL permissions
736
+
737
+ 4. **Path Resolution**
738
+ If you encounter an error "Could not connect to MCP server mcp-server-mysql", explicitly set the path of all required binaries:
739
+
740
+ ```json
741
+ {
742
+ "env": {
743
+ "PATH": "/path/to/node/bin:/usr/bin:/bin"
744
+ }
745
+ }
746
+ ```
747
+
748
+ *Where can I find my `node` bin path*
749
+ Run the following command to get it:
750
+
751
+ For **PATH**
752
+
753
+ ```bash
754
+ echo "$(which node)/../"
755
+ ```
756
+
757
+ For **NODE_PATH**
758
+
759
+ ```bash
760
+ echo "$(which node)/../../lib/node_modules"
761
+ ```
762
+
763
+ 5. **Claude Desktop Specific Issues**
764
+ - If you see "Server disconnected" logs in Claude Desktop, check the logs at `~/Library/Logs/Claude/mcp-server-mcp_server_mysql.log`
765
+ - Ensure you're using the absolute path to both the Node binary and the server script
766
+ - Check if your `.env` file is being properly loaded; use explicit environment variables in the configuration
767
+ - Try running the server directly from the command line to see if there are connection issues
768
+ - If you need write operations (INSERT, UPDATE, DELETE), set the appropriate flags to "true" in your configuration:
769
+
770
+ ```json
771
+ "env": {
772
+ "ALLOW_INSERT_OPERATION": "true", // Enable INSERT operations
773
+ "ALLOW_UPDATE_OPERATION": "true", // Enable UPDATE operations
774
+ "ALLOW_DELETE_OPERATION": "true" // Enable DELETE operations
775
+ }
776
+ ```
777
+
778
+ - Ensure your MySQL user has the appropriate permissions for the operations you're enabling
779
+ - For direct execution configuration, use:
780
+
781
+ ```json
782
+ {
783
+ "mcpServers": {
784
+ "mcp_server_mysql": {
785
+ "command": "/full/path/to/node",
786
+ "args": [
787
+ "/full/path/to/mcp-server-mysql/dist/index.js"
788
+ ],
789
+ "env": {
790
+ "MYSQL_HOST": "127.0.0.1",
791
+ "MYSQL_PORT": "3306",
792
+ "MYSQL_USER": "root",
793
+ "MYSQL_PASS": "your_password",
794
+ "MYSQL_DB": "your_database"
795
+ }
796
+ }
797
+ }
798
+ }
799
+ ```
800
+
801
+ 6. **Authentication Issues**
802
+ - For MySQL 8.0+, ensure the server supports the `caching_sha2_password` authentication plugin
803
+ - Check if your MySQL user is configured with the correct authentication method
804
+ - Try creating a user with legacy authentication if needed:
805
+
806
+ ```sql
807
+ CREATE USER 'user'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
808
+ ```
809
+
810
+ @lizhuangs
811
+
812
+ 7. I am encountering `Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'dotenv' imported from` error
813
+ try this workaround:
814
+
815
+ ```bash
816
+ npx -y -p @zilosoft/mcp-server-mysql -p dotenv mcp-server-mysql
817
+ ```
818
+
819
+ Thanks to @lizhuangs
820
+
821
+ ## Contributing
822
+
823
+ Contributions are welcome! Please feel free to submit a Pull Request to
824
+ [https://github.com/benborla/mcp-server-mysql](https://github.com/benborla/mcp-server-mysql)
825
+
826
+ ## Many Thanks to the following Contributors
827
+
828
+ [![Contributors](https://contrib.rocks/image?repo=benborla/mcp-server-mysql)](https://github.com/benborla/mcp-server-mysql/graphs/contributors)
829
+
830
+ ### Development Setup
831
+
832
+ 1. Clone the repository
833
+ 2. Install dependencies: `pnpm install`
834
+ 3. Build the project: `pnpm run build`
835
+ 4. Run tests: `pnpm test`
836
+
837
+ ### Project Roadmap
838
+
839
+ We're actively working on enhancing this MCP server. Check our [CHANGELOG.md](./CHANGELOG.md) for details on planned features, including:
840
+
841
+ - Enhanced query capabilities with prepared statements
842
+ - Advanced security features
843
+ - Performance optimizations
844
+ - Comprehensive monitoring
845
+ - Expanded schema information
846
+
847
+ If you'd like to contribute to any of these areas, please check the issues on GitHub or open a new one to discuss your ideas.
848
+
849
+ ### Submitting Changes
850
+
851
+ 1. Fork the repository
852
+ 2. Create a feature branch: `git checkout -b feature/your-feature-name`
853
+ 3. Commit your changes: `git commit -am 'Add some feature'`
854
+ 4. Push to the branch: `git push origin feature/your-feature-name`
855
+ 5. Submit a pull request
856
+
857
+ ## License
858
+
859
+ This MCP server is licensed under the MIT License. See the LICENSE file for details.