Search code examples
javascriptmysqlnode.jstwitch

MySql - not connecting in nodejs


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)   

Solution

  • 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.