0

So I am trying to code a discord.js bot! The discord bot I supposed to log all messages sent in a channel to mysql. I have searched around the internet but haven't found a solution for my problem. ex. node.js mysql error: ECONNREFUSED

But I am running into this error when I try to connect to mysql:

Error: connect ECONNREFUSED 127.0.0.1:3306
    at Object._errnoException (util.js:1022:11)
    at _exceptionWithHostPort (util.js:1044:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1182:14)
    --------------------
    at Protocol._enqueue (P:\home\pi\Documents\MineCityBot\node_modules\mysql\lib\protocol\Protocol.js:145:48)
    at Protocol.handshake (P:\home\pi\Documents\MineCityBot\node_modules\mysql\lib\protocol\Protocol.js:52:23)
    at Connection.connect (P:\home\pi\Documents\MineCityBot\node_modules\mysql\lib\Connection.js:130:18)
    at Object.<anonymous> (P:\home\pi\Documents\MineCityBot\index.js:15:5)
    at Module._compile (module.js:643:30)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
    at Function.Module.runMain (module.js:684:10)

This is my code:

const Discord = require("discord.js");
const client = new Discord.Client();
const fs = require("fs");
const config = require("./config.json");
const mysql = require('mysql');

var con = mysql.createConnection({
  port: '3306',
  host: config.host,
  user: config.user,
  password: config.password,
  database: config.database
});

con.connect(err => {
    if(err) throw err;
    console.log('Mysql connected')
});

console.log(config.StartupMessage)

client.on("message", message => {
  if(message.author.bot) return;

  if(message.channel.id == config.LogChannelId)  {
  var Attachment = (message.attachments).array()
  var sql = "INSERT INTO " + config.Table + " (" + config.ColumnName + ", " + config.ColumnMessage + ", " + config.ComumnAttachment + ") VALUES (" + message.author.tag + ", " + message + ", " + message.attachments + ")"
   con.query(sql, function (err, result) {
      if (err) return console.log(err);
      console.log("1 record inserted");
    })
    console.log(sql)
  };
});
client.login(config.token); 

Update

config.json file:

{
"token":"bot token",
"LogChannelId":"405382594645983233",
"StartupMessage":"Bot has started",

"host":"localhost",
"user":"root",
"password":"password",
"database":"PinkCraft",

"Table":"log",
"ColumnName":"Name",
"ColumnMessage":"Message",
"ColumnId":"id",
"ComumnAttachment":"attachment"
}

I am using service mysql start to start mysql.

Aidan el Goste
  • 383
  • 1
  • 3
  • 23
TheMole
  • 82
  • 1
  • 9
  • Is mysql running? – Phillip Thomas Jan 31 '18 at 15:12
  • 1
    It's not connecting to mysql, & could be for many reasons only you will be able to track down. Is MYSQL running on this same machine, is there some firewall blocking your app. As shown in the link, if you telnet to the host port, what happens. There really isn't enough info here to help. – Keith Jan 31 '18 at 15:13
  • @PhillipThomas Yes mysql is running – TheMole Jan 31 '18 at 15:22
  • @Keith Im not so experienced in troubleshooting :/ What more can i provide to make it easier? – TheMole Jan 31 '18 at 15:23
  • The link you provided suggested running the command `telnet localhost 3306` to verify mysql is running locally on the expected port. What does that command respond with? – Phillip Thomas Jan 31 '18 at 15:26
  • @PhillipThomas `bash: telnet: command not found` but with `SHOW GLOBAL VARIABLES LIKE 'PORT';` the port is 3306 – TheMole Jan 31 '18 at 15:40
  • What happens if you exclude the port all together in the configuration? From the github page, it looks like the port is a number, not a string. I assume if you are using the default port, you can remove this option or correct it to be `port: 3306,` – Phillip Thomas Jan 31 '18 at 15:46
  • I get the exact same error @PhillipThomas :/ – TheMole Jan 31 '18 at 15:47
  • Sadly, were going to need to see the command you used to start the mysql service and the config.js file. Keep in mind you should hide / obfuscate the password. – Phillip Thomas Jan 31 '18 at 15:51
  • @PhillipThomas Okey now i have added the config.json file – TheMole Jan 31 '18 at 16:25
  • The way you are starting the MySQL service by default has no password. Try excluding that option for now. – Phillip Thomas Jan 31 '18 at 16:46
  • Still the same error... @PhillipThomas – TheMole Jan 31 '18 at 17:08
  • I fixed it @PhillipThomas! The problem was that i was running the program from VSC which was connected with samba to my rpi... – TheMole Jan 31 '18 at 19:22
  • @TheMole - can you add your fix as an answer, then accept it please? That way, people can easily see your question has been answered – Blundering Philosopher Feb 18 '18 at 11:35

1 Answers1

0

It was my fault... The problem was that i was running node thru Visual Studio Code via samba to my rpi. When i did run node directly from the pi it worked!

TheMole
  • 82
  • 1
  • 9