mysql_connect
Establish a connection to a MySQL database by providing host, user credentials, and optional database name to enable database operations through the MySQL MCP Server.
Instructions
连接到 MySQL 数据库
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| host | Yes | MySQL 服务器地址 | |
| port | No | MySQL 端口号 | |
| user | Yes | 用户名 | |
| password | Yes | 密码 | |
| database | No | 数据库名称(可选) |
Implementation Reference
- src/server.ts:40-54 (schema)Input schema definition for the mysql_connect tool, including parameters like host, port, user, password, and optional database.{ name: 'mysql_connect', description: '连接到 MySQL 数据库', inputSchema: { type: 'object', properties: { host: { type: 'string', description: 'MySQL 服务器地址' }, port: { type: 'number', description: 'MySQL 端口号', default: 3306 }, user: { type: 'string', description: '用户名' }, password: { type: 'string', description: '密码' }, database: { type: 'string', description: '数据库名称(可选)' }, }, required: ['host', 'user', 'password'], }, },
- src/server.ts:275-289 (handler)Handler method in MySQLMCPServer that validates config, calls DatabaseManager.connect, and returns success message.private async handleConnect(args: MySQLConfig): Promise<any> { const config = { ...getConfig(), ...args }; validateConfig(config); await this.dbManager.connect(config); return { content: [ { type: 'text', text: `成功连接到 MySQL 服务器: ${config.host}:${config.port}`, }, ], }; }
- src/database.ts:13-35 (handler)Core implementation in DatabaseManager that creates mysql2 connection pool using provided config, tests the connection with ping, and logs success.async connect(config: MySQLConfig): Promise<void> { try { this.config = config; this.pool = mysql.createPool({ host: config.host, port: config.port, user: config.user, password: config.password, database: config.database, connectionLimit: config.connectionLimit || 10, multipleStatements: true }); // 测试连接 const connection = await this.pool.getConnection(); await connection.ping(); connection.release(); console.log(`已成功连接到 MySQL 服务器: ${config.host}:${config.port}`); } catch (error) { throw new Error(`连接 MySQL 失败: ${error instanceof Error ? error.message : String(error)}`); } }
- src/server.ts:233-234 (registration)Switch case in CallToolRequestSchema handler that routes mysql_connect calls to handleConnect method.case 'mysql_connect': return await this.handleConnect(args as any);