MySql - 没有在nodejs中连接

时间:2018-06-17 08:11:33

标签: javascript mysql node.js twitch

我试图设置它,以便我的twitch bot可以连接到数据库来存储数据。但我似乎无法弄清楚如何连接。我输入了所有正确的数据(我可以与客户端连接凭证),但它仍然无法连接。

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);
      }
    })()

错误:

            { 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' }

结果1这没有用,因为它不是mysql是mssql。我有点愚蠢对不起家伙:

      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)

输出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)

输出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)   

2 个答案:

答案 0 :(得分:0)

您正在使用you can specify the encoding on a per-file basis npm库连接到mysql数据库。这是两个完全不同的DBMS系统,因此不是交叉兼容的。您需要使用正确类型的客户端。

我知道您正在尝试连接到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(); 数据库,因为在URL中我可以看到mssql仅支持托管`mysql数据库。这意味着您需要使用正确类型的客户端。

为此,您可以使用https://www.freemysqlhosting.net/ npm依赖项,使用如下代码块:

   this.filteredItems = ko.computed(function () {
        if (!this.searchText()) {
            return this.originalItems();
        }

        var searchTextLowered = this.searchText().toLowerCase();
        return ko.utils.arrayFilter(this.originalItems(), function (item) {
            return item.text().indexOf(searchTextLowered) === 0;
        });
    }, this);

这将使用适当的协议打开连接,并允许您访问数据库。

额外阅读

  • 我读过有关mysql的内容,以填补您对此代码块无效的原因的理解。

答案 1 :(得分:0)

问题由:

修复
  1. 如果您根据npmjs.com/package/mssql使用mssql,则应在host对象上将server替换为sqlConfig。 (host用于mysql,server用于mssql)

  2. port对象上设置sqlConfig

  3. var sqlConfig = { server: "x", username: "x", password: "x", database: "x", port: "x" };

相关问题