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.