Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError [ERR_UNKNOWN_ENCODING]: Unknown encoding: <ref *1> Handshake #132

Closed
jrgleason opened this issue Sep 16, 2020 · 10 comments
Closed

Comments

@jrgleason
Copy link

I have a simple setup.

I have a host machine (OSX) running Virtual Box. The Virtual Box is running a Debian guest, which is running MariaDB. I am using a bridged network.

When I use an app to check the connection it works...

Screen Shot 2020-09-15 at 11 25 14 PM

But when I try to use the node library...

constructor(
        password = process.env.SQL_PASSWORD,
        host="...",
        user="API",
        database = "SITE"){
        this.host = host;
        this.password = password;
        this.user = user;
        this.database = database;
        this.connectionLimit = 5;
}
...
get connection(){
        console.log(`The password is ${this.password}`)
        // return this.pool.getConnection();
        return mariadb.createConnection(this)
}
...
const connection = await connector.connection
console.log("We have the connection");
const rows = await connection.query("SELECT * FROM JRG.TEST;");

I get...

TypeError [ERR_UNKNOWN_ENCODING]: Unknown encoding: <ref *1> Handshake {
  _events: [Object: null prototype],
  _eventsCount: 1,
  _maxListeners: undefined,
  sequenceNo: 1,
  compressSequenceNo: -1,
  resolve: [Function: bound _authSucceedHandler],
  reject: [Function: bound _authFailHandler],
  sending: false,
  _createSecureContext: [Function: bound _createSecureContext],
  _addCommand: [Function: bound _addCommandEnable],
  getSocket: [Function: _getSocket],
  onPacketReceive: [Function: parseHandshakeInit],
  plugin: [Circular *1],
  [Symbol(kCapture)]: false
}
    at new NodeError (internal/errors.js:253:15)
    at Socket.Writable.write (_stream_writable.js:296:13)
    at PacketOutputStream.flushBufferBasic (/.../rollup-demo/modules/esm/rollup-koa/node_modules/mariadb/lib/io/packet-output-stream.js:444:17)
    at Object.send (/.../rollup-demo/modules/esm/rollup-koa/node_modules/mariadb/lib/cmd/handshake/client-handshake-response.js:118:7)
    at Handshake.parseHandshakeInit (/.../rollup-demo/modules/esm/rollup-koa/node_modules/mariadb/lib/cmd/handshake/handshake.js:82:31)
    at PacketInputStream.receivePacketBasic (/.../rollup-demo/modules/esm/rollup-koa/node_modules/mariadb/lib/io/packet-input-stream.js:104:9)
    at PacketInputStream.onData (/.../rollup-demo/modules/esm/rollup-koa/node_modules/mariadb/lib/io/packet-input-stream.js:169:20)
    at Socket.emit (events.js:326:20)
    at addChunk (_stream_readable.js:322:12)
    at readableAddChunk (_stream_readable.js:297:9) {
  code: 'ERR_UNKNOWN_ENCODING'
}
@jrgleason
Copy link
Author

I can confirm this does work...

       const connection = mysql.createConnection({
              password : process.env.SQL_PASSWORD,
              host : "192.168.1.193",
              user : "API",
              database : "SITE"
       });
       connection.connect((err)=>{
              if(err) console.error(`There was a problem connecting ${err}`);
              else{
                     connection.query("SELECT * FROM JRG.TEST;", (error, results)=>{
                            if(error) console.error(`There was an error querying ${error}`)
                            else{
                                   console.log(`results: ${JSON.stringify(results)}`);
                            }
                     });
              }
       })

So it appears to be a problem with the lib

@rusher
Copy link
Collaborator

rusher commented Sep 16, 2020

This means that connection charset received by serveur is not supported.
without setting any of charset , collation or charsetNumber options, default is using 4 bytes utf8 charset. So if not using an very old server (<=5.1) this is supported by server.

Could you try connecting the same way than MySQL (using json param) ?
Does your object has charset , collation or charsetNumber object set somewhere else?

It would be great if you can add the debug log (adding this.debug = true) to identify the issue.

@jrgleason
Copy link
Author

@rusher I can try this but I am not sure what you want me to add. Here is the output with debug...

