im trying to set it up so my twitch bot can connect to a database to store data. however i cannot seem to work out how to connect. Ive inputted all the correct data (i can connect with the credetials with a client) and it still isnt connecting.
var sql = require('mssql');
var sqlConfig = {
server: "x",
username: "x",
password: "x",
database: "x"
};
(async function () {
try {
console.log("sql connecting......")
let pool = await sql.connect(sqlConfig)
let result = await pool.request()
.query('select * from Subject') // subject is my database table name
console.log(result )
} catch (err) {
console.log(err);
}
})()
that errors this :
{ ConnectionError: Failed to connect to sql2.freemysqlhosting.net:1433 in 15000ms
at Connection.tedious.once.err (H:\Node JS\TwitchBot\node_modules\mssql\lib\tedious.js:216:17)
at Object.onceWrapper (events.js:315:30)
at emitOne (events.js:116:13)
at Connection.emit (events.js:211:7)
at Connection.connectTimeout (H:\Node JS\TwitchBot\node_modules\tedious\lib\connection.js:924:12)
at ontimeout (timers.js:498:11)
at tryOnTimeout (timers.js:323:5)
at Timer.listOnTimeout (timers.js:290:5)
code: 'ETIMEOUT',
originalError:
{ ConnectionError: Failed to connect to sql2.freemysqlhosting.net:1433 in 15000ms
at ConnectionError (H:\Node JS\TwitchBot\node_modules\tedious\lib\errors.js:12:12)
at Connection.connectTimeout (H:\Node JS\TwitchBot\node_modules\tedious\lib\connection.js:924:28)
at ontimeout (timers.js:498:11)
at tryOnTimeout (timers.js:323:5)
at Timer.listOnTimeout (timers.js:290:5)
message: 'Failed to connect to sql2.freemysqlhosting.net:1433 in 15000ms',
code: 'ETIMEOUT' },
name: 'ConnectionError' }
OUTCOME 1 this didnt work because it was not mysql is mssql. Im a bit stupid sorry guys:
throw new RangeError('Index out of range');
RangeError: Index out of range
at checkOffset (buffer.js:977:11)
at Buffer.readUInt8 (buffer.js:1015:5)
at Packet.isLast (H:\Node JS\TwitchBot\node_modules\tedious\lib\packet.js:116:29)
at ReadablePacketStream.<anonymous> (H:\Node JS\TwitchBot\node_modules\tedious\lib\message-io.js:101:18)
at emitOne (events.js:116:13)
at ReadablePacketStream.emit (events.js:211:7)
at addChunk (H:\Node JS\TwitchBot\node_modules\tedious\node_modules\readable-stream\lib\_stream_readable.js:291:12)
at readableAddChunk (H:\Node JS\TwitchBot\node_modules\tedious\node_modules\readable-stream\lib\_stream_readable.js:278:11)
at ReadablePacketStream.Readable.push (H:\Node JS\TwitchBot\node_modules\tedious\node_modules\readable-stream\lib\_stream_readable.js:245:10)
at ReadablePacketStream.Transform.push (H:\Node JS\TwitchBot\node_modules\tedious\node_modules\readable-stream\lib\_stream_transform.js:148:32)
OUTPUT 2 :
Error: connect ECONNREFUSED 127.0.0.1:3306
at Object._errnoException (util.js:992:11)
at _exceptionWithHostPort (util.js:1014:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1186:14)
--------------------
at Protocol._enqueue (H:\Node JS\TwitchBot\node_modules\mysql\lib\protocol\Protocol.js:145:48)
at Protocol.handshake (H:\Node JS\TwitchBot\node_modules\mysql\lib\protocol\Protocol.js:52:23)
at Connection.connect (H:\Node JS\TwitchBot\node_modules\mysql\lib\Connection.js:130:18)
at Object.<anonymous> (H:\Node JS\TwitchBot\fb.js:11:12)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
OUTPUT 3:
Error: ER_ACCESS_DENIED_ERROR: Access denied for user ''#'MY isp identifier' (using password: YES)
at Handshake.Sequence._packetToError (H:\Node JS\TwitchBot\node_modules\mysql\lib\protocol\sequences\Sequence.js:52:14)
at Handshake.ErrorPacket (H:\Node JS\TwitchBot\node_modules\mysql\lib\protocol\sequences\Handshake.js:130:18)
at Protocol._parsePacket (H:\Node JS\TwitchBot\node_modules\mysql\lib\protocol\Protocol.js:279:23)
at Parser.write (H:\Node JS\TwitchBot\node_modules\mysql\lib\protocol\Parser.js:76:12)
at Protocol.write (H:\Node JS\TwitchBot\node_modules\mysql\lib\protocol\Protocol.js:39:16)
at Socket.<anonymous> (H:\Node JS\TwitchBot\node_modules\mysql\lib\Connection.js:103:28)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at addChunk (_stream_readable.js:263:12)
at readableAddChunk (_stream_readable.js:250:11)
--------------------
at Protocol._enqueue (H:\Node JS\TwitchBot\node_modules\mysql\lib\protocol\Protocol.js:145:48)
at Protocol.handshake (H:\Node JS\TwitchBot\node_modules\mysql\lib\protocol\Protocol.js:52:23)
at Connection.connect (H:\Node JS\TwitchBot\node_modules\mysql\lib\Connection.js:130:18)
at Object.<anonymous> (H:\Node JS\TwitchBot\fb.js:11:12)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
You're using the mssql npm library to connect to a mysql database. These are two completely different DBMS systems and as such, are not cross compatible. You need to use the correct kind of client.
I know you're trying to connect to a mysql db because in the URL I can see https://www.freemysqlhosting.net/ which only supports hosting of a `mysql database. This means you need to use the correct kind of client.
To do this, you can use the mysql npm dependency, with a code block like this:
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'x',
user : 'x',
password : 'x',
database : 'x'
});
connection.connect();
connection.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
if (error) throw error;
console.log('The solution is: ', results[0].solution);
});
connection.end();
This will open a connection using the appropriate protocol and allow you to access your database.
Extra Reading
I'd read about the concept of a DBMS to fill up your understanding of why this code block hasn't worked.
Problem fixed by:
if you are using mssql according to npmjs.com/package/mssql you should replace host by server on sqlConfig object. (host is for mysql, server is for mssql)
set port on sqlConfig object:
var sqlConfig = {
server: "x",
username: "x",
password: "x",
database: "x",
port: "x"
};
Related
I am trying to input some data into a mysql database, but I can't connect to it.
My code:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
I have also installed mysql into my system by using npm install mysql in cmd. Whenever I try to execute the above code, I get this error:
C:\Users\Abhishek\Desktop\Data scraping\scraping.js:60
if (err) throw err;
^
Error: connect ECONNREFUSED 127.0.0.1:3306
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1129:16)
--------------------
at Protocol._enqueue (C:\Users\Abhishek\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Protocol.handshake (C:\Users\Abhishek\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at Connection.connect (C:\Users\Abhishek\node_modules\mysql\lib\Connection.js:116:18)
at Object.<anonymous> (C:\Users\Abhishek\Desktop\Data scraping\scraping.js:59:5)
at Module._compile (node:internal/modules/cjs/loader:1083:30)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1112:10)
at Module.load (node:internal/modules/cjs/loader:948:32)
at Function.Module._load (node:internal/modules/cjs/loader:789:14)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:72:12)
at node:internal/main/run_main_module:17:47 {
errno: -4078,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 3306,
fatal: true
}
How do I fix this?
i have a problem that i cant use mysql server. so i asked the solution in stackoverflow and other error was happend..
var mysql = require("mysql");
var connection = mysql.createConnection({
host: "127.0.0.1",
user: "root",
password: "111111",
database: "opentutorials",
socketPath: "/Applications/MAMP/tmp/mysql/mysql.sock"
});
connection.connect();
connection.query("SELECT * FROM topic", function(error, results, fields) {
if (error) {
console.log(error);
}
console.log(results);
});
connection.end();
here is my code and the error
Error: connect ENOENT /Applications/MAMP/tmp/mysql/mysql.sock
at PipeConnectWrap.afterConnect [as oncomplete] (net.js:1106:14)
--------------------
at Protocol._enqueue (/Users/junggri/Desktop/mysql/node_modules/mysql/lib/protocol/Protocol.js:144:48)
at Protocol.handshake (/Users/junggri/Desktop/mysql/node_modules/mysql/lib/protocol/Protocol.js:51:23)
at Connection.connect (/Users/junggri/Desktop/mysql/node_modules/mysql/lib/Connection.js:119:18)
at Object. (/Users/junggri/Desktop/mysql/nodejs/mysql.js:11:12)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
errno: 'ENOENT',
code: 'ENOENT',
syscall: 'connect',
address: '/Applications/MAMP/tmp/mysql/mysql.sock',
fatal: true }
Let's say I have:
const req = require('net').createConnection(80, 'localhost');
req.on('error', (error) => {
console.error(error) // handle somehow
});
If error event occurs, I get
{ Error: connect ECONNREFUSED 127.0.0.1:80
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1117:14)
errno: 'ECONNREFUSED',
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 80 }
In the terminal.
I'd like instead to see the full stack:
{ Error: connect ECONNREFUSED 127.0.0.1:80
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1117:14)
at Object.<anonymous> (/Users/aleksey/error-handing-example.js:2:7)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)
errno: 'ECONNREFUSED',
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 80 }
Note: I obtained the full trace artificially like this:
Error.captureStackTrace(this);
const req = require('net').createConnection(80, 'localhost');
req.on('error', (error) => {
error.stack += this.stack.replace(/^.+$/, '');
console.error(error) // handle somehow
});
I can't be doing this for all errors, as it's not sustainable
Is there a way to do this:
a. In a way that doesn't hurt production stability (longjohn claims that it's for development only)?
b. Automatically?
Here's a hacky thing I cooked up using async_hooks and overriding Error.captureStackTrace:
const ah = require('async_hooks');
const traces = new Map();
ah.createHook({
init(id) {
const _trace = {};
Error.captureStackTrace(_trace);
traces.set(id, _trace.stack.replace(/(^.+$\n){4}/m, '\n'));
},
destroy(id) {
traces.delete(id);
},
}).enable();
global.Error = class extends Error {
constructor(message) {
super(message);
this.constructor.captureStackTrace(this, this.constructor);
}
static captureStackTrace(what, where) {
super.captureStackTrace.call(Error, what, where);
const trace = traces.get(ah.executionAsyncId());
if (trace) what.stack += trace;
}
};
The gist is:
Keep a map of asyncId => traces
Override Error.captureStackTrace to use the trace from that asyncId on top of the original stack
Not sure about its performance limitations, bugginess, and robustness, but that's all I could come up with .
Now if I have:
const req = net.createConnection(8080);
req.once('error', err => {
console.error(err);
});
instead of
{ Error: connect ECONNREFUSED 127.0.0.1:8080
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1084:14)
I get
{ Error: connect ECONNREFUSED 127.0.0.1:8080
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1084:14)
at TCPConnectWrap.emitInitNative (internal/async_hooks.js:137:43)
at internalConnect (net.js:840:26)
at defaultTriggerAsyncIdScope (internal/async_hooks.js:294:19)
at GetAddrInfoReqWrap.emitLookup [as callback] (net.js:1006:9)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:62:10)
at GetAddrInfoReqWrap.emitInitNative (internal/async_hooks.js:137:43)
at lookup (dns.js:142:19)
at net.js:981:5
at defaultTriggerAsyncIdScope (internal/async_hooks.js:294:19)
at lookupAndConnect (net.js:980:3)
at Socket.connect (net.js:915:5)
at Object.connect (net.js:162:17)
at Object.<anonymous> (/Users/aleksey/epudos-lite/server/test.js:28:17)
versions:
node > 11.4.0
mysql(node package) > 2.16.0
phpMyAdmin > 4.8.3
MySQL > 5.7
I'm using this code:
const mysql = require('mysql');
let db = mysql.createConnection({
host:'hidden',
port:'hidden',
user:'hidden',
password:'hidden',
database: 'hidden',
dubug: true
});
sqlfn.createTable('Internal', db, 'test_table', (result, err) => {
if(err) {console.log(err.stack); return};
console.log('Create OK: ', result);
});
From the function file sqlfn:
exports.createTable = (req, db, name, callback) => {
let query_string = String(`CREATE TABLE ${name} (uid VARCHAR(255), name VARCHAR(255), type VARCHAR(255), data VARCHAR(255), modified VARCHAR(255))`);
db.query(query_string, (err, result) => {
if(err) fn.logit(req, 'SQL', 'error', `in <createTable> ${err}`);
callback(result, err);
});
};
This is the error that I am getting:
>
Error: Packets out of order. Got: 80 Expected: 0
at Parser.write (C:\Users\hidden\Documents\Git\hidden\node_modules\mysql\lib\protocol\Parser.js:42:19)
at Protocol.write (C:\Users\hidden\Documents\Git\hidden\node_modules\mysql\lib\protocol\Protocol.js:38:16)
at Socket. (C:\Users\hidden\Documents\Git\hidden\node_modules\mysql\lib\Connection.js:91:28)
at Socket. (C:\Users\hidden\Documents\Git\hidden\node_modules\mysql\lib\Connection.js:502:10)
at Socket.emit (events.js:189:13)
at addChunk (_stream_readable.js:288:12)
at readableAddChunk (_stream_readable.js:269:11)
at Socket.Readable.push (_stream_readable.js:224:10)
at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:145:17)
--------------------
at Protocol._enqueue (C:\Users\hidden\Documents\Git\hidden\node_modules\mysql\lib\protocol\Protocol.js:144:48) at Protocol.handshake (C:\Users\hidden\Documents\Git\hidden\node_modules\mysql\lib\protocol\Protocol.js:51:23) at Connection.connect (C:\Users\hidden\Documents\Git\hidden\node_modules\mysql\lib\Connection.js:118:18)
at Connection._implyConnect (C:\Users\hidden\Documents\Git\hidden\node_modules\mysql\lib\Connection.js:453:10) at Connection.query (C:\Users\hidden\Documents\Git\hidden\node_modules\mysql\lib\Connection.js:198:8)
at Object.exports.createTable (C:\Users\hidden\Documents\Git\hidden\components\functions\sql.js:36:8)
at Object. (C:\Users\hidden\Documents\Git\hidden\components\controllers\controll_admin.js:22:7)
at Module._compile (internal/modules/cjs/loader.js:723:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:734:10)
at Module.load (internal/modules/cjs/loader.js:620:32)
If anyone know how i can fix or work-around this I'd love some feedback and help regarding this.
thank you.
:)
I was getting the same error and it was because I was not setting the correct port for my database which was 3305 instead of the default 3306
I am new to node js and i am trying to create a database.
i copied code from w3school and tried to run in my pc.
i got the following error:
C:\Users\HP\Desktop>node db.js
C:\Users\HP\Desktop\db.js:10
if (err) throw err;
^
Error: connect ECONNREFUSED 127.0.0.1:3306
at Object._errnoException (util.js:999:13)
at _exceptionWithHostPort (util.js:1020:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1207:14)
--------------------
at Protocol._enqueue (C:\Users\HP\node_modules\mysql\lib\protocol\Protocol.j
s:145:48)
at Protocol.handshake (C:\Users\HP\node_modules\mysql\lib\protocol\Protocol.
js:52:23)
at Connection.connect (C:\Users\HP\node_modules\mysql\lib\Connection.js:130:
18)
at Object.<anonymous> (C:\Users\HP\Desktop\db.js:9:5)
at Module._compile (module.js:660:30)
at Object.Module._extensions..js (module.js:671:10)
at Module.load (module.js:573:32)
at tryModuleLoad (module.js:513:12)
at Function.Module._load (module.js:505:3)
at Function.Module.runMain (module.js:701:10)
C:\Users\HP\Desktop>
db.js:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "myusername",
password: "mypassword"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
/*Create a database named "mydb":*/
con.query("CREATE DATABASE mydb", function (err, result) {
if (err) throw err;
console.log("Database created");
});
});
I am completely new to node js.