我正在尝试将Sequelize连接到本地mySql实例。在测试中,我首先使用了Sqlite,它运行良好。现在,我切换到MySql,在单元测试中遇到访问被拒绝的错误。这是一个node.js项目,正在使用.env设置sequelize模块环境变量以进行本地测试。 在项目中,我有一个models目录,用于初始化sequelize上下文:
models / index.js
let sequelize = {};
if (typeof process.env !== 'undefined' && process.env.ENVIRONMENT === 'production') {
sequelize = new Sequelize(process.env.DB_NAME, process.env.DB_USER, process.env.PASSWORD,
{ host: process.env.DB_HOST, dialect: process.env.MYSQLDIALECT, port: process.env.DB_PORT });
} else if(process.env !== 'undefined' && process.env.ENVIRONMENT === 'development') {
sequelize = new Sequelize({dialect: process.env.DIALECT, storage: process.env.STORAGE});
}
.env
ENVIRONMENT=production
DB_NAME='MYRDSDataStore'
DB_HOST=127.0.0.1
DB_PORT=3306
DB_USER='sqlserviceuser'
DB_PASSWORD='password'
MYSQLDIALECT='mysql'
SQLITEDIALECT='sqlite'
STORAGE='db.sqlite'
主机设置为127.0.0.1,但我也尝试使用localhost并收到相同的错误。
index.test.spec.js
describe('Create Audit Table and insert error object', ()=>{
it('Creates audit table and inserts record',()=>{
let errorObject ={
errorCode: '300',
event: 'POST',
userName: 'testUser',
message: 'Error message from test'
}
util.AuditLogger(errorObject)
.then(()=>{
model.Audit.findAll({
attributes: ['errorCode']
}).then((data)=>{
expect(data).to.have.lengthOf.at.least(1);
});
});
});
});
运行单元测试时,出现以下异常:
Unhandled rejection SequelizeAccessDeniedError: Access denied for user 'sqlserviceuser'@'localhost' (using password: NO)
用户sqlserviceuser是我使用sql workbench创建的新用户,并被授予用户dbo角色(因此,他们目前具有完全访问权限)
在终端窗口中,我尝试了以下命令:
mysql -h localhost -u sqlserviceuser -p MYRDSDataStore
输入此密码,然后提示我输入密码,然后该密码允许我登录,然后输入登录消息:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 29
Server version: 8.0.15 MySQL Community Server - GPL
对于Sequelize为何会失败或我需要进行哪些更改以允许Sequelize进行连接,任何人都可以提供任何建议或想法吗?
谢谢
答案 0 :(得分:0)
sequelize = new Sequelize(process.env.DB_NAME, process.env.DB_USER, process.env.PASSWORD
->
sequelize = new Sequelize(process.env.DB_NAME, process.env.DB_USER, process.env.DB_PASSWORD
应该是这里的问题