fix(数据库安装): 改进MySQL数据库创建流程并添加错误处理
This commit is contained in:
+24
-13
@@ -1035,24 +1035,35 @@ async function installMySQLRHEL(rootPassword) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function createMySQLDatabase(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 {
|
try {
|
||||||
const connection = await mysql.createConnection({
|
execSync(`mysql -u root -p'${rootPassword}' -e "${createDbSql}"`, {
|
||||||
host: 'localhost',
|
shell: '/bin/bash',
|
||||||
port: 3306,
|
stdio: ['pipe', 'pipe', 'pipe']
|
||||||
user: 'root',
|
|
||||||
password: rootPassword
|
|
||||||
});
|
});
|
||||||
|
|
||||||
await connection.execute(
|
|
||||||
'CREATE DATABASE IF NOT EXISTS idc_management CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci'
|
|
||||||
);
|
|
||||||
|
|
||||||
await connection.end();
|
|
||||||
log.success('数据库 idc_management 创建成功');
|
log.success('数据库 idc_management 创建成功');
|
||||||
|
return;
|
||||||
} catch (error) {
|
} 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('请手动创建数据库:');
|
log.info('请手动创建数据库:');
|
||||||
console.log(colors.cyan + ' mysql -u root -p' + colors.reset);
|
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);
|
console.log(colors.cyan + ' CREATE DATABASE idc_management CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;' + colors.reset);
|
||||||
|
|||||||
Reference in New Issue
Block a user