|
| 1 | +# Multi-DB Mode and Schema-Specific Permissions |
| 2 | + |
| 3 | +This document describes the new multi-database mode and schema-specific permissions features added to the MCP-Server-MySQL. |
| 4 | + |
| 5 | +## Multi-DB Mode |
| 6 | + |
| 7 | +MCP-Server-MySQL now supports working with multiple databases simultaneously when no specific database is set in the configuration. |
| 8 | + |
| 9 | +### How to Enable Multi-DB Mode |
| 10 | + |
| 11 | +To enable multi-DB mode, simply leave the `MYSQL_DB` environment variable empty: |
| 12 | + |
| 13 | +```json |
| 14 | +{ |
| 15 | + "mcpServers": { |
| 16 | + "mcp_server_mysql": { |
| 17 | + "env": { |
| 18 | + "MYSQL_HOST": "127.0.0.1", |
| 19 | + "MYSQL_PORT": "3306", |
| 20 | + "MYSQL_USER": "root", |
| 21 | + "MYSQL_PASS": "your_password", |
| 22 | + "MYSQL_DB": "", // Empty to enable multi-DB mode |
| 23 | + ... |
| 24 | + } |
| 25 | + } |
| 26 | + } |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +### Features in Multi-DB Mode |
| 31 | + |
| 32 | +1. **List All Databases**: In multi-DB mode, the server will list resources from all available databases when the LLM requests database schemas. |
| 33 | + |
| 34 | +2. **Query Any Database**: You can execute queries against any database to which the MySQL user has access. |
| 35 | + |
| 36 | +3. **Schema Qualification Required**: When working in multi-DB mode, you should use fully qualified table names with schema/database prefixes: |
| 37 | + ```sql |
| 38 | + -- Use fully qualified table names |
| 39 | + SELECT * FROM database_name.table_name; |
| 40 | + |
| 41 | + -- Or use USE statements to switch between databases |
| 42 | + USE database_name; |
| 43 | + SELECT * FROM table_name; |
| 44 | + ``` |
| 45 | + |
| 46 | +4. **Automatic Read-Only Mode**: For safety, multi-DB mode enforces read-only operations by default. This can be customized using schema-specific permissions (see below). |
| 47 | + |
| 48 | +5. **Database Exploration**: You can explore databases using commands like: |
| 49 | + ```sql |
| 50 | + -- List all databases |
| 51 | + SHOW DATABASES; |
| 52 | + |
| 53 | + -- List tables in a specific database |
| 54 | + SHOW TABLES FROM database_name; |
| 55 | + |
| 56 | + -- Describe a table's structure |
| 57 | + DESCRIBE database_name.table_name; |
| 58 | + ``` |
| 59 | + |
| 60 | +## Schema-Specific Permissions |
| 61 | + |
| 62 | +This new feature allows fine-grained control over which operations are allowed on specific database schemas. |
| 63 | + |
| 64 | +### Available Permission Types |
| 65 | + |
| 66 | +1. **INSERT Permissions**: Control which schemas can have new records inserted. |
| 67 | +2. **UPDATE Permissions**: Control which schemas can have records updated. |
| 68 | +3. **DELETE Permissions**: Control which schemas can have records deleted. |
| 69 | +4. **DDL Permissions**: Control which schemas can have their structure modified (CREATE, ALTER, DROP, TRUNCATE). |
| 70 | + |
| 71 | +### How to Configure Schema-Specific Permissions |
| 72 | + |
| 73 | +Set the following environment variables with a comma-separated list of schema:permission pairs: |
| 74 | + |
| 75 | +``` |
| 76 | +SCHEMA_INSERT_PERMISSIONS=production:false,development:true,test:true |
| 77 | +SCHEMA_UPDATE_PERMISSIONS=production:false,development:true,test:true |
| 78 | +SCHEMA_DELETE_PERMISSIONS=production:false,development:false,test:true |
| 79 | +SCHEMA_DDL_PERMISSIONS=production:false,development:false,test:true |
| 80 | +``` |
| 81 | + |
| 82 | +This configuration: |
| 83 | +- Allows INSERT and UPDATE on development and test databases, but not production |
| 84 | +- Allows DELETE and DDL operations only on the test database |
| 85 | +- Blocks all write operations on the production database |
| 86 | + |
| 87 | +### Example Configuration |
| 88 | + |
| 89 | +Here's a complete example configuration with schema-specific permissions: |
| 90 | + |
| 91 | +```json |
| 92 | +{ |
| 93 | + "mcpServers": { |
| 94 | + "mcp_server_mysql": { |
| 95 | + "command": "npx", |
| 96 | + "args": ["-y", "@benborla29/mcp-server-mysql"], |
| 97 | + "env": { |
| 98 | + "MYSQL_HOST": "127.0.0.1", |
| 99 | + "MYSQL_PORT": "3306", |
| 100 | + "MYSQL_USER": "root", |
| 101 | + "MYSQL_PASS": "your_password", |
| 102 | + "MYSQL_DB": "", // Empty for multi-DB mode |
| 103 | + |
| 104 | + // Global defaults (apply when no schema-specific permission is set) |
| 105 | + "ALLOW_INSERT_OPERATION": "false", |
| 106 | + "ALLOW_UPDATE_OPERATION": "false", |
| 107 | + "ALLOW_DELETE_OPERATION": "false", |
| 108 | + "ALLOW_DDL_OPERATION": "false", |
| 109 | + |
| 110 | + // Schema-specific permissions |
| 111 | + "SCHEMA_INSERT_PERMISSIONS": "dev_db:true,test_db:true,prod_db:false", |
| 112 | + "SCHEMA_UPDATE_PERMISSIONS": "dev_db:true,test_db:true,prod_db:false", |
| 113 | + "SCHEMA_DELETE_PERMISSIONS": "dev_db:false,test_db:true,prod_db:false", |
| 114 | + "SCHEMA_DDL_PERMISSIONS": "dev_db:false,test_db:true,prod_db:false" |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +### Permission Resolution Logic |
| 122 | + |
| 123 | +1. If a schema-specific permission is set, it takes precedence over the global setting. |
| 124 | +2. If no schema-specific permission is found, the global setting (`ALLOW_X_OPERATION`) is used. |
| 125 | +3. In multi-DB mode, if a query doesn't specify a schema and one can't be determined from context, only read operations are allowed for safety. |
| 126 | + |
| 127 | +## Environment Variables Summary |
| 128 | + |
| 129 | +### Multi-DB Mode |
| 130 | +- `MYSQL_DB`: Leave empty to enable multi-DB mode |
| 131 | +- `MULTI_DB_WRITE_MODE`: Set to "true" to allow write operations in multi-DB mode without schema-specific permissions (not recommended for security) |
| 132 | + |
| 133 | +### Schema-Specific Permissions |
| 134 | +- `SCHEMA_INSERT_PERMISSIONS`: Control INSERT permissions per schema |
| 135 | +- `SCHEMA_UPDATE_PERMISSIONS`: Control UPDATE permissions per schema |
| 136 | +- `SCHEMA_DELETE_PERMISSIONS`: Control DELETE permissions per schema |
| 137 | +- `SCHEMA_DDL_PERMISSIONS`: Control DDL permissions per schema (CREATE, ALTER, DROP, TRUNCATE) |
| 138 | + |
| 139 | +### Global Permission Defaults |
| 140 | +- `ALLOW_INSERT_OPERATION`: Global default for INSERT permissions |
| 141 | +- `ALLOW_UPDATE_OPERATION`: Global default for UPDATE permissions |
| 142 | +- `ALLOW_DELETE_OPERATION`: Global default for DELETE permissions |
| 143 | +- `ALLOW_DDL_OPERATION`: Global default for DDL permissions |
| 144 | + |
| 145 | +## Security Considerations |
| 146 | + |
| 147 | +1. **Default to Principle of Least Privilege**: By default, all write operations are disabled globally and must be explicitly enabled. |
| 148 | + |
| 149 | +2. **Isolation in Multi-DB Mode**: Consider using a dedicated MySQL user with limited database grants when using multi-DB mode. |
| 150 | + |
| 151 | +3. **Careful with DDL Permissions**: DDL operations can modify database structure, so grant these permissions cautiously. |
| 152 | + |
| 153 | +4. **Production Databases**: Always set `schema:false` for production database schemas in all write permission settings. |
| 154 | + |
| 155 | +5. **User Least Privilege**: Ensure the MySQL user only has the required permissions on the specific databases needed. |
0 commit comments