From 0be142d572d7e002d5955505d118bb6c47239722 Mon Sep 17 00:00:00 2001 From: zhang1106 <849185023@qq.com> Date: Tue, 17 Mar 2026 15:06:04 +0800 Subject: [PATCH] =?UTF-8?q?fix(install):=20=E6=94=B9=E8=BF=9B=20MySQL=20?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E6=B5=8B=E8=AF=95=E7=9A=84=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E5=A4=84=E7=90=86=E5=92=8C=E8=AF=8A=E6=96=AD=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 重构 MySQL 连接测试逻辑,使用 execSync 替代 mysql2 库 添加更详细的错误诊断信息,包括用户名密码错误、数据库不存在和连接失败等场景 --- install.js | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/install.js b/install.js index d42d817..0e9019d 100644 --- a/install.js +++ b/install.js @@ -1074,22 +1074,27 @@ async function testMySQLConnection() { log.subStep('测试 MySQL 连接...'); try { - const mysql = require('mysql2/promise'); - const connection = await mysql.createConnection({ - host: config.dbConfig.host, - port: parseInt(config.dbConfig.port), - user: config.dbConfig.username, - password: config.dbConfig.password, - database: config.dbConfig.database, - connectTimeout: 5000 + execSync(`mysql -h ${config.dbConfig.host} -P ${config.dbConfig.port} -u ${config.dbConfig.username} -p'${config.dbConfig.password}' -e "SELECT 1" ${config.dbConfig.database}`, { + shell: '/bin/bash', + stdio: ['pipe', 'pipe', 'pipe'], + timeout: 5000 }); - - await connection.execute('SELECT 1'); - await connection.end(); log.success('MySQL 连接测试成功'); return { success: true }; } catch (error) { - return { success: false, error: error.message }; + const errorMsg = error.stderr ? error.stderr.toString() : error.message; + + if (errorMsg.includes('Access denied') || errorMsg.includes('ERROR 1045')) { + return { success: false, error: '用户名或密码错误' }; + } + if (errorMsg.includes('Unknown database')) { + return { success: false, error: `数据库 ${config.dbConfig.database} 不存在` }; + } + if (errorMsg.includes('Connection refused') || errorMsg.includes("Can't connect")) { + return { success: false, error: `无法连接到 ${config.dbConfig.host}:${config.dbConfig.port}` }; + } + + return { success: false, error: errorMsg.trim() || '连接失败' }; } }