Tworzę bota na discorda i potrzebuję bazy danych.
Zainstalowałem za pomocą npm mysql w projekcie, ale nie mogę się połączyć ze swoją bazą na serwerze
const mysql = require('mysql');
const con = mysql.createConnection({
host: 'localhost',
user: 'username',
password: 'password',
database: 'dbname'
});
Wyświetla się błąd
node:events:496
throw er; // Unhandled 'error' event
^
AggregateError
at internalConnectMultiple (node:net:1114:18)
at afterConnectMultiple (node:net:1667:5)
--------------------
at Protocol._enqueue (C:\Users\piotr\Desktop\Bot\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Protocol.handshake (C:\Users\piotr\Desktop\Bot\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at Connection.connect (C:\Users\piotr\Desktop\Bot\node_modules\mysql\lib\Connection.js:116:18)
at Connection._implyConnect (C:\Users\piotr\Desktop\Bot\node_modules\mysql\lib\Connection.js:454:10)
at Connection.query (C:\Users\piotr\Desktop\Bot\node_modules\mysql\lib\Connection.js:196:8)
at Client.<anonymous> (C:\Users\piotr\Desktop\Bot\index.js:37:13)
at Client.emit (node:events:518:28)
at MessageCreateAction.handle (C:\Users\piotr\Desktop\Bot\node_modules\discord.js\src\client\actions\MessageCreate.js:28:14)
at module.exports [as MESSAGE_CREATE] (C:\Users\piotr\Desktop\Bot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (C:\Users\piotr\Desktop\Bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:355:31)
Emitted 'error' event on Connection instance at:
at Connection._handleProtocolError (C:\Users\piotr\Desktop\Bot\node_modules\mysql\lib\Connection.js:423:8)
at Protocol.emit (node:events:518:28)
at Protocol._delegateError (C:\Users\piotr\Desktop\Bot\node_modules\mysql\lib\protocol\Protocol.js:398:10)
at Handshake.<anonymous> (C:\Users\piotr\Desktop\Bot\node_modules\mysql\lib\protocol\Protocol.js:153:12)
at Handshake.emit (node:events:518:28)
at Sequence.end (C:\Users\piotr\Desktop\Bot\node_modules\mysql\lib\protocol\sequences\Sequence.js:78:12)
at Protocol.handleNetworkError (C:\Users\piotr\Desktop\Bot\node_modules\mysql\lib\protocol\Protocol.js:369:14)
at Connection._handleNetworkError (C:\Users\piotr\Desktop\Bot\node_modules\mysql\lib\Connection.js:418:18)
at Socket.emit (node:events:518:28)
at emitErrorNT (node:internal/streams/destroy:169:8) {
code: 'ECONNREFUSED',
fatal: true,
[errors]: [
Error: connect ECONNREFUSED ::1:3306
at createConnectionError (node:net:1634:14)
at afterConnectMultiple (node:net:1664:40) {
errno: -4078,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '::1',
port: 3306
},
Error: connect ECONNREFUSED 127.0.0.1:3306
at createConnectionError (node:net:1634:14)
at afterConnectMultiple (node:net:1664:40) {
errno: -4078,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 3306
}
]
}
Dane połączenia z bazą są poprawne
cały js
const { Client, GatewayIntentBits } = require('discord.js');
const mysql = require('mysql');
const con = mysql.createConnection({
host: 'localhost',
user: 'username',
password: 'pass',
database: 'db'
});
const client = new Client({ intents: [GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent
]});
client.once('ready', () => {
console.log(`Bot ${client.user.tag} jest online!`);
});
client.on('messageCreate', (message) => {
if (message.author.bot) return;
if (!message.content.startsWith('!')) return;
const args = message.content.slice(1).trim().split(/ +/);
const command = args.shift().toLowerCase();
message.channel.send(message + ', ' + args+ ', ' + command);
if (command == 'firma')
{
let companyName = args[0];
let workersLimit = 10;
let employeedWorkers = args[1];
message.channel.send(`Firma ${companyName} została założona. Posiada ona ${employeedWorkers} zatrudnionych pracowników`);
// createCompany(companyName, workersLimit, employeedWorkers);
const sql = `INSERT INTO companies (id, companyName, workersLimit, employeedWorkers) VALUES (NULL, ${companyName}, ${workersLimit}, ${employeedWorkers}')`;
con.query(sql);
}
});