简体   繁体   中英

im trying to connect mysql with nodejs but have error

im trying to connect mysql with nodejs but have error i think its a mysql problem not nodejs.
this is the error

Error: connect ECONNREFUSED 127.0.0.1:3306
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1145:16)
    --------------------
    at Protocol._enqueue (C:\Users\Owner\Desktop\MySQL\node_modules\mysql\lib\protocol\Protocol.js:144:48)
    at Protocol.handshake (C:\Users\Owner\Desktop\MySQL\node_modules\mysql\lib\protocol\Protocol.js:51:23)
    at Connection.connect (C:\Users\Owner\Desktop\MySQL\node_modules\mysql\lib\Connection.js:116:18)
    at Object.<anonymous> (C:\Users\Owner\Desktop\MySQL\server.js:23:12)
    at Module._compile (internal/modules/cjs/loader.js:1015:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1035:10)
    at Module.load (internal/modules/cjs/loader.js:879:32)
    at Function.Module._load (internal/modules/cjs/loader.js:724:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
    at internal/main/run_main_module.js:17:47 {
  errno: 'ECONNREFUSED',
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 3306,
  fatal: true
}

also i dont have mysql in my task manager i think i did something but i got error before it disapeared

my js code is

    const mysql = require('mysql');
    var connection = mysql.createConnection({
      host     : 'localhost',
      port     : 3306,
      user     : 'root',
      password : 'philipthe1',
      database : 'todo'
    });
    connection.connect();
    connection.query("select * from items",(error,res,fields)=>{
        if(error){
            console.error(error);
        }
        else{
            console.log(res)
        }
    })
    connection.end();

It is a mysql error, cuz it says you cannot connect to db.

Can you add the code in which you're trying to connect?

i was able to restart mysql by doing windows key and r on keyboard then services.msc then it gave option start server the docs for it is in mysql docs under restarting server

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM