Skip to main content
Glama
pickstar-2002

MySQL MCP Server

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
NameRequiredDescriptionDefault
hostYesMySQL 服务器地址
portNoMySQL 端口号
userYes用户名
passwordYes密码
databaseNo数据库名称(可选)

Implementation Reference

  • 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'], }, },
  • 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}`, }, ], }; }
  • 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);

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/pickstar-2002/mysql-mcp'

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