老规矩:先安装依赖:mysql2
import cloud from '@lafjs/cloud'
export async function main(ctx: FunctionContext) {
// 导入模块
const mysql = require('mysql2');
// 创建一个数据库连接
const connection = mysql.createConnection({
host: 'rds6068hv519ys4c4vp9.mysql.rds.aliyuncs.com',
user: 'fscz_jbpcdgpsqxx',
password: "XfoCE2K%uhXtTMrUfhRBVksqC@A^UZ81",
database: 'r5s9l6g5xn'
});
// 简单查询
connection.execute(
'SELECT * FROM `ppre_migrations` LIMIT 20;',
function (err, results, fields) {
console.log(results); // 结果集 在日志查看
console.log(fields); // 额外的元数据(如果有的话)
}
);
}
剩下的直接参考mysql2官方文档修改