Access denied for user ”@’localhost’ to database

nodejs操作数据出现Access denied for user ''@'localhost' to database 'mysql'错误信息

Access denied for user ”@’localhost’ to database

电脑系统:window7 x64

当前的MySQL版本为:5.6.16。

mysql是刚刚安装的,安装之后默认是没有设置任何密码的,mysql -u root -p直接回车既可以登录

node中的mysql模块版本为:mysql@2.17.1。

以下是js代码:

// 1. 引包
var mysql = require('mysql');
// 2. 创建连接
var connection = mysql.CreateConnection({
    host: 'localhost',
    user: 'root',
    password: '',
    datavase: 'mysql'
});

// 3. 连接数据库
connection.connect();

// 4. 增,删,改,查
connection.query('show tables', function (error, results, firlds) {
    if (error) throw error;
    console.log('students is :', results);
});

// 5. 关闭连接
connection.end();

解决方法:设置root密码即可

set password for root@localhost = password('8888');

而当解决了这个问题之后,又有另一个问题:Access denied for user ''@'localhost' (using password: YES)

评论
:broken_heart: :confounded: :flushed: :frowning: :grinning: :heart: :kissing_heart: :mask: :pensive: :rage: :relaxed: :scream: :smile: :smirk: :sob: :stuck_out_tongue_closed_eyes: :stuck_out_tongue_winking_eye: :wink: