如果在Egg.js中引入MySQL出現(xiàn)問題,可以按照以下步驟解決:
確保已經(jīng)安裝了MySQL數(shù)據(jù)庫,并且數(shù)據(jù)庫服務(wù)已經(jīng)啟動(dòng)。
在Egg.js項(xiàng)目中安裝相關(guān)的MySQL依賴包,可以使用以下命令安裝:
npm install mysql2 --save
config.mysql = {
client: {
host: 'localhost',
port: '3306',
user: 'root',
password: 'password',
database: 'dbname',
},
};
const mysql = require('mysql2/promise');
const config = require('../config/config.default');
async function queryDatabase() {
const connection = await mysql.createConnection(config.mysql.client);
const [rows, fields] = await connection.execute('SELECT * FROM table');
return rows;
}