1

我尝试使用以下代码连接到我的第一个 MySql 服务器

var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  user: "yourusername",
  password: "yourpassword"
});

con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
});

但我收到以下错误。仅当我注释掉“抛出错误”部分时才有效

C:\NodeJS Projects>node demo_db_connection.js
C:\NodeJS Projects\demo_db_connection.js:10
  if (err) throw err;
       ^

Error: connect ECONNREFUSED 127.0.0.1:3306
    at Object.exports._errnoException (util.js:1022:11)
    at exports._exceptionWithHostPort (util.js:1045:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1087:14)
    --------------------
    at Protocol._enqueue (C:\NodeJS 
Projects\node_modules\mysql\lib\protocol\Protocol.js:145:48)
    at Protocol.handshake (C:\NodeJS 
Projects\node_modules\mysql\lib\protocol\Protocol.js:52:23)
    at Connection.connect (C:\NodeJS 
Projects\node_modules\mysql\lib\Connection.js:130:18)
    at Object.<anonymous> (C:\NodeJS Projects\demo_db_connection.js:9:5)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
4

0 回答 0