137 lines
4.0 KiB
JavaScript
137 lines
4.0 KiB
JavaScript
const mysql = require('mysql2');
|
||
|
||
// 数据库配置
|
||
const dbConfig = {
|
||
host: '129.211.213.226',
|
||
port: 9527,
|
||
user: 'root',
|
||
password: 'aiotAiot123!',
|
||
database: 'jiebandata'
|
||
};
|
||
|
||
// 创建连接
|
||
const connection = mysql.createConnection(dbConfig);
|
||
|
||
// 连接数据库
|
||
connection.connect((err) => {
|
||
if (err) {
|
||
console.error('数据库连接失败: ' + err.stack);
|
||
return;
|
||
}
|
||
console.log('数据库连接成功,连接ID: ' + connection.threadId);
|
||
|
||
// 查询所有表
|
||
connection.query('SHOW TABLES', (error, results) => {
|
||
if (error) {
|
||
console.error('查询表失败: ' + error.stack);
|
||
connection.end();
|
||
return;
|
||
}
|
||
|
||
console.log('数据库中的所有表:');
|
||
const tables = results.map(row => {
|
||
const tableName = Object.values(row)[0];
|
||
console.log('- ' + tableName);
|
||
return tableName;
|
||
});
|
||
|
||
// 检查是否存在管理员表
|
||
const adminTableExists = tables.some(table =>
|
||
table.includes('admin') || table.includes('Admin') || table.includes('ADMIN')
|
||
);
|
||
|
||
if (adminTableExists) {
|
||
console.log('\n✅ 找到可能的管理员相关表');
|
||
|
||
// 显示管理员相关表结构
|
||
const adminTables = tables.filter(table =>
|
||
table.includes('admin') || table.includes('Admin') || table.includes('ADMIN')
|
||
);
|
||
|
||
showTableStructure(adminTables, 0);
|
||
} else {
|
||
console.log('\n❌ 未找到管理员相关表');
|
||
|
||
// 显示所有表的简要信息
|
||
showAllTablesInfo(tables, 0);
|
||
}
|
||
});
|
||
|
||
// 显示表结构的递归函数
|
||
function showTableStructure(tables, index) {
|
||
if (index >= tables.length) {
|
||
connection.end();
|
||
return;
|
||
}
|
||
|
||
const tableName = tables[index];
|
||
console.log(`\n${tableName} 表结构:`);
|
||
|
||
connection.query(`DESCRIBE ${tableName}`, (error, results) => {
|
||
if (error) {
|
||
console.error(`查询 ${tableName} 表结构失败: ` + error.stack);
|
||
} else {
|
||
console.table(results.map(row => ({
|
||
字段: row.Field,
|
||
类型: row.Type,
|
||
空: row.Null,
|
||
键: row.Key,
|
||
默认值: row.Default,
|
||
额外: row.Extra
|
||
})));
|
||
}
|
||
|
||
showTableStructure(tables, index + 1);
|
||
});
|
||
}
|
||
|
||
// 显示所有表信息的递归函数
|
||
function showAllTablesInfo(tables, index) {
|
||
if (index >= tables.length) {
|
||
// 创建管理员表的SQL
|
||
console.log('\n=== 建议创建的管理员表结构 ===');
|
||
const createAdminTableSQL = `
|
||
CREATE TABLE admins (
|
||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||
username VARCHAR(50) NOT NULL UNIQUE,
|
||
password VARCHAR(255) NOT NULL,
|
||
email VARCHAR(100) UNIQUE,
|
||
nickname VARCHAR(100),
|
||
avatar VARCHAR(255),
|
||
role ENUM('super_admin', 'admin', 'operator') DEFAULT 'admin',
|
||
status TINYINT DEFAULT 1,
|
||
last_login TIMESTAMP NULL,
|
||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
|
||
);`;
|
||
|
||
console.log(createAdminTableSQL);
|
||
|
||
// 插入测试数据的SQL
|
||
console.log('\n=== 插入测试数据 ===');
|
||
const insertTestDataSQL = `
|
||
INSERT INTO admins (username, password, email, nickname, role) VALUES
|
||
('admin', '$2b$10$rVuz/q97ocR1Zb07DzW5F.9Qx6B6HnV7JFzQb5nR1W3v7Z2mH4n6O', 'admin@example.com', '超级管理员', 'super_admin'),
|
||
('operator', '$2b$10$rVuz/q97ocR1Zb07DzW5F.9Qx6B6HnV7JFzQb5nR1W3v7Z2mH4n6O', 'operator@example.com', '操作员', 'operator');
|
||
`;
|
||
|
||
console.log(insertTestDataSQL);
|
||
|
||
connection.end();
|
||
return;
|
||
}
|
||
|
||
const tableName = tables[index];
|
||
console.log(`\n${tableName} 表信息:`);
|
||
|
||
connection.query(`SELECT COUNT(*) as count FROM ${tableName}`, (error, results) => {
|
||
if (error) {
|
||
console.error(`查询 ${tableName} 表数据量失败: ` + error.stack);
|
||
} else {
|
||
console.log(`数据量: ${results[0].count} 条`);
|
||
}
|
||
|
||
showAllTablesInfo(tables, index + 1);
|
||
});
|
||
}
|
||
}); |