在使用node.js访问mysql数据库方面需要帮助 [英] Need assistance with accessing mysql database using node.js
问题描述
我正在尝试运行一个用于访问mysql数据库的简单节点示例,并收到以下错误-错误:ER_ACCESS_DENIED_ERROR:用户'root'@'ubuntu.local'的访问被拒绝(使用密码:是)
I am trying to run a simple node sample for access a mysql db and am getting the following error-Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'root'@'ubuntu.local' (using password: YES)
var mysql = require('mysql');
var connection = mysql.createConnection({
host : '192.168.0.12',
user : 'root',
password : 'password',
database : 'app'
});
connection.connect();
connection.query('SELECT * from users', function(err, rows, fields) {
if (err) throw err;
console.log('The solution is: ', rows[0]);
});
connection.end();
上面是使用node.js访问mysql的代码.关于我可能做错了的任何建议,谢谢.
The above is the code for accessing the mysql using node.js.Any suggestions as to what I may be doing wrong, thanks in advance.
推荐答案
在遇到相同错误时,我遇到了类似的问题.
I had a similar problem where I was getting the same error.
对我来说,答案最终是我使用了其他端口.默认情况下,mysql期望使用3306端口,但是我使用的是MAMP,其中将MySQL服务器端口指定为8889.将端口定义添加到数据库连接后,就可以了:
The answer for me ended up being that I was using a different port. By default, mysql expects a port of 3306, but I was using MAMP, where the MySQL server port was specified as 8889. As soon as I added a port definition to my database connection, it was fine:
var connection = mysql.createConnection({
port: 8889,
user: 'root',
password : 'password',
database : 'my_database'
});
注意:这适用于使用MAMP的Mac,尚未在ubuntu或Windows上尝试过.
NOTE: This is for Mac using MAMP, haven't tried it on ubuntu or Windows.
这篇关于在使用node.js访问mysql数据库方面需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!