From 23ab97cfc257bd47ed8104d5be1d745cecee574f Mon Sep 17 00:00:00 2001 From: zhang1106 <849185023@qq.com> Date: Tue, 17 Mar 2026 14:57:51 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=95=B0=E6=8D=AE=E5=BA=93=E5=AE=89?= =?UTF-8?q?=E8=A3=85):=20=E6=94=B9=E8=BF=9BMySQL=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E5=88=9B=E5=BB=BA=E6=B5=81=E7=A8=8B=E5=B9=B6=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E9=94=99=E8=AF=AF=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- install.js | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/install.js b/install.js index 3d337d5..d42d817 100644 --- a/install.js +++ b/install.js @@ -1035,24 +1035,35 @@ async function installMySQLRHEL(rootPassword) { } async function createMySQLDatabase(rootPassword) { - const mysql = require('mysql2/promise'); + log.subStep('创建数据库...'); + + const createDbSql = 'CREATE DATABASE IF NOT EXISTS idc_management CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;'; try { - const connection = await mysql.createConnection({ - host: 'localhost', - port: 3306, - user: 'root', - password: rootPassword + execSync(`mysql -u root -p'${rootPassword}' -e "${createDbSql}"`, { + shell: '/bin/bash', + stdio: ['pipe', 'pipe', 'pipe'] }); - - await connection.execute( - 'CREATE DATABASE IF NOT EXISTS idc_management CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci' - ); - - await connection.end(); log.success('数据库 idc_management 创建成功'); + return; } catch (error) { - log.warning(`数据库创建失败: ${error.message}`); + const errorMsg = error.stderr ? error.stderr.toString() : error.message; + + if (errorMsg.includes('Access denied') || errorMsg.includes('ERROR 1045')) { + log.warning('MySQL root 密码验证失败,尝试无密码连接...'); + try { + execSync(`mysql -u root -e "${createDbSql}"`, { + shell: '/bin/bash', + stdio: ['pipe', 'pipe', 'pipe'] + }); + log.success('数据库 idc_management 创建成功'); + return; + } catch (e2) { + log.warning(`无密码连接也失败: ${e2.message}`); + } + } + + log.warning(`数据库自动创建失败: ${errorMsg.trim()}`); log.info('请手动创建数据库:'); console.log(colors.cyan + ' mysql -u root -p' + colors.reset); console.log(colors.cyan + ' CREATE DATABASE idc_management CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;' + colors.reset);