<== conn:-1 Handshake.parseHandshakeInit (0,99)
+--------------------------------------------------+
|  0  1  2  3  4  5  6  7   8  9  a  b  c  d  e  f |
+--------------------------------------------------+------------------+
| 63 00 00 00 0A 35 2E 35  2E 35 2D 31 30 2E 33 2E | c....5.5.5-10.3. |
| 32 33 2D 4D 61 72 69 61  44 42 2D 30 2B 64 65 62 | 23-MariaDB-0+deb |
| 31 30 75 31 00 2A 00 00  00 48 23 71 34 4D 67 41 | 10u1.*...H#q4MgA |
| 4D 00 FE F7 2D 02 00 BF  81 15 00 00 00 00 00 00 | M...-........... |
| 07 00 00 00 3F 32 7B 37  3E 72 44 3E 49 34 68 30 | ....?2{7>rD>I4h0 |
| 00 6D 79 73 71 6C 5F 6E  61 74 69 76 65 5F 70 61 | .mysql_native_pa |
| 73 73 77 6F 72 64 00                             | ssword.          |
+--------------------------------------------------+------------------+

_stream_writable.js:296
      throw new ERR_UNKNOWN_ENCODING(encoding);
      ^

TypeError [ERR_UNKNOWN_ENCODING]: Unknown encoding: <ref *1> Handshake {
  _events: [Object: null prototype],
  _eventsCount: 1,
  _maxListeners: undefined,
  sequenceNo: 1,
  compressSequenceNo: -1,
  resolve: [Function: bound _authSucceedHandler],
  reject: [Function: bound _authFailHandler],
  sending: false,
  _createSecureContext: [Function: bound _createSecureContext],
  _addCommand: [Function: bound _addCommandEnable],
  getSocket: [Function: _getSocket],
  onPacketReceive: [Function: parseHandshakeInit],
  plugin: [Circular *1],
  [Symbol(kCapture)]: false
}

Seems like even if I can get it to work the default is not working correctly

@Nunatani
Copy link

Hello guys,

I've the same problem @jrgleason : did you fixed it ?

The weird thing is that I have the same configuration (maria DB version 10.4.14 & node module mariadb@2.5.0 on fedora 32) on my laptop and my desktop, but it only fails on my laptop.
I tried as you did with mysql.createConnection, and it worked too on my laptop.

Here is my result with this.debug set to true :

<== conn:-1 Handshake.parseHandshakeInit (0,89)
+--------------------------------------------------+
|  0  1  2  3  4  5  6  7   8  9  a  b  c  d  e  f |
+--------------------------------------------------+------------------+
| 59 00 00 00 0A 35 2E 35  2E 35 2D 31 30 2E 34 2E | Y....5.5.5-10.4. |
| 31 34 2D 4D 61 72 69 61  44 42 00 09 00 00 00 73 | 14-MariaDB.....s |
| 4F 70 65 29 3A 2A 62 00  FE F7 08 02 00 FF 81 15 | Ope):*b......... |
| 00 00 00 00 00 00 07 00  00 00 30 65 2F 58 4F 63 | ..........0e/XOc |
| 24 28 3B 31 24 5E 00 6D  79 73 71 6C 5F 6E 61 74 | $(;1$^.mysql_nat |
| 69 76 65 5F 70 61 73 73  77 6F 72 64 00          | ive_password.    |
+--------------------------------------------------+------------------+

node:internal/streams/writable:296
      throw new ERR_UNKNOWN_ENCODING(encoding);
      ^

TypeError [ERR_UNKNOWN_ENCODING]: Unknown encoding: <ref *1> Handshake {
  _events: [Object: null prototype],
  _eventsCount: 1,
  _maxListeners: undefined,
  sequenceNo: 1,
  compressSequenceNo: -1,
  resolve: [Function: bound _authSucceedHandler],
  reject: [Function: bound _authFailHandler],
  sending: false,
  _createSecureContext: [Function: bound _createSecureContext],
  _addCommand: [Function: bound _addCommandEnable],
  getSocket: [Function: _getSocket],
  onPacketReceive: [Function: parseHandshakeInit],
  plugin: [Circular *1],
  [Symbol(kCapture)]: false
}

@rusher : any idea about how I can debug this ?

@rusher
Copy link
Collaborator

rusher commented Oct 22, 2020

I now have an idea of what can cause this, but fail to reproduce it.
Could you indicate node.js version you are using ?

or if you have the possibility, could you confirm that changing in node_modules/mariadb/lib/connection.js line 949:
_socket.writeBuf = _socket.write;
to
_socket.writeBuf = (buf) => _socket.write(buf);
correct the issue ?

@Nunatani
Copy link

Thanks for the so quick answer !

I'm using node v15.0.1

Yes, it works perfectly with this change ! 👍

What do you suggest ? To downgrade node to an other version ?

@rusher
Copy link
Collaborator

rusher commented Oct 23, 2020

Thanks, issue is reproduced with node v15 version, this is due to stream implementation change for QUIC implementation.
Correction will be done in version 2.5.1 soon. node v15 is not supported until then, so better stay with v14 for now

@rusher rusher closed this as completed Oct 23, 2020
@rusher
Copy link
Collaborator

rusher commented Oct 23, 2020

A corrective version 2.5.1 is now released on npm

@daniele-orlando
Copy link

Thank you guys for the fix ❤️

@jonnytest1

This comment was marked as outdated.

robeady added a commit to robeady/yamplayer that referenced this issue May 8, 2022
robeady added a commit to robeady/yamplayer that referenced this issue May 8, 2022
* update mariadb to fix weird connection error

similar issue to mariadb-corporation/mariadb-connector-nodejs#132 albeit this was much longer ago

* make this compile

* fix some tests

* update tests and migration list

* fix some queries

* lint

* upgrade ts-node-dev and friends to fix startup issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants