我是Node js的新手,我想开始我的第一个项目来适应它。我正在使用多余的框架。
我发现了一些很好的mysql模块,可以轻松下载和安装。模块正确放置在模块文件夹中。
我试着在我的app.js文件中调用它:
...
var mysql = require('mysql');
...
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '27sparks&&55',
database : 'react'
});
connection.connect();
app.locals.mysql = connection;
connection.query('SELECT * FROM comment', function(err, rows, fields) {
if (err) throw err;
console.log(rows);
});
connection.end();
当我尝试使用以下命令运行/ bin目录中的www:node www
以启动我的localhost项目时,我现在在终端中收到以下错误:
/Applications/XAMPP/xamppfiles/htdocs/nodeschool/learn/app.js:79 if(err)throw err; ^
Error: connect ECONNREFUSED 127.0.0.1:3306
at Object.exports._errnoException (util.js:837:11)
at exports._exceptionWithHostPort (util.js:860:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1060:14)
--------------------
at Protocol._enqueue (/Applications/XAMPP/xamppfiles/htdocs/nodeschool/learn/node_modules/mysql/lib/protocol/Protocol.js:135:48)
at Protocol.handshake (/Applications/XAMPP/xamppfiles/htdocs/nodeschool/learn/node_modules/mysql/lib/protocol/Protocol.js:52:41)
at Connection.connect (/Applications/XAMPP/xamppfiles/htdocs/nodeschool/learn/node_modules/mysql/lib/Connection.js:123:18)
at Object.<anonymous> (/Applications/XAMPP/xamppfiles/htdocs/nodeschool/learn/app.js:75:12)
at Module._compile (module.js:434:26)
at Object.Module._extensions..js (module.js:452:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
at require (module.js:384:17)
答案 0 :(得分:0)
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '27sparks&&55',
database : 'react',
socketPath : 'path/to/your/mysqlsocket' // /*example: /var/run/mysqld/mysqld.sock on ubuntu*/
});
到您的创建连接选项对象
{{1}}
答案 1 :(得分:0)
尝试从连接线获取反馈(代码取自here):
var event = document.createEvent("Event");
event.initEvent("submit", false, true);
// args: string type, boolean bubbles, boolean cancelable
element.dispatchEvent(event);
此外,尝试手动将端口设置为3306,即使它默认为。
值得一提的愚蠢问题:你的MySQL实例正在运行吗?它是在听好的端口吗?密码是否正确?