要将本地的 Node.js 服务器连接到运行在亚马逊网络服务(AWS)上的 MySQL 数据库,你需要遵循以下步骤:
mysql
模块。mysql
模块。const fs = require('fs');
const mysql = require('mysql');
const connection = mysql.createConnection({
host: 'your-rds-endpoint',
user: 'your-username',
password: 'your-password',
database: 'your-database-name',
port: 3306,
ssl: {
ca: fs.readFileSync('path-to-your-rds-ca-cert.pem')
}
});
connection.connect((err) => {
if (err) {
console.error('Error connecting to the database:', err);
return;
}
console.log('Connected to the MySQL server.');
});
确保你已经下载了 AWS RDS 提供的 CA 证书,并指定了正确的路径。
通过以上步骤,你应该能够成功地将本地 Node.js 服务器连接到 AWS 上的 MySQL 数据库。
领取专属 10元无门槛券
手把手带您无忧上云