我有一个谷歌云SQL实例,我希望能够从我的机器上本地运行的node.js应用程序访问它。
我已经通过公共IP启用了云SQL实例访问,并在GCP上用我的本地机器IP创建了一个网络,我已经通过几种方式测试了连接:
- MySQL工作台
- 终端
- 从本地运行的NodeJS应用程序使用mysql2连接
上述所有方式都成功连接,我可以按预期在我的云SQL实例上工作,问题是当试图允许我的NodeJS应用程序使用sequelize连接时,我可以看到这个错误
UnhandledPromiseRejectionWarning: SequelizeAccessDeniedError: Access denied for user 'root'@'localhost' (using password: YES)
这是我的连接代码:
host: 'XXX.XX.XX.XX',
port: "",
user: "root",
password: 'password',
database: "DB_NAME",
dialect: "mysql",
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
}
const db = {};
const connection = await mysql.createConnection({ host, port, user, password });
await connection.query(`CREATE DATABASE IF NOT EXISTS `${database}`;`);
const sequelize = new Sequelize(database, user, password, { dialect: dialect });
db.Sequelize = Sequelize;
db.sequelize = sequelize;
// // init models and add them to the exported db object
db.orders = require("../../models/order.model.js")(sequelize, Sequelize);
db.sequelize.sync().then(function(){
console.log('DB connection sucessful.');
}, function(err){
// catch error here
});
似乎我应该传递主机,因为它在某种程度上认为主机是localhost,而没有明确指定
所以这个:
const sequelize = new Sequelize(database, user, password, { dialect: dialect });
应更新为:
const sequelize = new Sequelize(database, user, password, { host: host, dialect: dialect });