问题描述
我正在Windows上运行苏格兰威士忌箱(无家可归的LAMP堆栈).
I'm running scotch box (a vagrant LAMP stack) on Windows.
我通过PHP或Navicat正常连接到MySQL没问题.
I have no problems connecting to MySQL normally through PHP or Navicat.
但是我试图在Node.js
中连接到它,但出现错误"Error connecting to MySQL: Error: connect ECONNREFUSED 127.0.0.1:3306
"
but I'm trying to connect to it in Node.js
and I get the error "Error connecting to MySQL: Error: connect ECONNREFUSED 127.0.0.1:3306
"
据我了解,似乎我需要设置套接字路径
From what I've understood by searching around it seems that I need to set the socket path
但是当我这样做的时候,我得到了错误"Error connecting to MySQL: Error: connect ENOENT /var/run/mysqld/mysqld.sock
"
but when I do that I get the error "Error connecting to MySQL: Error: connect ENOENT /var/run/mysqld/mysqld.sock
"
我100%确信这是正确的套接字路径.我已经通过SSH进入了无业游民的行列,并验证了mysqld.sock实际上在那里,并且已在/etc/mysql/my.cnf中正确设置了它.跳过网络也处于关闭状态.
I am 100% sure this is the correct socket path. I've SSHed into vagrant and verified that mysqld.sock is actually there and it is correctly set in /etc/mysql/my.cnf. Skip networking is also OFF.
我还尝试将bind-address设置为0.0.0.0并完全注释掉bind-address,重命名mysqld.sock并重新启动mysql服务器,以便它将再次重新创建mysqld.sock.还尝试禁用防火墙(仍然允许使用node.js)并重新安装MySQL.
I've also tried setting the bind-address to 0.0.0.0 and commenting out the bind-address entirely, renaming mysqld.sock and restarting the mysql server so that it would recreate mysqld.sock again. Also tried disabling my firewall (node.js is allowed anyway) and reinstalling MySQL.
这些都不起作用,并且错误保持不变:"Error connecting to MySQL: Error: connect ENOENT /var/run/mysqld/mysqld.sock
"
None of this has worked and the error has remained the same: "Error connecting to MySQL: Error: connect ENOENT /var/run/mysqld/mysqld.sock
"
这是我到目前为止的代码:
this is the code I have so far:
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'root',
database : 'test',
socketPath : '/var/run/mysqld/mysqld.sock'
});
connection.connect(function(err) {
if (err) {
console.error('Error connecting to MySQL: ' + err.stack);
return;
}
console.log('connected as id ' + connection.threadId);
});
我该如何解决?
推荐答案
所以我设法解决了这个问题.
So I managed to solve it.
所有您需要做的就是注释掉my.cnf中的bind-address并使用站点的IP进行连接.
All you have to do is comment out bind-address in my.cnf and connect using the IP of the site.
在我的情况下,这是192.168.33.10(这是scotchbox默认将其设置为
In my case it was 192.168.33.10 (This is what scotchbox sets it to by default).
var mysql = require('mysql');
var connection = mysql.createConnection({
host : '192.168.33.10',
user : 'root',
password : 'root',
database : 'test'
});
connection.connect(function(err) {
if (err) {
console.error('Error connecting to MySQL: ' + err.stack);
return;
}
console.log('connected as id ' + connection.threadId);
});
这篇关于在node.js中连接ENOENT/var/run/mysqld/mysqld.sock时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!