fix(install): 改进 MySQL 连接测试的错误处理和诊断信息
重构 MySQL 连接测试逻辑,使用 execSync 替代 mysql2 库 添加更详细的错误诊断信息,包括用户名密码错误、数据库不存在和连接失败等场景
This commit is contained in:
+17
-12
@@ -1074,22 +1074,27 @@ async function testMySQLConnection() {
|
|||||||
log.subStep('测试 MySQL 连接...');
|
log.subStep('测试 MySQL 连接...');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const mysql = require('mysql2/promise');
|
execSync(`mysql -h ${config.dbConfig.host} -P ${config.dbConfig.port} -u ${config.dbConfig.username} -p'${config.dbConfig.password}' -e "SELECT 1" ${config.dbConfig.database}`, {
|
||||||
const connection = await mysql.createConnection({
|
shell: '/bin/bash',
|
||||||
host: config.dbConfig.host,
|
stdio: ['pipe', 'pipe', 'pipe'],
|
||||||
port: parseInt(config.dbConfig.port),
|
timeout: 5000
|
||||||
user: config.dbConfig.username,
|
|
||||||
password: config.dbConfig.password,
|
|
||||||
database: config.dbConfig.database,
|
|
||||||
connectTimeout: 5000
|
|
||||||
});
|
});
|
||||||
|
|
||||||
await connection.execute('SELECT 1');
|
|
||||||
await connection.end();
|
|
||||||
log.success('MySQL 连接测试成功');
|
log.success('MySQL 连接测试成功');
|
||||||
return { success: true };
|
return { success: true };
|
||||||
} catch (error) {
|
} 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() || '连接失败' };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user