SequelizeAccessDeniedError的原因和解决方式

在学习廖雪峰的使用Sequelize时,遇到了一个问题,搞了好久,问题如下:

failed: SequelizeAccessDeniedError: ER_ACCESS_DENIED_ERROR: Access denied for user 'www'@'localhost' (using password: YES)
(node:7512) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): SequelizeAccessDeniedError: ER_ACCESS_DENIED_ERROR: Access denied for user 'www'@'localhost' (using password: YES)
(node:7512) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): SequelizeAccessDeniedError: ER_ACCESS_DENIED_ERROR: Access denied for user 'www'@'localhost' (using password: YES)


最后发现是写mysql的时候 授权后没有flush privileges;的原因。刚开始只写了第一句,没有写第二句。就出现了上面的错误。

解决方法:用root登陆mysql之后,输入如下语句再运行就可以了。

grant all privileges on test.* to 'www'@'%' identified by 'www';
flush privileges;
其中第一句的意思:允许用户www从任何主机连到test数据库,并使用www作为密码。

第二句的意思:刷新MySQL的系统权限相关表,否则会出现拒绝访问。当然,还有一种方法,就是重新启动mysql服务器,来使新设置生效。­

执行第一次结果如下,由于异步执行的,因此刚开始的结果是find 0 pets,又由于await写法更胜一筹,速度更快,因此结果先出来。

init sequelize...
Executing (default): SELECT `id`, `name`, `gender`, `birth`, `createdAt`, `updatedAt`, `version` FROM `pets` AS `pet` WHERE `pet`.`name` = 'Gaffey';
Executing (default): INSERT INTO `pets` (`id`,`name`,`gender`,`birth`,`createdAt`,`updatedAt`,`version`) VALUES ('g-1482410594167','Gaffey',false,'2007-07-07',1482410594167,1482410594167,0);
Executing (default): INSERT INTO `pets` (`id`,`name`,`gender`,`birth`,`createdAt`,`updatedAt`,`version`) VALUES ('d-1482410594167','Odie',false,'2008-08-08',1482410594167,1482410594167,0);
find 0 pets:
created: {"id":"d-1482410594167","name":"Odie","gender":false,"birth":"2008-08-08","createdAt":1482410594167,"updatedAt":1482410594167,"version":0}
created.{"id":"g-1482410594167","name":"Gaffey","gender":false,"birth":"2007-07-07","createdAt":1482410594167,"updatedAt":1482410594167,"version":0}
执行第二次结果,find 1 pets的意思是通过条件查找,找到名字为Gaffey的记录有一条,然后对其进行了更新。

init sequelize...
Executing (default): SELECT `id`, `name`, `gender`, `birth`, `createdAt`, `updatedAt`, `version` FROM `pets` AS `pet` WHERE `pet`.`name` = 'Gaffey';
Executing (default): INSERT INTO `pets` (`id`,`name`,`gender`,`birth`,`createdAt`,`updatedAt`,`version`) VALUES ('g-1482410605916','Gaffey',false,'2007-07-07',1482410605916,1482410605916,0);
Executing (default): INSERT INTO `pets` (`id`,`name`,`gender`,`birth`,`createdAt`,`updatedAt`,`version`) VALUES ('d-1482410605916','Odie',false,'2008-08-08',1482410605916,1482410605916,0);
find 1 pets:
{"id":"g-1482410594167","name":"Gaffey","gender":false,"birth":"2007-07-07","createdAt":1482410594167,"updatedAt":1482410594167,"version":0}
update pet...
Executing (default): UPDATE `pets` SET `gender`=true,`updatedAt`=1482410606256,`version`=1 WHERE `id` = 'g-1482410594167'
created.{"id":"g-1482410605916","name":"Gaffey","gender":false,"birth":"2007-07-07","createdAt":1482410605916,"updatedAt":1482410605916,"version":0}
created: {"id":"d-1482410605916","name":"Odie","gender":false,"birth":"2008-08-08","createdAt":1482410605916,"updatedAt":1482410605916,"version":0}


主程序app.js的代码如下:

const Sequelize =require('sequelize');
const config=require('./config');
console.log('init sequelize...');
var sequelize = new Sequelize(config.database,config.username,config.password,{
    host:config.host,
    dialect:'mysql',
    pool:{
        max:5,
        min:0,
        idle:30000
    }
});
var Pet=sequelize.define('pet',{
    id:{
        type:Sequelize.STRING(50),
        primaryKey: true,
    },
    name: Sequelize.STRING(100),
    gender: Sequelize.BOOLEAN,
    birth: Sequelize.STRING(10),
    createdAt: Sequelize.BIGINT,
    updatedAt: Sequelize.BIGINT,
    version: Sequelize.BIGINT
},{
    timestamps:false
});

var now =Date.now();
Pet.create({
    id: 'g-' + now,
    name: 'Gaffey',
    gender: false,
    birth: '2007-07-07',
    createdAt: now,
    updatedAt: now,
    version: 0
}).then(function (p) {
    console.log('created.' + JSON.stringify(p));
}).catch(function (err) {
    console.log('failed: ' + err);
});

(async () => {
    var dog = await Pet.create({
        id: 'd-' + now,
        name: 'Odie',
        gender: false,
        birth: '2008-08-08',
        createdAt: now,
        updatedAt: now,
        version: 0
    });
    console.log('created: ' + JSON.stringify(dog));
})();

(async () => {
    var pets = await Pet.findAll({
        where: {
            name: 'Gaffey'
        }
    });
    console.log(`find ${pets.length} pets:`);
    for (let p of pets) {
        console.log(JSON.stringify(p));
        console.log('update pet...');
        p.gender = true;
        p.updatedAt = Date.now();
        p.version ++;
        await p.save();
        if (p.version === 3) {
            await p.destroy();
            console.log(`${p.name} was destroyed.`);
        }
    }
})();




你可能感兴趣的:(数据库,nodejs)