0

我是 Web 开发的新手,我已经开始研究 Nodejs。我试图连接到端口 3308上的 MySQL 服务器,WampServer但我总是连接到本地主机服务器。为确保我使用正确的端口和主机名,我尝试使用 MYSQL Workbench 设置连接,并且成功了!但是,我仍然无法弄清楚如何连接 mysql 驱动程序。

const sql = require('mysql');
var con = sql.createConnection({
   host: '127.0.0.1:3308',
   user: 'root',
   password: '',
   port: 3308
});
con.connect(function (err) {
    if (err) throw err;
    console.log("Connected!");
});

createDatabaseQuery= theQ =>{
   return "CREATE DATABASE ".concat(theQ);
}

con.query(createDatabaseQuery('nodedb'), function(err) {
   if (err) {
     throw err;

   }
});

这是我的输出:

[Running] node "c:\Users\Asus\WebstormProjects\Impulse\MySQLTest.js"
c:\Users\Asus\WebstormProjects\Impulse\MySQLTest.js:8
    if (err) throw err;
             ^

Error: connect ECONNREFUSED 127.0.0.1:3306
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1137:16)
    --------------------
    at Protocol._enqueue (c:\Users\Asus\WebstormProjects\Impulse\node_modules\mysql\lib\protocol\Protocol.js:144:48)
    at Protocol.handshake (c:\Users\Asus\WebstormProjects\Impulse\node_modules\mysql\lib\protocol\Protocol.js:51:23)
    at Connection.connect (c:\Users\Asus\WebstormProjects\Impulse\node_modules\mysql\lib\Connection.js:116:18)
    at Object.<anonymous> (c:\Users\Asus\WebstormProjects\Impulse\MySQLTest.js:7:5)
    at Module._compile (internal/modules/cjs/loader.js:1158:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
    at Module.load (internal/modules/cjs/loader.js:1002:32)
    at Function.Module._load (internal/modules/cjs/loader.js:901:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
    at internal/main/run_main_module.js:18:47 {
  errno: 'ECONNREFUSED',
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 3306,
  fatal: true
}

[Done] exited with code=1 in 1.198 seconds
4

0 回答 0