gpt4 book ai didi

mysql - 如何使用 node.js(使用 node-mysql 驱动程序)连接到 MySQL?

转载 作者:太空宇宙 更新时间:2023-11-03 12:23:18 25 4
gpt4 key购买 nike

我有以下代码,我正在尝试运行它。我已经相应地给出了评论,以提及我所做的一切。

// File saved as app.js
var mysql = require('mysql');
var connection = mysql.createConnection({
host:'localhost', //even tried with 127.0.0.1
port:3306, //confirmed port by looking into /etc/mysql/my.cnf has [client] port = 3306
user:'root', //credentials are verified since Php My Admin logs in with same
password:'password' //credentials are verified since Php My Admin logs in with same
//Even tried to connect using _socket: '/var/run/mysqld/mysqld.sock'
});

connection.connect(function(err) {
// connected! (unless `err` is set)
if(err)
console.log(err); return;
});

由于默认端口未更改,phpMyAdmin 对我来说工作正常。但我无法使用 node-mysql driver 进行连接来自 Node.js .

以下是我收到的错误信息:

amolkulkarni@ubuntu:~/Project/Node_MySQL$ node app.js
{ [Error: connect ECONNREFUSED]
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
fatal: true }

有人可以帮忙吗?


更新:基于评论

 amolkulkarni@ubuntu:~/ ps ax | grep mysqld
5905 ? S 0:00 sudo /usr/sbin/mysqld --skip-grant-tables --skip-networking
5906 ? Sl 0:12 /usr/sbin/mysqld --skip-grant-tables --skip-networking
28964 ? Ssl 0:00 /usr/sbin/mysqld
29098 pts/3 S+ 0:00 grep --color=auto mysqld

我可以使用 mysql -hlocalhost -uroot -ppassword 连接到 mysql,但是如果我用 ip 替换,例如mysql -h192.168.1.10 -uroot -ppassword 报错如下:

ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.3.78' (111)

最佳答案

您的服务器以 --skip-networking 启动标志:

Do not listen for TCP/IP connections at all. All interaction with mysqld must be made using named pipes or shared memory (on Windows) or Unix socket files (on Unix). This option is highly recommended for systems where only local clients are permitted.

尝试连接到 unix 套接字或启动启用 TCP 的服务器。

关于mysql - 如何使用 node.js(使用 node-mysql 驱动程序)连接到 MySQL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18782724/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com