AWS Lambda在执行完函数后会自动关闭与MySQL数据库的连接,因此无需手动关闭。以下是一个基本的示例,演示了如何在AWS Lambda中使用MySQL:
const mysql = require('mysql');
exports.handler = (event, context, callback) => {
const connection = mysql.createConnection({
host: 'your_mysql_host',
user: 'your_mysql_user',
password: 'your_mysql_password',
database: 'your_mysql_database'
});
connection.connect();
connection.query('SELECT * FROM your_table', (error, results, fields) => {
if (error) throw error;
console.log(results);
callback(null, 'Success');
});
// No need to manually close the connection,
// as AWS Lambda automatically closes it
};